package com.jihe.punchnet.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
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.foundation.layout.width
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.jihe.punchnet.data.ButtonViewModel
import com.jihe.punchnet.data.RouteViewModel
import com.jihe.punchnet.data.ServiceViewModel

sealed class Screen(val route: String) {
    object MainScreen: Screen("home") // 首页
    object DeviceScreen: Screen("devices") // 设备 (取代原先的 Route)
    object AppAuthScreen: Screen("appAuth") // 应用授权 (取代原先的 Profile)
    object SettingsScreen: Screen("settings") // 设置
    object WebViewScreen: Screen("webview/{url}") {
        fun createRoute(url: String) = "webview/${android.net.Uri.encode(url)}"
    }
    object LoginScreen: Screen("login")
}

val bottomNavItems = listOf(
    Screen.MainScreen,
    Screen.DeviceScreen,
    Screen.AppAuthScreen,
)

@Composable
fun MainApp(
    buttonViewModel: ButtonViewModel = viewModel(),
    serviceViewModel: ServiceViewModel = viewModel(),
    routeModel: RouteViewModel = viewModel(),
    navController: NavHostController,
) {

    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute by remember { derivedStateOf { navBackStackEntry?.destination?.route } }
    val showBottomBar = currentRoute != Screen.LoginScreen.route

    Scaffold(
        bottomBar = {
            if (showBottomBar) {
                AppBottomNavigation(navController = navController)
            }
        }
    ){ paddingValues ->
        /*
        Image(
            painter = painterResource(R.drawable.punchnet_log),
            contentDescription = null,
            modifier = Modifier.padding(paddingValues).size(200.dp)
        )
         */
        AppNavHost2(buttonViewModel, serviceViewModel, routeModel, navController, paddingValues)
    }
}

@Composable
fun CustomHeaderScreen(
    text: String,
    onBack: @Composable (() -> Unit)? = null,
    onMenu: @Composable (() -> Unit)? = null,
    body: @Composable () -> Unit,
) {
    Column(
        modifier = Modifier.fillMaxWidth(),
    ) {
        Box (
            modifier = Modifier
                .fillMaxWidth()
                .height(56.dp)
                .background(MaterialTheme.colorScheme.background),
            //.padding(horizontal = 16.dp),
            contentAlignment = Alignment.CenterStart
        ) {
            Row (
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween,
                modifier = Modifier.fillMaxWidth()
            ) {

                Box(
                    modifier = Modifier.fillMaxHeight()
                        .padding(horizontal = 8.dp)
                        .width(35.dp)
                ) {
                    if (onBack != null) {
                        onBack()
                    }
                }


                Text(
                    text,
                    // color = MaterialTheme.colorScheme.onPrimary,
                    style = MaterialTheme.typography.titleLarge,
                )

                Box(
                    modifier = Modifier.fillMaxHeight()
                        .padding(horizontal = 8.dp)
                        .width(35.dp)
                ) {
                    if (onMenu != null) {
                        onMenu()
                    }
                }

            }
        }

        Box(modifier = Modifier.fillMaxSize()) {
            body()
        }
    }
}
