package com.aiosman.ravenow.ui.index.tabs.profile.composable

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
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.aspectRatio
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.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
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 com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R
import com.aiosman.ravenow.entity.MomentEntity
import com.aiosman.ravenow.exp.formatPostTime2
import com.aiosman.ravenow.ui.NavigationRoute
import com.aiosman.ravenow.ui.composables.AnimatedLikeIcon
import com.aiosman.ravenow.ui.composables.CustomAsyncImage
import com.aiosman.ravenow.ui.composables.MomentOperateBtn
import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import com.aiosman.ravenow.ui.navigateToPost
import com.aiosman.ravenow.ui.post.NewPostViewModel

@Composable
fun EmptyMomentPostUnit() {
    TimeGroup(stringResource(R.string.empty_my_post_title))
    ProfileEmptyMomentCard()
}

@Composable
fun ProfileEmptyMomentCard(

) {
    val AppColors = LocalAppTheme.current
    var columnHeight by remember { mutableStateOf(0) }
    val navController = LocalNavController.current

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(start = 24.dp, top = 18.dp, end = 24.dp)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            Canvas(
                modifier = Modifier
                    .height(with(LocalDensity.current) { columnHeight.toDp() })
                    .width(14.dp)
            ) {
                drawLine(
                    color = Color(0xff899DA9),
                    start = Offset(0f, 0f),
                    end = Offset(0f, size.height),
                    strokeWidth = 4f,
                    pathEffect = PathEffect.dashPathEffect(floatArrayOf(20f, 20f), 0f)
                )
            }
            Spacer(modifier = Modifier.width(10.dp))
            Column(
                modifier = Modifier
                    .weight(1f)
                    .onGloballyPositioned { coordinates ->
                        columnHeight = coordinates.size.height
                    }
            ) {
                Text(stringResource(R.string.empty_my_post_content), fontSize = 16.sp)
                Spacer(modifier = Modifier.height(24.dp))
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .aspectRatio(3f / 2f)
                        .background(Color.White)
                        .padding(16.dp)
                ) {
                    Box(
                        modifier = Modifier
                            .fillMaxSize()
                            .background(Color(0xFFF5F5F5))
                            .noRippleClickable {
                                NewPostViewModel.asNewPost()
                                navController.navigate(NavigationRoute.NewPost.route)
                            }
                    ) {
                        Icon(
                            Icons.Default.Add,
                            tint = Color(0xFFD8D8D8),
                            contentDescription = "New post",
                            modifier = Modifier
                                .size(32.dp)
                                .align(Alignment.Center)
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun MomentPostUnit(
    momentEntity: MomentEntity,
    onCommentClick: () -> Unit = {},
    onLikeClick: () -> Unit = {}
) {
    TimeGroup(momentEntity.time.formatPostTime2())
    ProfileMomentCard(
        momentEntity.momentTextContent,
        momentEntity.images[0].thumbnail,
        momentEntity.likeCount.toString(),
        momentEntity.commentCount.toString(),
        momentEntity = momentEntity,
        onCommentClick = onCommentClick,
        onLikeClick = onLikeClick
    )
}

@Composable
fun TimeGroup(time: String = "2024.06.08 12:23") {
    val appColors = LocalAppTheme.current
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(start = 24.dp, top = 40.dp, end = 24.dp),
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            modifier = Modifier
                .height(16.dp)
                .width(14.dp),
            painter = painterResource(id = R.drawable.rider_pro_moment_time_flag),
            contentDescription = ""
        )
        Spacer(modifier = Modifier.width(12.dp))
        Text(
            text = time,
            fontSize = 16.sp,
            color = appColors.text,
            style = TextStyle(fontWeight = FontWeight.W600)
        )
    }
}

@Composable
fun ProfileMomentCard(
    content: String,
    imageUrl: String,
    like: String,
    comment: String,
    momentEntity: MomentEntity,
    onCommentClick: () -> Unit = {},
    onLikeClick: () -> Unit = {}
) {
    var columnHeight by remember { mutableStateOf(0) }
    val AppColors = LocalAppTheme.current
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(start = 24.dp, top = 18.dp, end = 24.dp)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            Canvas(
                modifier = Modifier
                    .height(with(LocalDensity.current) { columnHeight.toDp() })
                    .width(14.dp)
            ) {
                drawLine(
                    color = AppColors.divider,
                    start = Offset(0f, 0f),
                    end = Offset(0f, size.height),
                    strokeWidth = 4f,
                    pathEffect = PathEffect.dashPathEffect(floatArrayOf(20f, 20f), 0f)
                )
            }
            Spacer(modifier = Modifier.width(10.dp))
            Column(
                modifier = Modifier
                    .background(
                        AppColors.background)
                    .weight(1f)
                    .onGloballyPositioned { coordinates ->
                        columnHeight = coordinates.size.height
                    }
            ) {
                if (content.isNotEmpty()) {
                    MomentCardTopContent(content)
                }
                MomentCardPicture(imageUrl, momentEntity = momentEntity)
                MomentCardOperation(
                    like = like,
                    isLike = momentEntity.liked,
                    onLikeClick = onLikeClick,
                    comment = comment,
                    onCommentClick = onCommentClick
                )
            }
        }
    }
}

@Composable
fun MomentCardTopContent(content: String) {
    val AppColors = LocalAppTheme.current
    Row(
        modifier = Modifier
            .fillMaxWidth(),
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(
            modifier = Modifier.padding(top = 16.dp, bottom = 0.dp, start = 16.dp, end = 16.dp),
            text = content, fontSize = 16.sp, color = AppColors.text
        )
    }
}

@Composable
fun MomentCardPicture(imageUrl: String, momentEntity: MomentEntity) {
    val navController = LocalNavController.current
    val context = LocalContext.current
    CustomAsyncImage(
        context,
        imageUrl,
        modifier = Modifier
            .fillMaxSize()
            .aspectRatio(3f / 2f)
            .padding(top = 16.dp)
            .noRippleClickable {
                navController.navigateToPost(
                    id = momentEntity.id,
                    highlightCommentId = 0,
                    initImagePagerIndex = 0
                )
            },
        contentDescription = "",
        contentScale = ContentScale.Crop
    )


}

@Composable
fun MomentCardOperation(
    like: String,
    isLike: Boolean,
    onLikeClick: () -> Unit,
    comment: String,
    onCommentClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(48.dp),
        horizontalArrangement = Arrangement.End,
        verticalAlignment = Alignment.CenterVertically
    ) {
        MomentOperateBtn(count = like) {
            AnimatedLikeIcon(
                liked = isLike,
                onClick = onLikeClick,
            )
        }

        MomentOperateBtn(count = comment) {
            Image(
                painter = painterResource(id = R.drawable.rider_pro_moment_comment),
                contentDescription = "",
                colorFilter = ColorFilter.tint(LocalAppTheme.current.text),
                modifier = Modifier.noRippleClickable {
                    onCommentClick()
                }

            )
        }
    }
}

@Composable
fun MomentCardOperationItem(@DrawableRes drawable: Int, number: String, modifier: Modifier) {
    val AppColors = LocalAppTheme.current
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            modifier = Modifier.padding(start = 16.dp, end = 8.dp),
            painter = painterResource(id = drawable), contentDescription = "",
            colorFilter = ColorFilter.tint(AppColors.text)
        )
        Text(text = number, color = AppColors.text)
    }
}