package com.aiosman.riderpro.ui.composables

import androidx.compose.animation.Crossfade
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
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.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.riderpro.R
import com.aiosman.riderpro.entity.CommentEntity
import com.aiosman.riderpro.ui.modifiers.noRippleClickable

@Composable
fun EditCommentBottomModal(
    replyComment: CommentEntity? = null,
    onSend: (String) -> Unit = {}
) {
    var text by remember { mutableStateOf("") }
    var navBarHeight = WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding()
    val focusRequester = remember { FocusRequester() }
    val context = LocalContext.current

    LaunchedEffect(Unit) {
        focusRequester.requestFocus()
    }

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(Color(0xfff7f7f7))
            .padding(horizontal = 16.dp, vertical = 16.dp)
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.fillMaxWidth()
        ) {
            Text(
                if (replyComment == null) "评论" else "回复",
                fontWeight = FontWeight.W600,
                modifier = Modifier.weight(1f),
                fontSize = 20.sp,
                fontStyle = FontStyle.Italic
            )
            Crossfade(targetState = text.isNotEmpty(), animationSpec = tween(500)) { isNotEmpty ->
                Image(
                    painter = rememberUpdatedState(
                        if (isNotEmpty) painterResource(id = R.drawable.rider_pro_send) else painterResource(
                            id = R.drawable.rider_pro_send_disable
                        )
                    ).value,
                    contentDescription = "Send",
                    modifier = Modifier
                        .size(32.dp)
                        .noRippleClickable {
                            if (text.isNotEmpty()){
                                onSend(text)
                                text = ""
                            }

                        },
                )
            }
        }
        Spacer(modifier = Modifier.height(16.dp))
        if (replyComment != null) {
            Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                Box(
                    modifier = Modifier
                        .size(24.dp)
                        .clip(CircleShape)
                ) {
                    CustomAsyncImage(
                        context,
                        replyComment.avatar,
                        modifier = Modifier.fillMaxSize(),
                        contentDescription = "Avatar",
                    )
                }
                Spacer(modifier = Modifier.width(8.dp))
                Text(replyComment.name, fontWeight = FontWeight.Bold, fontSize = 16.sp)
            }
            Spacer(modifier = Modifier.height(4.dp))
            Text(
                replyComment.comment,
                maxLines = 1,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(start = 32.dp),
                overflow = TextOverflow.Ellipsis
            )
            Spacer(modifier = Modifier.height(16.dp))
        }
        Row(
            modifier = Modifier
                .fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .weight(1f)
                    .clip(RoundedCornerShape(20.dp))
                    .background(Color(0xffe5e5e5))
                    .padding(horizontal = 16.dp, vertical = 16.dp)
            ) {
                BasicTextField(
                    value = text,
                    onValueChange = {
                        text = it
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .focusRequester(focusRequester),
                    textStyle = TextStyle(
                        color = Color.Black,
                        fontWeight = FontWeight.Normal
                    ),
                    minLines = 5
                )
            }
        }
        Spacer(modifier = Modifier.height(navBarHeight))
    }
}