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

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
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.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.ui.draw.clip
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.unit.dp
import androidx.compose.ui.unit.sp
import com.example.iplace.R
import com.example.iplace.main.viewModel.MainViewModel
import com.example.iplace.ui.theme.brown_contrast

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
//noinspection UsingMaterialAndMaterial3Libraries
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Scaffold
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.example.iplace.main.viewModel.AuthData
import com.example.iplace.model.Place


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MenuV(navController: NavHostController){
    val viewModel = MainViewModel()

    var placeList by remember { mutableStateOf<List<Place>>(emptyList()) }
//    LaunchedEffect(key1 = true) {
//        viewModel.getPlaceListRealTime()
//            .collect {list -> placeList = list}
//    }
    LaunchedEffect(key1 = true) {
        placeList = viewModel.getPlaceList()

    }

    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()
    )


    val DrawerState = rememberDrawerState(DrawerValue.Closed)
    ModalNavigationDrawer(
        drawerState = DrawerState,
        modifier = Modifier
            .fillMaxWidth(1f)
        ,

        drawerContent = {
            DrawerContent(navController)
        }
    ) {

        Scaffold(
            topBar = {
                TopAppBar(

                    title = {
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceBetween,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            IconButton(onClick = { DrawerValue.Open }) {
                                Icon(Icons.Filled.Menu, contentDescription = "Меню")
                            }

                            OutlinedTextField(
                                value = "",
                                onValueChange = {},
                                label = { Text("Поиск") },
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(horizontal = 16.dp)
                                    .weight(1f)
                                    .clip(RoundedCornerShape(20.dp))
                            )

                            IconButton(onClick = { navController.navigate("profile") }) {
                                Icon(Icons.Filled.Person, contentDescription = "Профиль")
                            }
                        }
                    }

                )
            }

        ) { innerPadding ->
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(innerPadding)
            ) {
                // Кнопка "Фильтр" с выпадающим списком (заглушка)
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Button(onClick = { /* Обработка нажатия на фильтр */ }) {
                        Text("Фильтр")
                    }
                    // Выпадающий список (заглушка)
                    Spacer(modifier = Modifier.width(8.dp))
                    // ...
                    Button(
                        onClick = { navController.navigate("addPlace") },
                        modifier = Modifier
                            .width(40.dp)
                            .height(40.dp),
                        colors = ButtonDefaults.buttonColors(brown_contrast)
                    ) {
                        Text(text = "добавить место")
                    }

                }
                Text(
                    text = "Лучшие места",
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .fillMaxWidth(),
                    textAlign = TextAlign.Center
                )
                LazyColumn(
                    contentPadding = PaddingValues(
                        horizontal = 16.dp,
                        vertical = 10.dp),
                    verticalArrangement = Arrangement.spacedBy(10.dp)
                ){
                    items(placeList) {
                            place ->
                        PlaceCard(place = place, navController

                        )
                    }

                }
            }
        }
    }

}

@Composable
fun PlaceCard(place: Place, navController: NavHostController){
    val viewModel = MainViewModel()
    val placeId = place.id_place
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(300.dp)
            .clip(RoundedCornerShape(20.dp))
            .clickable(onClick = {

                navController.navigate("placeV/$placeId")
            })
        ,
        contentAlignment = Alignment.Center
    ){

        AsyncImage(
            model = place.img_place,
            contentDescription = null,
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.Crop
        )
        Box(

            modifier = Modifier
                .matchParentSize()
                .background(color = Color.LightGray.copy(alpha = 0.2f))
                .padding(16.dp)
        ){
            Column(
                modifier = Modifier
                    .align(Alignment.BottomStart) // Выравниваем Column по левому нижнему углу
            ) {
                Text(
                    text = place.title,
                    fontSize = 18.sp,
                    color = Color.White,

                    )
                Text(
                    text = place.id_categoryFK.toString(),
                    fontSize = 18.sp,
                    color = Color.White,

                    )
            }

            Button(
                onClick = { /* Обработка нажатия на фильтр */ },
                modifier = Modifier
                    .align(Alignment.TopEnd) // Выравниваем Button по правому верхнему углу
                    .width(40.dp)
                    .height(40.dp),
                colors = ButtonDefaults.buttonColors(brown_contrast)
            ) {
                Text(text = "☆")
            }
        }



    }

}


@Composable
fun DrawerContent(navController: NavHostController) {
    val headlinesOswald = FontFamily( //выбор шрифта
        Font(resId = R.font.headlines_oswald)
    )
    val basicNunitosans = FontFamily(
        Font(resId = R.font.basic_nunitosans)
    )
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .height(170.dp)
        ,
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Spacer(modifier = Modifier.height(10.dp))
        Button(
            onClick = { navController.navigate("profile") },
            modifier = Modifier.width(290.dp).height(50.dp),
            colors = ButtonDefaults.buttonColors(brown_contrast)
        ) {
            Text(
                text = "Профиль",
                fontSize = 24.sp,
                fontFamily = basicNunitosans
            )
        }
        Spacer(modifier = Modifier.height(10.dp))
        Button(
            onClick = { navController.navigate("favouritesV") },
            modifier = Modifier.width(290.dp).height(50.dp),
            colors = ButtonDefaults.buttonColors(brown_contrast)
        ) {
            Text(
                text = "Избранное",
                fontSize = 24.sp,
                fontFamily = basicNunitosans
            )
        }
        Spacer(modifier = Modifier.height(10.dp))
        Button(
            onClick = { navController.navigate("addPlace") },
            modifier = Modifier.width(290.dp).height(50.dp),
            colors = ButtonDefaults.buttonColors(brown_contrast)
        ) {
            Text(
                text = "Добавить место",
                fontSize = 24.sp,
                fontFamily = basicNunitosans
            )
        }
        Spacer(modifier = Modifier.height(10.dp))
        Button(
            onClick = { navController.navigate("menuV") },
            modifier = Modifier.width(290.dp).height(50.dp),
            colors = ButtonDefaults.buttonColors(brown_contrast)
        ) {
            Text(
                text = "Меню",
                fontSize = 24.sp,
                fontFamily = basicNunitosans
            )
        }
        Spacer(modifier = Modifier.height(10.dp))
        Button(
            onClick = {  },
            modifier = Modifier.width(290.dp).height(50.dp),
            colors = ButtonDefaults.buttonColors(brown_contrast)
        ) {
            Text(
                text = "Ещё",
                fontSize = 24.sp,
                fontFamily = basicNunitosans
            )
        }
        Spacer(modifier = Modifier.height(10.dp))

    }
}