package com.aiosman.riderpro.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.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.riderpro.LocalNavController
import com.aiosman.riderpro.R
import com.aiosman.riderpro.entity.MomentEntity
import com.aiosman.riderpro.exp.formatPostTime2
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.composables.CustomAsyncImage
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import com.aiosman.riderpro.ui.navigateToPost
import com.aiosman.riderpro.ui.post.NewPostViewModel

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

@Composable
fun ProfileEmptyMomentCard(

) {
    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) {
    TimeGroup(momentEntity.time.formatPostTime2())
    ProfileMomentCard(
        momentEntity.momentTextContent,
        momentEntity.images[0].thumbnail,
        momentEntity.likeCount.toString(),
        momentEntity.commentCount.toString(),
        momentEntity = momentEntity
    )
}

@Composable
fun TimeGroup(time: String = "2024.06.08 12:23") {
    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 = Color.Black,
            style = TextStyle(fontWeight = FontWeight.W600)
        )
    }
}

@Composable
fun ProfileMomentCard(
    content: String,
    imageUrl: String,
    like: String,
    comment: String,
    momentEntity: MomentEntity
) {
    var columnHeight by remember { mutableStateOf(0) }

    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
                    .background(Color.White)
                    .weight(1f)
                    .onGloballyPositioned { coordinates ->
                        columnHeight = coordinates.size.height
                    }
            ) {
                if (content.isNotEmpty()) {
                    MomentCardTopContent(content)
                }
                MomentCardPicture(imageUrl, momentEntity = momentEntity)
                MomentCardOperation(like, comment)
            }
        }
    }
}

@Composable
fun MomentCardTopContent(content: String) {
    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 = Color.Black
        )
    }
}

@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, comment: String) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(48.dp),
        horizontalArrangement = Arrangement.End,
        verticalAlignment = Alignment.CenterVertically
    ) {
        MomentCardOperationItem(
            drawable = R.drawable.rider_pro_like,
            number = like,
            modifier = Modifier.padding(end = 32.dp)
        )
        MomentCardOperationItem(
            drawable = R.drawable.rider_pro_moment_comment,
            number = comment,
            modifier = Modifier.padding(end = 32.dp)
        )
    }
}

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