package com.vpn.fastestvpnservice.screens.accountScreensAll

import android.content.res.Configuration
import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.LocalOverscrollConfiguration
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
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.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
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.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.vpn.fastestvpnservice.R
import com.vpn.fastestvpnservice.beans.favList
import com.vpn.fastestvpnservice.customItems.FavoriteServerItem
import com.vpn.fastestvpnservice.customItems.ServerItem
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.onServer
import com.vpn.fastestvpnservice.sealedClass.BottomBarScreen
import com.vpn.fastestvpnservice.sealedClass.Screen
import com.vpn.fastestvpnservice.viewmodels.HomeViewModel
import com.vpn.fastestvpnservice.viewmodels.ServerListViewModel

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun FavoriteServers(navHostController: NavHostController) {
    val context = LocalContext.current
    val basePreferenceHelper = BasePreferenceHelper(context)
    val serverListViewModel: ServerListViewModel = viewModel{
        ServerListViewModel(context)
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
    )
    {
        HeaderRowFS(navHostController = navHostController)

        Column(
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.Start,
            modifier = Modifier
                .padding(
                    top = 120.dp,
                    start = 16.dp, end = 16.dp
                )
                .fillMaxSize()
                .background(Color.Transparent)
        ) {
            val favoriteServers = serverListViewModel.liveDataGetFavList.observeAsState().value

            CompositionLocalProvider(
                LocalOverscrollConfiguration provides null
            ) {
                LazyColumn {
//                    val favoriteServers = basePreferenceHelper.getServerData().get(0)
//                    val filterFavoriteServers = favoriteServers.servers?.let {
//                        serverListViewModel.filterServersByISO(
//                            it
//                        )
//                    }


//                    favoriteServers?.forEachIndexed { index, server ->
//                        Log.d("filterFavoriteServers", "${server.country} ${server.server_name}")
//                    }

                    favoriteServers?.let {
                        items(items = favoriteServers) {server ->
                            FavoriteServerItem(server = server, navHostController)
                        }
                    }

                }
            }
        }
    }
}

@Composable
fun BoxScope.HeaderRowFS(
    navHostController: NavHostController) {
    IconButton(
        onClick = {
            navHostController.popBackStack()

//            navHostController.popBackStack(BottomBarScreen.Home.route, false)
//            navHostController.navigate(BottomBarScreen.Home.route)
        },
        modifier = Modifier
            .align(Alignment.TopStart)
            .padding(top = 50.dp)
            .padding(start = 16.dp)
            .size(30.dp, 32.dp)
    ) {
        Icon(
            painter = painterResource(id = R.drawable.backarrow3x),
            contentDescription = "Arrow-Back",
            tint = MaterialTheme.colorScheme.primary,
            modifier = Modifier.size(18.dp, 12.dp)
        )
    }
    Surface(
        modifier = Modifier
            .padding(top = 50.dp)
            .height(32.dp)
            .align(Alignment.TopCenter)
            .padding(5.dp),
        color = colorResource(id = R.color.transparent)
    ) {
        Text(text = "Favorite Servers",
            color = MaterialTheme.colorScheme.primary,
            style = MaterialTheme.typography.bodyMedium,
            modifier = Modifier.fillMaxHeight()

        )
    }
}


@Preview
@Composable
fun FavoriteServersPreview() {
    FavoriteServers(rememberNavController())
}

@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun FavoriteServersPreviewDark() {
    FavoriteServers(rememberNavController())
}