package com.aiosman.riderpro.ui.like

import androidx.compose.foundation.background
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.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.paging.compose.collectAsLazyPagingItems
import com.aiosman.riderpro.AppState
import com.aiosman.riderpro.LocalNavController
import com.aiosman.riderpro.R
import com.aiosman.riderpro.entity.AccountLikeEntity
import com.aiosman.riderpro.exp.timeAgo
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.comment.NoticeScreenHeader
import com.aiosman.riderpro.ui.composables.AnimatedLikeIcon
import com.aiosman.riderpro.ui.composables.BottomNavigationPlaceholder
import com.aiosman.riderpro.ui.composables.CustomAsyncImage
import com.aiosman.riderpro.ui.composables.StatusBarMaskLayout
import com.aiosman.riderpro.ui.index.tabs.profile.MyProfileViewModel
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import java.util.Date

@Preview
@Composable
fun LikeNoticeScreen() {
    val model = LikeNoticeViewModel
    val listState = rememberLazyListState()
    var dataFlow = model.likeItemsFlow
    var likes = dataFlow.collectAsLazyPagingItems()
    LaunchedEffect(Unit) {
        model.reload()
        model.updateNotice()
    }

    StatusBarMaskLayout(
        darkIcons = true,
        maskBoxBackgroundColor = Color(0xFFFFFFFF)
    ) {
        Column(
            modifier = Modifier
                .weight(1f)
                .background(color = Color(0xFFFFFFFF))
                .padding(horizontal = 16.dp)
        ) {
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 16.dp)
            ) {
                NoticeScreenHeader(
                    stringResource(R.string.like_upper),
                    moreIcon = false
                )
            }

//            Spacer(modifier = Modifier.height(28.dp))
            LazyColumn(
                modifier = Modifier.weight(1f),
                state = listState,
            ) {
                items(likes.itemCount) {
                    val likeItem = likes[it]
                    if (likeItem != null) {
                        likeItem.post?.let { post ->
                            ActionPostNoticeItem(
                                avatar = likeItem.user.avatar,
                                nickName = likeItem.user.nickName,
                                likeTime = likeItem.likeTime,
                                thumbnail = post.images[0].thumbnail,
                                action = "like",
                                userId = likeItem.user.id,
                                postId = post.id
                            )
                        }
                        likeItem.comment?.let { comment ->
                            LikeCommentNoticeItem(likeItem)
                        }
                    }
                }
                item {
                    BottomNavigationPlaceholder()
                }
            }
        }
    }
}


@Composable
fun ActionPostNoticeItem(
    avatar: String,
    nickName: String,
    likeTime: Date,
    thumbnail: String,
    action: String,
    userId: Int,
    postId: Int
) {
    val context = LocalContext.current
    val navController = LocalNavController.current
    Box(
        modifier = Modifier.padding(vertical = 16.dp)
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.Top,
        ) {
            CustomAsyncImage(
                context,
                imageUrl = avatar,
                modifier = Modifier
                    .size(48.dp)
                    .clip(CircleShape)
                    .noRippleClickable {
                        navController.navigate(
                            NavigationRoute.AccountProfile.route.replace(
                                "{id}",
                                userId.toString()
                            )
                        )
                    },
                contentDescription = action,
            )
            Spacer(modifier = Modifier.width(12.dp))
            Column(
                modifier = Modifier
                    .weight(1f)
                    .noRippleClickable {
                        navController.navigate(
                            NavigationRoute.Post.route.replace(
                                "{id}",
                                postId.toString()
                            )
                        )
                    }
            ) {
                Text(nickName, fontWeight = FontWeight.Bold, fontSize = 16.sp)
                Spacer(modifier = Modifier.height(2.dp))
                when (action) {
                    "like" -> Text("Like your post")
                    "favourite" -> Text("Favourite your post")
                }
                Spacer(modifier = Modifier.height(2.dp))
                Row {
                    Text(likeTime.timeAgo(context), fontSize = 12.sp, color = Color(0x99000000))
                }
            }
            CustomAsyncImage(
                context,
                imageUrl = thumbnail,
                modifier = Modifier.size(48.dp),
                contentDescription = action,
            )
        }
    }
}

@Composable
fun LikeCommentNoticeItem(
    item: AccountLikeEntity
) {
    val navController = LocalNavController.current
    val context = LocalContext.current
    Box(
        modifier = Modifier.padding(vertical = 16.dp).noRippleClickable {
            item.comment?.postId.let {
                navController.navigate(
                    NavigationRoute.Post.route.replace(
                        "{id}",
                        it.toString()
                    )
                )
            }

        }
    ) {
        Column(

        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.Top,
            ) {
                CustomAsyncImage(
                    imageUrl = item.user.avatar,
                    modifier = Modifier
                        .size(48.dp)
                        .clip(CircleShape),
                    contentDescription = "Like your comment"
                )
                Spacer(modifier = Modifier.width(12.dp))
                Column(
                    modifier = Modifier
                        .weight(1f)
                ) {
                    Text(item.user.nickName, fontWeight = FontWeight.Bold, fontSize = 16.sp)
                    Spacer(modifier = Modifier.height(2.dp))
                    Text("Like your comment")
                    Spacer(modifier = Modifier.height(2.dp))
                    Row {
                        Text(
                            item.likeTime.timeAgo(context),
                            fontSize = 12.sp,
                            color = Color(0x99000000)
                        )
                    }
                }
            }
            Spacer(modifier = Modifier.height(12.dp))
            Row(
                modifier = Modifier.padding(start = 48.dp)
            ) {
                Box(
                    modifier = Modifier
                        .size(24.dp)
                        .background(Color.Gray.copy(alpha = 0.1f))
                ) {
                    CustomAsyncImage(
                        context = context,
                        imageUrl = MyProfileViewModel.avatar,
                        contentDescription = "Comment Profile Picture",
                        modifier = Modifier.size(24.dp),
                        contentScale = ContentScale.Crop
                    )
                }
                Spacer(modifier = Modifier.width(8.dp))
                Column(
                    modifier = Modifier.weight(1f)
                ) {
                    Text(
                        text = MyProfileViewModel.nickName,
                        fontWeight = FontWeight.W600,
                        fontSize = 14.sp
                    )
                    Text(
                        text = item.comment?.content ?: "",
                        fontSize = 12.sp,
                        color = Color(0x99000000),
                        maxLines = 2
                    )
                }
            }


        }

    }
}