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

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
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.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
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.rememberCoroutineScope
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.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.example.iplace.R
import com.example.iplace.main.viewModel.MainViewModel
import com.example.iplace.model.Category
import com.example.iplace.model.Place
import com.example.iplace.ui.theme.brown_contrast
import kotlinx.coroutines.launch

@Composable
fun PlaceV(navController: NavHostController, placeId: String) {
    val viewModel = MainViewModel()
    var place by remember { mutableStateOf<Place?>(null) }
    var categoryList by remember { mutableStateOf<List<Category>>(emptyList()) }
    LaunchedEffect (key1 = placeId) {
        place = viewModel.getPlaceById(id = placeId)
        categoryList = viewModel.getCategoryList()
    }

    var isButtonEnabled by remember { mutableStateOf(true) }
    val headlinesOswald = FontFamily( //выбор шрифта
        Font(resId = R.font.headlines_oswald)
    )
    val basicNunitosans = FontFamily(
        Font(resId = R.font.basic_nunitosans)
    )
    val coroutineScope = rememberCoroutineScope()
    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.6f))
        ) {
            AsyncImage(
                model = place?.img_place,
                contentDescription = null,
                modifier = Modifier.fillMaxSize(),
                contentScale = ContentScale.Crop
            )
            // Кнопки назад и автора
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .align(Alignment.TopStart)
                    .padding(16.dp) , // Выравнивание по левому верхнему углу
                horizontalArrangement = Arrangement.Start, // Выравнивание по левому краю
                verticalAlignment = Alignment.CenterVertically,

            ) {
                IconButton(onClick = { navController.navigate("MenuV") }) {
                    Icon(Icons.Filled.ArrowBack, contentDescription = "Назад")
                }
                Spacer(modifier = Modifier.width(170.dp))
                Text(
                    text = place?.author ?:"",
                    fontWeight = FontWeight.Bold,
                    color = Color.White,
                    modifier = Modifier.padding(start = 16.dp).background(Color.Black.copy(alpha = 0.5f)) // Отступ от кнопки "Назад"
                )
            }

            // Блок с Название и Категория
            Column(
                modifier = Modifier
                    .align(Alignment.BottomStart)
            ) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth() // Занимает всю ширину
                        .background(Color.Black.copy(alpha = 0.5f)) // Черный фон с прозрачностью
                        .padding(16.dp) // Отступ внутри бокса
                ) {
                    Column {
                        Text(
                            text = place?.title ?: "",
                            fontWeight = FontWeight.Bold,
                            fontSize = 22.sp,
                            color = Color.White
                        )
                        Spacer(modifier = Modifier.height(8.dp))
                        Text(
                            text = categoryList.find { it.id_category == place?.id_categoryFK ?: "" }?.title ?: "Категория не найдена",
                            fontSize = 14.sp,
                            color = Color.White,
                        )
                    }
                }
            }
            Button(
                onClick = {
                    isButtonEnabled = false
                    coroutineScope.launch {
                        place?.let { viewModel.insertFavourites(idPlace = it.id_place) }
                    }
                },
                modifier = Modifier
                    .align(Alignment.TopEnd)
                    .padding(top = 23.dp)
                    .width(40.dp)
                    .height(40.dp),
                colors = ButtonDefaults.buttonColors(
                    containerColor = if (isButtonEnabled) brown_contrast else Color.Gray,
                    contentColor = Color.Black
                ),
                enabled = isButtonEnabled
            ) {
                Icon(
                    imageVector = Icons.Filled.Add,
                    contentDescription = "Lock icon"
                )
            }
        }

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

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

                OutlinedTextField(
                    value = place?.description ?:"",
                    onValueChange = {},
                    label = { Text("Описание", color = Color.Black) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp)
                        .clip(RoundedCornerShape(20.dp)),
                    textStyle = TextStyle(color = Color.Black)
                )
                Spacer(modifier = Modifier.height(10.dp))

                OutlinedTextField(
                    value = place?.address ?:"",
                    onValueChange = {},
                    label = { Text("Адрес", color = Color.Black) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp)
                        .clip(RoundedCornerShape(20.dp)),
                    textStyle = TextStyle(color = Color.Black)
                )
                Spacer(modifier = Modifier.height(10.dp))

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    OutlinedTextField(
                        value = "",
                        onValueChange = {},
                        label = { Text("Фото") },
                        modifier = Modifier
                            .height(120.dp)
                            .width(200.dp)
                            .padding(horizontal = 16.dp)
                            .clip(RoundedCornerShape(20.dp))
                    )
                    OutlinedTextField(
                        value = "",
                        onValueChange = {},
                        label = { Text("Фото") },
                        modifier = Modifier
                            .height(120.dp)
                            .width(200.dp)
                            .padding(horizontal = 16.dp)
                            .clip(RoundedCornerShape(20.dp))
                    )
                }
                OutlinedTextField(
                    value = "",
                    onValueChange = {},
                    label = { Text("Карта") },
                    modifier = Modifier
                        .height(120.dp)
                        .width(400.dp)
                        .padding(horizontal = 16.dp)
                        .clip(RoundedCornerShape(20.dp))
                )
            }
        }
    }
}

