package com.example.iplace.main.viewModel.dataView

import androidx.compose.foundation.Image
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.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.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.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.setValue
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.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import coil.compose.rememberImagePainter
import com.example.iplace.R
import com.example.iplace.main.viewModel.MainViewModel
import com.example.iplace.model.Place
import com.example.iplace.model.Users
import com.example.iplace.ui.theme.beige_white
import com.example.iplace.ui.theme.brown_contrast



@Composable
fun Profile(navController: NavHostController) {
    val viewModel = MainViewModel()
    var user by remember { mutableStateOf<Users?>(null) }

    LaunchedEffect(Unit) { // Запускаем корутину при первом рендере composable
        viewModel.getUserById() // Вызываем функцию из viewModel
        user = viewModel.user.value // Получаем результат из viewModel
    }


    val headlinesOswald = FontFamily( //выбор шрифта
        Font(resId = R.font.headlines_oswald)
    )
    val basicNunitosans = FontFamily(
        Font(resId = R.font.basic_nunitosans)
    )
    Image(
        painter = painterResource(id = R.drawable.fon),
        contentDescription = "",
        contentScale = ContentScale.FillHeight,
        modifier = Modifier.fillMaxSize()
    )
    Column(
        modifier = Modifier
            .fillMaxSize()
    ) {
        Spacer(modifier = Modifier.height(20.dp))
        // Верхний блок (2/5 размера экрана)
        Box(
            modifier = Modifier
                .weight(2f)
                .background(Color.LightGray.copy(alpha = 0.8f))
                .padding(16.dp),
            contentAlignment = Alignment.TopCenter
        ) {

            // Кнопки назад и настроек
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                IconButton(onClick = { navController.navigate("menuV") }) {
                    Icon(Icons.Filled.ArrowBack, contentDescription = "Назад")
                }
                IconButton(onClick = { navController.navigate("userSettings") }) {
                    Icon(Icons.Filled.Settings, contentDescription = "Настройки")
                }
            }
            Spacer(modifier = Modifier.height(60.dp))

            // Заголовок "Профиль"
            Text(
                text = "Профиль",
                fontWeight = FontWeight.Bold,
                modifier = Modifier
                    .padding(top = 32.dp)
                    .fillMaxWidth(),
                textAlign = TextAlign.Center
            )

            // Блок с фотографией, именем и гендером
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 64.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {

                AsyncImage(
                    model = user?.img_user,
                    contentDescription = null,
                    modifier = Modifier.size(200.dp)
                        .clip(CircleShape)
                        .background(Color.Gray),
                    contentScale = ContentScale.Crop
                )

                // Имя и гендер
                Row(
                    modifier = Modifier
                        .padding(top = 16.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = user?.name ?:""
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    when (user?.id_genderFK?.toString() ?: "") {
                        "1" -> Text(text = "♂")
                        "2" -> Text(text = "♀") // Или любой другой символ, который вы хотите использовать для значения 2
                        else -> {} // Можно добавить обработку других значений или оставить пустым
                    }
                }
            }
        }

        // Остальной контент (3/5 размера экрана)
        Box(
            modifier = Modifier
                .weight(3f)
                .fillMaxSize()
        ) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 64.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {

                Spacer(modifier = Modifier.height(10.dp))

                OutlinedTextField(
                    value = user?.address ?:"",
                    onValueChange = {},
                    label = { Text("Адрес", color = Color.Black) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp)
                        .clip(RoundedCornerShape(20.dp))
                        .clickable(enabled = false) { /* Ничего не делаем при нажатии */ },
                    textStyle = TextStyle(color = Color.Black),
                    enabled = false // Отключаем возможность ввода
                )
                Spacer(modifier = Modifier.height(10.dp))

                OutlinedTextField(
                    value = user?.about_me ?:"",
                    onValueChange = {},
                    label = { Text("О себе", color = Color.Black) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp)
                        .clip(RoundedCornerShape(20.dp))
                        .clickable(enabled = false) { /* Ничего не делаем при нажатии */ },
                    textStyle = TextStyle(color = Color.Black),
                    enabled = false // Отключаем возможность ввода
                )
                Spacer(modifier = Modifier.height(10.dp))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    OutlinedTextField(
                        value = "",
                        onValueChange = {},
                        label = { Text("места, посещенные пользователем", color = Color.Black) },
                        modifier = Modifier
                            .width(200.dp)
                            .padding(horizontal = 16.dp)
                            .clip(RoundedCornerShape(20.dp))
                            .clickable(enabled = false) { /* Ничего не делаем при нажатии */ },
                        textStyle = TextStyle(color = Color.Black),
                        enabled = false // Отключаем возможность ввода
                    )
                    OutlinedTextField(
                        value = "",
                        onValueChange = {},
                        label = { Text("места, отмеченные пользователем", color = Color.Black) },
                        modifier = Modifier
                            .width(200.dp)
                            .padding(horizontal = 16.dp)
                            .clip(RoundedCornerShape(20.dp))
                            .clickable(enabled = false) { /* Ничего не делаем при нажатии */ },
                        textStyle = TextStyle(color = Color.Black),
                        enabled = false // Отключаем возможность ввода
                    )
                }
            }
        }
    }
}