package com.example.triphelper.view.ProfileScreen

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.fillMaxHeight
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.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import coil.compose.AsyncImagePainter
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.example.testirovanye_supabase.R
import com.example.triphelper.view.navigation.NavigationRoutes
import com.example.triphelper.view.style.StyleMainFone
import com.example.triphelper.view.theme.SeoulBold

@Composable
fun ProfileUser(
    navController: NavController, iconsProfileIsActivity: MutableState<Boolean>, viewModel: ProfileViewModel = hiltViewModel()) {
    iconsProfileIsActivity.value = true
    val users by viewModel.users.collectAsState(initial = emptyList())
    val titleCountry by viewModel.titleCountry.collectAsState(initial = null)
    val email by viewModel.userEmail.collectAsState(initial = null)
    var imageState: AsyncImagePainter.State

    StyleMainFone()
    LaunchedEffect(key1 = viewModel.navigationTo) {
        viewModel.navigationTo.collect { destination ->
            destination?.let {
                navController.navigate(destination)
            }
        }

    }
    viewModel.GetUser()
    if (users.isNotEmpty()) {
        users.firstOrNull().let { item ->
            viewModel.getTitleCountry(item?.country)
            if (item?.avatar == null) {
                imageState = rememberAsyncImagePainter(
                    model = ImageRequest.Builder(LocalContext.current).data(R.drawable.no_avatar)
                        .size(coil.size.Size.ORIGINAL).build()
                ).state
            } else {
                imageState = rememberAsyncImagePainter(
                    model = ImageRequest.Builder(LocalContext.current).data(item.avatar)
                        .size(coil.size.Size.ORIGINAL).build()
                ).state
            }
            if (imageState is AsyncImagePainter.State.Error) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(200.dp),
                    contentAlignment = Alignment.Center
                ) {
                    CircularProgressIndicator()
                }
            }
            if (imageState is AsyncImagePainter.State.Success) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .fillMaxHeight(0.9f),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Column {
                        imageState.painter?.let {
                            Image(
                                modifier = Modifier
                                    .size(180.dp)
                                    .clip(RoundedCornerShape(20.dp)),
                                painter = it,
                                contentDescription = ""
                            )
                        }
                    }
                    Column(
                        modifier = Modifier.padding(top = 20.dp, bottom = 20.dp)
                    ){
                        if (item != null) {
                            Text(
                                text = item.name,
                                fontSize = 32.sp,
                                fontFamily = SeoulBold,
                                fontWeight = FontWeight.Bold,
                                color = Color(0xFF510B3C)
                            )
                        }
                    }
                    Box(
                        modifier = Modifier
                            .fillMaxWidth(0.8f)
                            .fillMaxHeight(0.65f)
                            .background(Color.White)
                            .border(
                                width = 3.dp,
                                color = Color(0xFF510B3C)
                            ),
                        contentAlignment = Alignment.CenterStart
                    ){
                        Column(
                            modifier = Modifier.padding(start = 20.dp),
                            verticalArrangement = Arrangement.spacedBy(30.dp)
                        ) {
                            Column(
                                verticalArrangement = Arrangement.spacedBy(15.dp)
                            ) {
                                Text(
                                    text = "Почта",
                                    fontSize = 32.sp,
                                    fontFamily = SeoulBold,
                                    fontWeight = FontWeight.Medium,
                                    color = Color(0xFFDC5B6E)
                                )
                                Text(
                                    modifier = Modifier.padding(start = 2.dp),
                                    text = email.toString(),
                                    fontSize = 24.sp,
                                    fontFamily = SeoulBold,
                                    fontWeight = FontWeight.Medium,
                                    color = Color(0xFF510B3C)
                                )
                            }
                            Column(
                                verticalArrangement = Arrangement.spacedBy(15.dp)
                            ) {
                                Text(
                                    text = "Пароль",
                                    fontSize = 32.sp,
                                    fontFamily = SeoulBold,
                                    fontWeight = FontWeight.Medium,
                                    color = Color(0xFFDC5B6E)
                                )
                                Text(
                                    modifier = Modifier.padding(start = 2.dp),
                                    text = "* * * * * * * *",
                                    fontSize = 24.sp,
                                    fontFamily = SeoulBold,
                                    fontWeight = FontWeight.Medium,
                                    color = Color(0xFF510B3C)
                                )
                            }
                            Column(
                                verticalArrangement = Arrangement.spacedBy(15.dp)
                            ) {
                                Text(
                                    text = "Страна",
                                    fontSize = 32.sp,
                                    fontFamily = SeoulBold,
                                    fontWeight = FontWeight.Medium,
                                    color = Color(0xFFDC5B6E)
                                )
                                Text(
                                    modifier = Modifier.padding(start = 2.dp),
                                    text = titleCountry.toString(),
                                    fontSize = 24.sp,
                                    fontFamily = SeoulBold,
                                    fontWeight = FontWeight.Medium,
                                    color = Color(0xFF510B3C)
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}