package com.example.pmulabs.designElem.items

import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
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.requiredHeight
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.example.pmulabs.basecomponents.navigate.BottomBarScreen
import com.example.pmulabs.room.models.Comment
import com.example.pmulabs.room.models.User
import com.example.pmulabs.viewModels.AppViewModelProvider
import com.example.pmulabs.viewModels.CommentItemViewModel
import com.example.pmulabs.viewModels.CurrentUserViewModel
import kotlinx.coroutines.launch

@SuppressLint("CoroutineCreationDuringComposition", "UnrememberedMutableState")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CommentItem(navController: NavController,modifier: Modifier = Modifier, comm : Comment,commentItemViewModel: CommentItemViewModel = viewModel(factory = AppViewModelProvider.Factory), currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory)) {

    var getUser by remember { mutableStateOf(currentUserViewModel.user) }
    var openDialog by remember { mutableStateOf(false) }
    var text by remember { mutableStateOf(comm.text) }
    val coroutineScope = rememberCoroutineScope()
    commentItemViewModel.getUsersByArticle(comm.articleId)
    var users by mutableStateOf(commentItemViewModel.userListByArticle)
    var user by mutableStateOf<User?>(null)
    for(item in users){
        if(comm.userId==item.id){
            user=item
        }
    }

    if (openDialog) {
        AlertDialog(
            onDismissRequest = {
                openDialog = false
                text=comm.text
            },
            content = {
                Column(
                    modifier = Modifier
                        .clip(RoundedCornerShape(16.dp))
                        .background(Color.White)
                        .padding(16.dp),
                ) {
                    Text(
                        text = "Edit your comment",
                        color = Color(0xff423a99),
                        style = TextStyle(
                            fontSize = 20.sp,
                        ),
                    )
                    OutlinedTextField(
                        value = text,
                        onValueChange = {
                            text=it
                        },
                        colors = TextFieldDefaults.textFieldColors(
                            containerColor = Color.White,
                            focusedIndicatorColor = Color(0xff423a99),
                            focusedLabelColor = Color(0xff423a99),
                            unfocusedIndicatorColor = Color(0xff423a99),
                            unfocusedLabelColor = Color(0xff423a99)
                        ),
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .padding(10.dp),
                        label = { Text("Comment") },
                        placeholder = { Text("Write comment...") },
                        minLines = 3,
                        maxLines = 3,
                    )
                    Row(
                        modifier = Modifier.padding(all = 8.dp),
                        horizontalArrangement = Arrangement.Center
                    ) {
                        Button(
                            colors = ButtonColors(
                                containerColor = Color(0xff423a99),
                                disabledContainerColor = Color(0xff423a99),
                                contentColor = Color.White,
                                disabledContentColor = Color.White
                            ),
                            onClick = {
                                openDialog = false
                                comm.text=text
                                coroutineScope.launch {
                                    commentItemViewModel.updateComment(comm)
                                }
                            },
                            modifier = Modifier
                                .padding(start=100.dp)
                                .fillMaxWidth(0.5f)
                                .height(40.dp)
                        ) {
                            Text(text = "Edit", fontSize = 20.sp)
                        }
                    }
                }
            },
        )
    }

    Row(
        modifier = Modifier
            .padding(3.dp)
            .fillMaxWidth()
            .background(Color.White)
    )
    {
        Column(
            modifier= Modifier
                .padding(start = 10.dp,top=10.dp)
        ) {
            Row() {
                Text(
                    text = "${user?.nickname}",
                    color = Color(0xff423a99),
                    style = TextStyle(
                        textDecoration = TextDecoration.Underline,
                        fontSize = 17.sp,
                        fontWeight = FontWeight.Bold
                    ),
                )
                if(getUser?.id==comm.userId) {
                    Icon(
                        modifier=Modifier
                            .requiredHeight(20.dp)
                            .clickable {
                                openDialog=true
                            },
                        imageVector = Icons.Filled.Edit,
                        contentDescription = "Update Icon",
                        tint = Color(0xff423a99)
                    )
                    Icon(
                        modifier=Modifier
                            .requiredHeight(20.dp)
                            .clickable {
                                coroutineScope.launch {
                                    commentItemViewModel.deleteComment(comm)
                                }
                                navController.navigate(BottomBarScreen.ArticlePage.passId(comm.articleId.toString()))
                            },
                        imageVector = Icons.Filled.Close,
                        contentDescription = "Delete Icon",
                        tint = Color(0xff423a99)
                    )
                }
            }
            Text(
                text = "${comm.text}",
                color = Color(0xff423a99),
                style = TextStyle(fontSize = 15.sp, fontWeight = FontWeight.Bold),
            )
            Spacer(modifier = Modifier.padding(10.dp))
        }
    }

}
