package com.example.pmulabs.designElem.items

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
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.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.layout.requiredWidth
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.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
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.R
import com.example.pmulabs.basecomponents.navigate.BottomBarScreen
import com.example.pmulabs.room.models.Tag
import com.example.pmulabs.viewModels.AppViewModelProvider
import com.example.pmulabs.viewModels.CurrentUserViewModel
import com.example.pmulabs.viewModels.TagItemViewModel
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TagItem(navController: NavController,teg: Tag, modifier: Modifier = Modifier, onTagClick: () -> Unit, viewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory), tagViewModel: TagItemViewModel =viewModel(factory = AppViewModelProvider.Factory)) {

    var getUser by remember { mutableStateOf(viewModel.user) }
    var openDialog by remember { mutableStateOf(false) }
    var text by remember { mutableStateOf(teg.title) }
    val coroutineScope = rememberCoroutineScope()

    if (openDialog) {
        AlertDialog(
            onDismissRequest = {
                openDialog = false
                text = teg.title
            },
            content = {
                Column(
                    modifier = Modifier
                        .clip(RoundedCornerShape(16.dp))
                        .background(Color.White)
                        .padding(16.dp),
                ) {
                    Text(
                        text = "Edit your tag",
                        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("Tag") },
                        placeholder = { Text("Write tag...") },
                        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
                                teg.title = text
                                coroutineScope.launch {
                                    tagViewModel.updateTag(teg)
                                }
                            },
                            modifier = Modifier
                                .padding(start = 100.dp)
                                .fillMaxWidth(0.5f)
                                .height(40.dp)
                        ) {
                            Text(text = "Edit", fontSize = 20.sp)
                        }
                    }
                }
            },
        )
    }

    Box(
        modifier = modifier
            .requiredWidth(width = 365.dp)
            .requiredHeight(height = 58.dp)
    ) {
        Box(
            modifier = Modifier
                .requiredWidth(width = 365.dp)
                .requiredHeight(height = 58.dp)
                .clip(shape = RoundedCornerShape(15.dp))
                .background(color = Color(0xFFDBDBF1))
                .clickable { onTagClick() }
        )

        Icon(
            painter = painterResource(id = R.drawable.iconhashtag1),
            tint = Color(0xff423a99),
            contentDescription = "icon \"hashtag 1\"",
            modifier = Modifier

                .offset(
                    x = 20.dp,
                    y = 14.dp
                )
                .requiredWidth(width = 31.dp)
                .requiredHeight(height = 29.dp)
        )
        Text(
            text = "${teg.title}",
            color = Color(0xff423a99),
            textAlign = TextAlign.Center,
            style = TextStyle(
                fontSize = 24.sp,
                fontWeight = FontWeight.Bold
            ),
            modifier = Modifier

                .offset(
                    x = 64.dp,
                    y = 14.dp
                )
        )
        if (getUser?.id == teg.userId) {
            Icon(
                modifier = Modifier
                    .requiredHeight(40.dp)
                    .offset(
                        x = 290.dp,
                        y = 9.dp
                    )
                    .clickable {
                        openDialog = true
                    },
                imageVector = Icons.Filled.Edit,
                contentDescription = "Update Icon",
                tint = Color(0xff423a99)
            )
            Icon(
                modifier = Modifier
                    .requiredHeight(40.dp)
                    .offset(
                        x = 320.dp,
                        y = 9.dp
                    )
                    .clickable {
                        coroutineScope.launch {
                            tagViewModel.deleteTag(teg)
                        }
                        navController.navigate(BottomBarScreen.Categories.route)
                    },
                imageVector = Icons.Filled.Close,
                contentDescription = "Delete Icon",
                tint = Color(0xff423a99)
            )
        }
    }
}

