package com.vpn.fastestvpnservice.screens

import android.content.res.Configuration
import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
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.layout.width
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.ContentAlpha
import androidx.compose.material.LocalContentColor
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.vpn.fastestvpnservice.R
import com.vpn.fastestvpnservice.navigation.BottomBarNavGraph
import com.vpn.fastestvpnservice.sealedClass.BottomBarScreen

@Composable
fun BottomBarMainScreen(navController: NavHostController) {
    val navController1 = rememberNavController()
    var isBottomBarVisible by remember { mutableStateOf(true) }

    val screens = listOf(
        BottomBarScreen.Home,
        BottomBarScreen.Settings,
        BottomBarScreen.Help,
        BottomBarScreen.Account
    )

    navController1.addOnDestinationChangedListener { _,_,_ ->
        isBottomBarVisible = navController1.currentDestination?.route in listOf(
            screens[0].route, screens[1].route, screens[2].route, screens[3].route)
    }

    Scaffold (
        bottomBar = {
                    if (isBottomBarVisible) {
                        BottomBar(navController1, screens)
                    }
        },
        content = { padding ->
            Box(modifier = Modifier.padding(padding)) {
                BottomBarNavGraph(navHostController = navController1,
                    settingsNavHostController = navController)
            }
        }
    )
}

@Composable
fun BottomBar(navHostController: NavHostController, screens: List<BottomBarScreen>) {


    val navBackStackEntry = navHostController.currentBackStackEntryAsState()
    val currentDestination = navBackStackEntry.value?.destination

    Log.d("currentRoute -> BB ", currentDestination.toString())


    BottomNavigation (
        backgroundColor = MaterialTheme.colorScheme.onPrimary,
        contentColor = Color.Gray,
        modifier = Modifier.height(84.dp)
    ) {
        screens.forEach { screen ->
            AddItem(
                screen = screen,
                currentDestination = currentDestination,
                navHostController = navHostController
            )
        }
    }
}

@Composable
fun RowScope.AddItem(
    screen: BottomBarScreen,
    currentDestination: NavDestination?,
    navHostController: NavHostController
) {
    val route = currentRoute(navController = navHostController)
    Log.d("currentRoute -> ", route.toString())
    BottomNavigationItem(
        label = {
            if (currentDestination?.route == screen.route) {
                Text(text = screen.title,
                    color = colorResource(id = R.color.blue_text),
                    )
            }
            else {
                Text(text = screen.title,
                    color = Color.Gray)
            }

        },
        alwaysShowLabel = true,
        selected = currentDestination?.hierarchy?.any{
            it.route == screen.route
        } == true,
        onClick = {

            /* First time currentDestination is different when on BottomBar Item */

            if (currentDestination?.route != screen.route) {
                Log.d("test_bottom_navbar",
                    "currentDestination -> " + currentDestination?.route
                            + "Screen Clicked -> " + screen.route
                )

                navHostController.navigate(screen.route) {
                    popUpTo(
                        navHostController.graph
                            .findStartDestination().id) {
                        saveState = true
                    }
                    launchSingleTop = true
                }
            }
        },
        icon = {
            if (currentDestination?.route == screen.route) {
                Icon(
                    painter = painterResource(id = screen.icon),
                    contentDescription = "Navigation Icon",
                    tint = colorResource(id = R.color.dark_blue_icon),
                    modifier = Modifier
                        .size(45.dp, 39.dp)
                        .padding(top = 9.dp, bottom = 7.dp)

                )
            }
            else {
                Icon(
                    painter = painterResource(id = screen.icon),
                    contentDescription = "Navigation Icon",
                    tint = colorResource(id = R.color.gray_icon),
                    modifier = Modifier
                        .size(45.dp, 39.dp)
                        .padding(top = 9.dp, bottom = 7.dp)                )
            }

        },
        unselectedContentColor = LocalContentColor.current.copy(
            alpha = ContentAlpha.disabled
        )
    )
}

@Composable
fun currentRoute(navController: NavHostController): String? {
    val navBackStackEntry = navController.currentBackStackEntryAsState()
    return navBackStackEntry.value?.arguments?.getString("KEY_ROUTE")
}

@Composable
@Preview
fun BottomNavBarPreview() {
    BottomBarMainScreen(rememberNavController())
}

@Composable
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
fun BottomNavBarPreviewDark() {
    BottomBarMainScreen(rememberNavController())
}