package com.fastest.pass.dashboard.presentation.ui.component

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
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.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
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.graphics.ColorFilter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentContainerView
import androidx.fragment.app.FragmentManager
import com.fastest.pass.R
import com.fastest.pass.account.presentation.ui.fragment.AccountFragment
import com.fastest.pass.browse.presentation.ui.fragment.NewItemBrowseFragment
import com.fastest.pass.dashboard.domain.model.Screen
import com.fastest.pass.home.presentation.ui.fragment.BrowseFragment
import com.fastest.pass.home.presentation.ui.fragment.HomeFragment
import com.fastest.pass.security.presentation.ui.fragment.SecurityFragment
import com.fastest.pass.ui.theme.Gray_Splash

@Composable
fun BottomTab() {
    val activity = LocalContext.current as FragmentActivity
    var selectedTab = remember { mutableStateOf<Screen>(Screen.Home) }
    var isBottomBarVisible by remember { mutableStateOf(true) }

    Scaffold(
        bottomBar = {
            if (isBottomBarVisible) {
                BottomNavigationBar(
                    selectedTab = selectedTab.value,
                    onTabSelected = { screen ->
                        selectedTab.value = screen
                    }
                )
            }
        }
    ) { paddingValues ->
        Box(modifier = Modifier
            .fillMaxSize()
            .background(colorResource(id = R.color.blue_login))
            .padding(paddingValues)
        ) {
            // Add a container for hosting fragments
            AndroidView(
                factory = { context ->
                    FragmentContainerView(context).apply {
                        id = R.id.fragment_container_view
                    }
                },
                modifier = Modifier.fillMaxSize()
            )

//            DisposableEffect(key1 = activity) {
//                val listener = FragmentManager.OnBackStackChangedListener {
//                    val currentFragment = activity.supportFragmentManager.fragments.lastOrNull()
//
//                    Log.d("test_navbar", "frag = ${currentFragment?.id} $currentFragment")
//
//                    isBottomBarVisible = when (currentFragment) {
//                        is HomeFragment -> true
//                        is BrowseFragment -> true
//                        is AccountFragment -> true
//                        is NewItemBrowseFragment -> false
//                        else -> false
//                    }
//                }
//
//                activity.supportFragmentManager.addOnBackStackChangedListener(listener)
//                onDispose {
//                    activity.supportFragmentManager.removeOnBackStackChangedListener(listener)
//                }
//            }

            // Handle fragment replacement when the selected tab changes
            LaunchedEffect(selectedTab.value) {
                loadFragment(activity, selectedTab.value)
            }
        }
    }
}

@Composable
fun BottomNavigationBar(selectedTab: Screen, onTabSelected: (Screen) -> Unit) {
//    val items = listOf(Screen.Home, Screen.Browse, Screen.Security, Screen.Account)
    val items = listOf(Screen.Home, Screen.Browse, Screen.Account)

    BottomNavigation(
        backgroundColor = colorResource(id = R.color.blue_login),
        contentColor = colorResource(id = R.color.white),
        modifier = Modifier
            .fillMaxWidth()
            .height(70.dp),
    ) {
        items.forEach { screen ->
            BottomNavigationItem(
                modifier = Modifier
                    .align(Alignment.CenterVertically),
                icon = {
                    Image(
                    painter = painterResource(id = screen.icon),
                    contentDescription = null,
                    colorFilter = ColorFilter.tint(if (selectedTab == screen) colorResource(id = R.color.white)
                    else colorResource(id = R.color.gray_text)),
                ) },
                label = {
                    Text(
                        screen.title,
                    color =  if (selectedTab == screen) colorResource(id = R.color.white)
                    else colorResource(id = R.color.gray_text)
                    )
                        },
                selected = selectedTab == screen,
                onClick = { onTabSelected(screen) },
                selectedContentColor = Color.Transparent, // You can customize selected item color
                unselectedContentColor = Color.Transparent
            )
        }
    }
}

fun loadFragment(activity: FragmentActivity, screen: Screen) {
    val fragment = when (screen) {
        Screen.Home -> HomeFragment()// Replace with your actual fragments
        Screen.Browse -> BrowseFragment()
        Screen.Security -> SecurityFragment()
        Screen.Account -> AccountFragment()
    }

    activity.supportFragmentManager.beginTransaction()
        .replace(R.id.fragment_container_view, fragment)
        .addToBackStack(screen.route)
        .commit()
}


