package com.aiosman.riderpro

import android.view.RoundedCorner
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.paging.LoadState
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.compose.collectAsLazyPagingItems

val chatNotificationData = ChatNotificationData(
    R.drawable.default_avatar,
    "Tokunaga Yae",
    "Memphis",
    "3 minutes ago",
    6
)

private val ChatData = (0..10).toList().map { chatNotificationData}

@Composable
fun MessagePage(){
    val myBackend = remember { TestChatBackend(ChatData) }
    val pager = remember {
        Pager(
            PagingConfig(
                pageSize = myBackend.DataBatchSize,
                enablePlaceholders = true,
                maxSize = 200
            )
        ) {
            myBackend.getAllData()
        }
    }
    val lazyPagingItems = pager.flow.collectAsLazyPagingItems()
    MessageTopSwitchBtnGroup()
    MessageBarrierLine()
    MessageNotification()
    LazyColumn (
        modifier = Modifier.padding(bottom = 20.dp)
    ){
        if (lazyPagingItems.loadState.refresh == LoadState.Loading) {
            item {
                MomentListLoading()
            }
        }
        items(count = lazyPagingItems.itemCount) { index ->
            val item = lazyPagingItems[index]
            if (item != null) {
                ChatItem(item)
            }
        }
        if (lazyPagingItems.loadState.append == LoadState.Loading) {
            item {
                MomentListLoading()
            }
        }
    }

}

@Composable
fun MessageTopSwitchBtnGroup(){
    Column (
        modifier = Modifier
            .fillMaxWidth()
            .height(113.dp)
    ) {
        Row(modifier = Modifier.fillMaxSize()){
            val notificationBtnModifier = Modifier
                .fillMaxHeight()
                .weight(1f)
            NotificationBtn(notificationBtnModifier,drawableId = R.drawable.rider_pro_like,
                displayText = "LIKE")
            NotificationBtn(notificationBtnModifier,drawableId = R.drawable.rider_pro_followers,
                displayText = "FOLLOWERS")
            NotificationBtn(notificationBtnModifier,drawableId = R.drawable.rider_pro_comments,
                displayText = "COMMENTS")
        }
    }
}

@Composable
fun MessageBarrierLine(){
    Box(modifier = Modifier
        .fillMaxWidth()
        .height(1.dp)
        .padding(start = 24.dp, end = 24.dp)
        .border(width = 1.dp, Color(0f, 0f, 0f, 0.1f))
    )
}

@Composable
fun NotificationBtn(modifier: Modifier, drawableId: Int, displayText: String){
    Box(modifier = modifier,
        contentAlignment = Alignment.Center){
        Column(modifier = Modifier
            .size(width = 79.dp, height = 88.dp),
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.CenterHorizontally){
            Box(modifier = Modifier
                .padding(top = 8.dp)
                .size(width = 55.dp, height = 55.dp)
                .shadow(
                    spotColor = Color.White,
                    ambientColor = Color(0f, 0f, 0f, 0.4f),
                    elevation = 12.dp,
                ),
                contentAlignment = Alignment.Center,
            ){
                Image(
                    modifier = Modifier
                        .size(width = 24.dp, height = 24.dp),
                    painter = painterResource(id = drawableId),
                    contentDescription = ""
                )
            }
            Text(
                modifier = Modifier.padding(top = 8.dp),
                text = displayText,
                fontSize = 12.sp, style = TextStyle(fontWeight = FontWeight.Bold)
            )
        }
    }
}

@Composable
fun MessageNotification(){
    Row(modifier = Modifier
        .fillMaxWidth()
        .height(88.dp)
        .padding(start = 22.dp, top = 20.dp, bottom = 20.dp, end = 24.dp),
        verticalAlignment = Alignment.CenterVertically){
        Box(modifier = Modifier
            .size(width = 48.dp, height = 48.dp)
            .border(width = 1.dp, Color(0f, 0f, 0f, 0.1f), RoundedCornerShape(2.dp)),
            contentAlignment = Alignment.Center){
            Icon(
                modifier = Modifier
                    .size(width = 24.dp, height = 24.dp),
                painter = painterResource(R.drawable.rider_pro_notification),
                contentDescription = ""
            )
        }
        Text(text = "NOTIFICATIONS", fontSize = 18.sp, modifier = Modifier.padding(start = 12.dp), style = TextStyle(fontWeight = FontWeight.Bold))
        Box(modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.CenterEnd){
            Box(modifier = Modifier
                .height(18.dp)
                .clip(RoundedCornerShape(10.dp))
                .background(Color.Red),
                contentAlignment = Alignment.Center){
                Text(text = "18", fontSize = 10.sp, color = Color.White, modifier = Modifier.padding(start = 6.dp, end = 6.dp, top = 2.dp, bottom = 2.dp))
                }
            }
        }
}

@Composable
fun ChatItem(chatNotificationData: ChatNotificationData){
    Row(modifier = Modifier
        .fillMaxWidth()
        .height(88.dp)
        .padding(start = 22.dp, top = 20.dp, bottom = 20.dp, end = 24.dp),
        verticalAlignment = Alignment.CenterVertically){
        Image(modifier = Modifier
            .size(width = 48.dp, height = 48.dp)
            .clip(RoundedCornerShape(2.dp)),
            painter = painterResource(chatNotificationData.avatar),
            contentDescription = "")
        Column (
            modifier = Modifier.fillMaxHeight().padding(start = 12.dp),
            verticalArrangement = Arrangement.SpaceAround
        ){
            Text(text = chatNotificationData.name, fontSize = 18.sp, style = TextStyle(fontWeight = FontWeight.Bold))
            Text(text = chatNotificationData.message, fontSize = 14.sp,
                color = Color(0f, 0f, 0f, 0.6f))
        }

        Box(modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.CenterEnd){
            Column (
                modifier = Modifier.fillMaxHeight(),
                verticalArrangement = Arrangement.SpaceAround,
                horizontalAlignment = Alignment.End
            ){
                Text(text = chatNotificationData.time, fontSize = 12.sp,color = Color(0f, 0f, 0f, 0.4f))
                Box(modifier = Modifier
                    .height(18.dp)
                    .clip(RoundedCornerShape(10.dp))
                    .background(Color.Red),
                    contentAlignment = Alignment.Center){
                    Text(text = chatNotificationData.unread.toString(), fontSize = 10.sp, color = Color.White, modifier = Modifier.padding(start = 6.dp, end = 6.dp, top = 2.dp, bottom = 2.dp))
                }
            }
        }
    }
}