package com.example.mystictale.Screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
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.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
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.paint
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.example.mystictale.R
import com.example.mystictale.ViewModels.AuthViewModel
import com.example.mystictale.models.Users
import com.example.mystictale.resources.components.Loading
import com.example.mystictale.resources.components.ProfileTextField
import com.example.mystictale.ui.theme.Grey
import com.example.mystictale.ui.theme.OpenSans

@Composable
fun Profile(navController: NavController) {
    val viewModel = AuthViewModel()
    val user by viewModel.user
    val focusManager = LocalFocusManager.current
    val keyboardController = LocalSoftwareKeyboardController.current
    val isEnabled = remember { mutableStateOf(false) }
    val flagName = remember { mutableStateOf(false) }
    val flag = remember { mutableStateOf(true) }
    if (flag.value) {
        viewModel.selectProfile()
        flag.value = false
    }
    if (user.isNotEmpty()) {
        val name = remember {
            mutableStateOf(user.last().name)
        }

        val date_of_birth = remember {
            mutableStateOf(user.last().date_of_birth)
        }
        val gender = remember {
            mutableStateOf("")
        }
        if(user.last().id_gender == 1){
            gender.value = "Женский"
        }else{
            gender.value = "Мужской"
        }
        Column(
            Modifier
                .fillMaxSize()
                .paint(
                    painterResource(id = R.drawable.background),
                    contentScale = ContentScale.FillBounds
                )
                .pointerInput(Unit) {
                    detectTapGestures(onTap = {
                        keyboardController?.hide()
                        focusManager.clearFocus()
                    })
                }
                .padding(10.dp)
                .padding(top = 30.dp),
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Профиль",
                fontFamily = OpenSans,
                fontWeight = FontWeight.Bold,
                fontSize = 40.sp,
                color = Color.White
            )
            Spacer(modifier = Modifier.height(20.dp))
            Image(painter = painterResource(id = R.drawable.girl), contentDescription = "")
            Spacer(modifier = Modifier.height(20.dp))
            Column(
                Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 20.dp)
            ) {
                Text(
                    text = "Имя",
                    fontFamily = OpenSans,
                    fontWeight = FontWeight.Bold,
                    fontSize = 14.sp,
                    color = Grey
                )
                Spacer(modifier = Modifier.height(10.dp))
                ProfileTextField(
                    value = name.value,
                    onValue = { name.value = it }, placeholder = "Имя",
                    keyboardType = KeyboardType.Text,
                    isEnable = isEnabled.value,
                    flag = flagName.value
                )
            }
            Spacer(modifier = Modifier.height(15.dp))
            Column(
                Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 20.dp)
            ) {
                Text(
                    text = "Дата рождения",
                    fontFamily = OpenSans,
                    fontWeight = FontWeight.Bold,
                    fontSize = 14.sp,
                    color = Grey
                )
                Spacer(modifier = Modifier.height(10.dp))
                ProfileTextField(
                    value = date_of_birth.value,
                    onValue = { date_of_birth.value = it },
                    placeholder = "Дата рождения",
                    keyboardType = KeyboardType.Text,
                    isEnable = isEnabled.value,
                    flag = flagName.value
                )
            }
            Spacer(modifier = Modifier.height(15.dp))
            Column(
                Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 20.dp)
            ) {
                Text(
                    text = "Пол",
                    fontFamily = OpenSans,
                    fontWeight = FontWeight.Bold,
                    fontSize = 14.sp,
                    color = Grey
                )
                Spacer(modifier = Modifier.height(10.dp))
                ProfileTextField(
                    value = gender.value,
                    onValue = { gender.value = it },
                    placeholder = "Дата рождения",
                    keyboardType = KeyboardType.Text,
                    isEnable = isEnabled.value,
                    flag = flagName.value
                )
            }
        }
    } else {
        Box(
            Modifier
                .fillMaxSize()
                .paint(
                    painterResource(id = R.drawable.background),
                    contentScale = ContentScale.FillBounds
                )
        ) {
            Loading()
        }
    }

}