package com.jihe.punchnet.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.jihe.punchnet.R
import com.jihe.punchnet.RouteInfo
import com.jihe.punchnet.data.ButtonRepository
import com.jihe.punchnet.data.ButtonState
import com.jihe.punchnet.data.ButtonViewModel
import com.jihe.punchnet.data.RouteViewModel
import com.jihe.punchnet.data.ServiceViewModel

@Composable
fun AppNavHost2(
    buttonViewModel: ButtonViewModel,
    serviceViewModel: ServiceViewModel,
    routeViewModel: RouteViewModel = viewModel(),
    navController: NavHostController,
    paddingValues: PaddingValues
) {

    val context = androidx.compose.ui.platform.LocalContext.current
    val token = com.jihe.punchnet.helper.getPreferenceString(context, com.jihe.punchnet.helper.PreferenceName.PreferenceToken)
    val startDest = if (token.isNullOrEmpty()) Screen.LoginScreen.route else Screen.MainScreen.route

    NavHost(
        navController = navController,
        startDestination = startDest,
        modifier = Modifier.padding(paddingValues)
    ) {
        composable(Screen.LoginScreen.route) {
            LoginScreen(navController)
        }
        composable(Screen.MainScreen.route) {
            HomeScreen(buttonViewModel, serviceViewModel, routeViewModel, navController)
        }
        composable(Screen.DeviceScreen.route) {
            CustomHeaderScreen("设备") {
                DeviceScreen(navController)
            }
        }
        composable(Screen.AppAuthScreen.route) {
            CustomHeaderScreen("应用授权") {
                AppAuthScreen(navController)
            }
        }
        composable(Screen.SettingsScreen.route) {
            CustomHeaderScreen("设置") {
                SettingsScreen(ButtonRepository.buttonState.collectAsState().value != ButtonState.ButtonStarted, routeViewModel)
            }
        }
        composable(
            route = Screen.WebViewScreen.route,
            arguments = listOf(navArgument("url") { type = androidx.navigation.NavType.StringType })
        ) { backStackEntry ->
            val url = backStackEntry.arguments?.getString("url") ?: ""
            WebViewScreen(
                url = url,
                onBack = { navController.popBackStack() }
            )
        }
    }
}
