package com.tapjoy.easyapp.navigation

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.tapjoy.easyapp.ConfigScreen
import com.tapjoy.easyapp.ConfigViewModel
import com.tapjoy.easyapp.MainScreen
import com.tapjoy.easyapp.MainViewModel
import com.tapjoy.easyapp.OfferwallDiscoverScreen
import com.tapjoy.easyapp.OfferwallDiscoverViewModel
import com.tapjoy.easyapp.UnityAdsScreen
import com.tapjoy.easyapp.UnityAdsViewModel
import com.tapjoy.easyapp.UserScreen
import com.tapjoy.easyapp.UserViewModel
import com.tapjoy.easyapp.theme.GreyColor

@Composable
fun Navigation(
    mainViewModel: MainViewModel,
    userViewModel: UserViewModel,
    owdViewModel: OfferwallDiscoverViewModel,
    configViewModel: ConfigViewModel,
    unityAdsViewModel: UnityAdsViewModel,
) {
    val navController = rememberNavController()
    Scaffold(
        bottomBar = { BottomNavigation(navController = navController) },
    ) { padding: PaddingValues ->
        Column(modifier = Modifier.padding(padding)) {
            NavigationGraph(navController = navController, mainViewModel, userViewModel, owdViewModel, configViewModel, unityAdsViewModel)
        }
    }
}

@Composable
fun BottomNavigation(navController: NavController) {
    val items = listOf(
        BottomNavItem.Main,
        BottomNavItem.User,
        BottomNavItem.OfferwallDiscover,
        BottomNavItem.Config,
        BottomNavItem.UnityAds,
    )
    NavigationBar(containerColor = GreyColor) {
        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val currentRoute = navBackStackEntry?.destination?.route
        items.forEach { item ->
            NavigationBarItem(
                icon = { Icon(painterResource(id = item.icon), contentDescription = item.title) },
                label = { Text(text = item.title, fontSize = 12.sp) },
                colors = NavigationBarItemDefaults.colors().copy(
                    selectedIconColor = Color.Black,
                    unselectedIconColor = Color.Black.copy(0.4f),
                ),
                alwaysShowLabel = true,
                selected = currentRoute == item.screen_route,
                onClick = {
                    navController.navigate(item.screen_route) {
                        navController.graph.startDestinationRoute?.let { screen_route ->
                            popUpTo(screen_route) {
                                saveState = true
                            }
                        }
                        launchSingleTop = true
                        restoreState = true
                    }
                },
            )
        }
    }
}

@Composable
fun NavigationGraph(
    navController: NavHostController,
    mainViewModel: MainViewModel,
    userViewModel: UserViewModel,
    owdViewModel: OfferwallDiscoverViewModel,
    configViewModel: ConfigViewModel,
    unityAdsViewModel: UnityAdsViewModel,
) {
    NavHost(navController, startDestination = BottomNavItem.Main.screen_route) {
        composable(BottomNavItem.Main.screen_route) {
            MainScreen(mainViewModel)
        }
        composable(BottomNavItem.User.screen_route) {
            UserScreen(userViewModel)
        }
        composable(BottomNavItem.OfferwallDiscover.screen_route) {
            OfferwallDiscoverScreen(owdViewModel)
        }
        composable(BottomNavItem.Config.screen_route) {
            ConfigScreen(configViewModel)
        }
        composable(BottomNavItem.UnityAds.screen_route) {
            UnityAdsScreen(unityAdsViewModel)
        }
    }
}
