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.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
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.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
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.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 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.Favourites
import com.example.iplace.model.Place
import com.example.iplace.model.Users
import com.example.iplace.ui.theme.brown_contrast
import kotlinx.coroutines.launch

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


    var placeList by remember { mutableStateOf<List<Place>>(emptyList()) }
    var categoryList by remember { mutableStateOf<List<Category>>(emptyList()) }
    var favouritesList by remember { mutableStateOf<List<Favourites>>(emptyList()) }
    var user by remember { mutableStateOf<Users?>(null) }

    LaunchedEffect(key1 = true) {
        viewModel.getUserById()
        user = viewModel.user.value
        placeList = viewModel.getPlaceList()
        categoryList = viewModel.getCategoryList()
        favouritesList = viewModel.getFavouritesList()
    }

    val selectedCategoryId = remember { mutableStateOf<Int?>(null) }
    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
                ) {
                    FilterDropdown(selectedCategoryId)
                    Button(onClick = {
                        selectedCategoryId.value = null
                    }) {
                        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(getFilteredPlaceListF(placeList, selectedCategoryId,favouritesList,user)) { place ->
                        PlaceCard(place = place, navController, categoryList = categoryList)
                    }
                }
            }
        }
    }

}



fun getFilteredPlaceListF(
    placeList: List<Place>, selectedCategoryId: MutableState<Int?>,
    favouritesList: List<Favourites>,
    user: Users?
): List<Place> {
    return if (selectedCategoryId.value != null) {
        placeList.filter { place ->
            place.id_categoryFK == selectedCategoryId.value &&
                    favouritesList.any { it.id_placeFK == place.id_place && it.id_userFK == user?.id_uuid ?: "" }
        }
    } else {
        placeList.filter { place ->
            favouritesList.any { it.id_placeFK == place.id_place && it.id_userFK == user?.id_uuid ?: "" }
        }
    }
}





