package com.example.wabi.view

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
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.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
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.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavHostController
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.domain.repository.UserShareDate
import com.example.wabi.domain.system.ConnectivityObserver
import com.example.wabi.domain.system.NetworkConnectivityObserver
import com.example.wabi.ui.theme.WabiTheme
import com.example.wabi.view.components.bars.bottom.BottomBar
import com.example.wabi.view.screeens.connection.ConncetionDestinations
import com.example.wabi.view.screeens.connection.NoInternet
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    private lateinit var connectivityObserver: ConnectivityObserver

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

        connectivityObserver = NetworkConnectivityObserver(applicationContext)
        connectivityObserver.observe().onEach {
            Log.d("connection status", "status is $it")
        }.launchIn(lifecycleScope)

        installSplashScreen()

        enableEdgeToEdge()

        setContent {
            UserShareDate.context = LocalContext.current
            val navHostController = rememberNavController()
            val isBottomBar = remember { mutableStateOf(true) }
            val isTopBar = remember { mutableStateOf(false) }

            PrefManager.initPrefManager(UserShareDate.context!!)

            WabiTheme(themeState = PrefManager.userTheme) {

                val connectionStatus by connectivityObserver.observe().collectAsState(
                    initial = ConncetionDestinations.internetConnection
                )

                Scaffold(modifier = Modifier.fillMaxSize(),
                    containerColor = WabiTheme.colors.backgroundColor,
                    bottomBar = {
                        if (isBottomBar.value) {
                            BottomBar(
                                navHostController = navHostController
                            )
                        }
                    },
                    topBar = {}) { innerPadding ->

                    if (connectionStatus) {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .padding(innerPadding)
                        ) {
                            InitApp(navHostController, isBottomBar)
                        }
                    } else {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .padding(innerPadding)
                        ) {
                            NoInternet()
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun InitApp(
    navHostController: NavHostController,
    isBottomBar: MutableState<Boolean>,
    vm: MainActivityViewModel = hiltViewModel()
) {
    vm.checkUserLogging()
    Navigation(
        navHostController, start = if (PrefManager.onboard) {
            Routes.ONBOARD
        } else {
            UserShareDate.currentRoute
        }, isBottomBar
    )
}