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.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
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.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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberImagePainter
import com.example.iplace.R
import com.example.iplace.main.viewModel.MainViewModel
import com.example.iplace.ui.theme.beige_white
import com.example.iplace.ui.theme.brown_contrast

import androidx.compose.foundation.background
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.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MenuV(navController: NavHostController) {
    val viewModel = MainViewModel()
    var showDrawer by remember { mutableStateOf(false) }
    val headlinesOswald = FontFamily( //выбор шрифта
        Font(resId = R.font.headlines_oswald)
    )
    val basicNunitosans = FontFamily(
        Font(resId = R.font.basic_nunitosans)
    )
    // Шторка (Drawer)
    AnimatedVisibility(
        visible = showDrawer,
        enter = slideInHorizontally { it },
        exit = slideOutHorizontally { -it }
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.White.copy(alpha = 0.8f)) // Полупрозрачный фон
                .clickable { showDrawer = false } // Закрытие при клике вне шторки
        ) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .background(Color.White)
                    .padding(16.dp)
            ) {
//        IconButton(onClick = { showDrawer = false }) {
//            Icon(Icons.Filled.Menu, contentDescription = "Закрыть")
//        }
                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
                    )
                }
                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
                    )
                }
                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
                    )
                }
                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
                    )
                }
                Button(
                    onClick = {  },
                    modifier = Modifier.width(290.dp).height(50.dp),
                    colors = ButtonDefaults.buttonColors(brown_contrast)
                ) {
                    Text(
                        text = "Ещё",
                        fontSize = 24.sp,
                        fontFamily = basicNunitosans
                    )
                }

            }
        }
    }


    Scaffold(
        topBar = {
            TopAppBar(
                title = {
                    // Кнопка меню
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        IconButton(onClick = { showDrawer  = true} ) {
                            Icon(Icons.Filled.Menu, contentDescription = "Меню")
                        }
                        OutlinedTextField(
                            value = "",
                            onValueChange = {},
                            label = { Text("Поиск") },
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(horizontal = 16.dp)
                                .weight(1f)
                                .clip(RoundedCornerShape(20.dp))
                        )// Заглушка для поиска
//                        Text(text = "Поиск", modifier = Modifier.weight(1f))
                        IconButton(onClick = { navController.navigate("profile") }) {
                            Icon(Icons.Filled.Person, contentDescription = "Профиль")
                        }
                    }
                }
//                ,backgroundColor = Color.White,
//                elevation = 0.dp
            )
        }
//        ,
//        drawerContent = {
//            DrawerContent(showDrawer, navController)
//        },
//        drawerGesturesEnabled = showDrawer
    ) { 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
            )

            // 4 блока данных
            LazyColumn {
                items(listOf(1, 2, 3, 4)) { item ->
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(230.dp)
                            .padding(16.dp)
                            .background(Color.LightGray)
                            .clickable{navController.navigate("placeV")}
                    ) {
                        Column(
                            modifier = Modifier
                                .align(Alignment.BottomStart) // Выравниваем Column по левому нижнему углу
                        ) {
                            Text(text = "Название $item", modifier = Modifier)
                            Text(text = "Категория $item", modifier = Modifier)
                        }

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

@Composable
fun DrawerContent(showDrawer: Boolean, navController: NavHostController) {
    val headlinesOswald = FontFamily( //выбор шрифта
        Font(resId = R.font.headlines_oswald)
    )
    val basicNunitosans = FontFamily(
        Font(resId = R.font.basic_nunitosans)
    )
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
            .padding(16.dp)
    ) {
//        IconButton(onClick = { showDrawer = false }) {
//            Icon(Icons.Filled.Menu, contentDescription = "Закрыть")
//        }
        Button(
            onClick = { navController.navigate("policyConditionality") },
            modifier = Modifier.width(290.dp).height(50.dp),
            colors = ButtonDefaults.buttonColors(brown_contrast)
        ) {
            Text(
                text = "Профиль",
                fontSize = 24.sp,
                fontFamily = basicNunitosans
            )
        }
        Button(
            onClick = { navController.navigate("policyConditionality") },
            modifier = Modifier.width(290.dp).height(50.dp),
            colors = ButtonDefaults.buttonColors(brown_contrast)
        ) {
            Text(
                text = "Избранное",
                fontSize = 24.sp,
                fontFamily = basicNunitosans
            )
        }
        Button(
            onClick = { navController.navigate("policyConditionality") },
            modifier = Modifier.width(290.dp).height(50.dp),
            colors = ButtonDefaults.buttonColors(brown_contrast)
        ) {
            Text(
                text = "Добавить место",
                fontSize = 24.sp,
                fontFamily = basicNunitosans
            )
        }
        Button(
            onClick = { navController.navigate("policyConditionality") },
            modifier = Modifier.width(290.dp).height(50.dp),
            colors = ButtonDefaults.buttonColors(brown_contrast)
        ) {
            Text(
                text = "Меню",
                fontSize = 24.sp,
                fontFamily = basicNunitosans
            )
        }
        Button(
            onClick = { navController.navigate("policyConditionality") },
            modifier = Modifier.width(290.dp).height(50.dp),
            colors = ButtonDefaults.buttonColors(brown_contrast)
        ) {
            Text(
                text = "Ещё",
                fontSize = 24.sp,
                fontFamily = basicNunitosans
            )
        }

    }
}