package com.laseroptek.raman.navigation.graphs

import android.content.res.Configuration
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.laseroptek.raman.data.datasource.db.RamanDatabase
import com.laseroptek.raman.data.source.db.RamanDatabaseService
import com.laseroptek.raman.navigation.Routes
import com.laseroptek.raman.repository.DatabaseRepository
import com.laseroptek.raman.repository.PreferenceRepository
import com.laseroptek.raman.repository.SerialPortRepository
import com.laseroptek.raman.ui.screens.config.ConfigScreen
import com.laseroptek.raman.ui.screens.engineer.EngineerScreen
import com.laseroptek.raman.ui.screens.home.HomeScreen
import com.laseroptek.raman.ui.screens.info.InfoScreen
import com.laseroptek.raman.ui.screens.lock.LockScreen
import com.laseroptek.raman.ui.screens.main.MainViewModel
import com.laseroptek.raman.utils.DefaultDispatcherProvider

@Composable
fun MainNavGraph(
    mainNavController: NavHostController = rememberNavController(),
    innerPadding: PaddingValues = PaddingValues(),
    mainViewModel: MainViewModel = hiltViewModel<MainViewModel>(),
) {
    NavHost(
        navController = mainNavController,
        startDestination = Routes.Home.route,
    ) {
        composable(route = Routes.Home.route) {
            HomeScreen(
                paddingValues = innerPadding,
                mainViewModel = mainViewModel,
            )
        }
        composable(route = Routes.Info.route) {
            InfoScreen(
                paddingValues = innerPadding,
                mainViewModel = mainViewModel,
            )

        }
        composable(route = Routes.Config.route) {
            ConfigScreen(
                mainNavController = mainNavController,
                paddingValues = innerPadding,
                mainViewModel = mainViewModel,
            )
        }
        composable(route = Routes.Lock.route) {
            LockScreen(
                mainNavController = mainNavController
            )
        }
        composable(route = Routes.Engineer.route) {
            EngineerScreen(
                mainNavController = mainNavController,
                paddingValues = innerPadding,
                mainViewModel = mainViewModel,
            )
        }
    }
}

@Preview(
    apiLevel = 34,
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false,
    device = "spec:width=1920dp,height=1080dp,dpi=150,isRound=false,chinSize=0dp,orientation=landscape"
)
@Composable
fun PreviewMainNavGraph(
    mainViewModel:MainViewModel = MainViewModel(
        preferenceRepository = PreferenceRepository(LocalContext.current),
        serialPortRepository = SerialPortRepository.createWithFakeRepository(),
        databaseRepository = DatabaseRepository(RamanDatabaseService(RamanDatabase.getInstance(LocalContext.current))),
        dispatcherProvider = DefaultDispatcherProvider(),
        applicationContext = LocalContext.current
    )
) {
    MainNavGraph(
        mainViewModel = mainViewModel,
    )
}
