package com.vpn.fastestvpnservice.screens.accountScreensAll

import android.content.res.Configuration
import android.util.Log
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.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.viewmodels.ServerListViewModel

@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)
        ) {
            LazyColumn() {
                val favoriteServers = basePreferenceHelper.getServerData().get(0)
                val filterFavoriteServers = favoriteServers.servers?.let {
                    serverListViewModel.filterServersByISO(
                        it
                    )
                }

                filterFavoriteServers?.forEachIndexed { index, server ->
                    Log.d("filterFavoriteServers", "${server.country} ${server.server_name}")
                }
                items(items = filterFavoriteServers!!) {server ->
                    FavoriteServerItem(server = server)
                }
            }
        }
    }
}

@Composable
fun BoxScope.HeaderRowFS(
    navHostController: NavHostController) {
    IconButton(
        onClick = {
            navHostController.popBackStack()
//                    navHostController.navigate(BottomBarScreen.Help.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",
            style = TextStyle(
                fontSize = 18.sp,
                color = MaterialTheme.colorScheme.primary,
                textAlign = TextAlign.Center,
                fontWeight = FontWeight.Medium
            ),
            modifier = Modifier.fillMaxHeight()

        )
    }
}


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

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