package com.example.wabi.view

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.navigation.compose.rememberNavController
import com.example.wabi.domain.navigation.Navigation
import com.example.wabi.domain.navigation.Routes
import com.example.wabi.domain.repository.PrefManager
import com.example.wabi.ui.theme.WabiTheme
import com.example.wabi.view.common_elements.bars.bottom.BottomBar
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        installSplashScreen()

        setContent {
            val context = LocalContext.current
            PrefManager.initPrefManager(context)

            val navHostController = rememberNavController()
            val visibleBB = remember { mutableStateOf(false) }
            val visibleTB = remember { mutableStateOf(false) }

            PrefManager.currentUser = "на логин"
            PrefManager.userTheme = 0

            WabiTheme(themeState = PrefManager.userTheme) {
                Scaffold(modifier = Modifier.fillMaxSize(),
                    containerColor = WabiTheme.colors.backgroundColor,
                    bottomBar = {
                        if (visibleBB.value) {
                            BottomBar(navController = navHostController, context = context)
                        }
                    }) { paddingValues ->
                    Box(
                        modifier = Modifier.padding(paddingValues)
                    ) {
                        Navigation(
                            controller = navHostController,
                            visibleBB = visibleBB,
                            visibleTB = visibleTB
                        )

                        if (PrefManager.onboard) {
                            PrefManager.onboard = false
                            visibleBB.value = false
                            navHostController.navigate(Routes.ONBOARD)
                        } else {
                            visibleBB.value = true
                            navHostController.navigate(Routes.BASKET)
                        }
                    }
                }
            }
        }
    }
}
