package com.lanyun.wanglaotou

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navArgument
import androidx.navigation.navigation
import com.lanyun.common.baseDate.Nav
import com.lanyun.common.widget.TwoBackFinish
import com.lanyun.wanglaotou.composable.*
import com.lanyun.wanglaotou.viewModel.MainViewModel
import com.lanyun.wanglaotou.widget.StatsBarUtil


@Composable
fun NavigationHost(
    mainViewModel: MainViewModel,
    navHostController: NavHostController,
    paddingValues: PaddingValues = PaddingValues(0.dp),
    onFinish: () -> Unit
) {
    val mViewModel  = mainViewModel
    val context = LocalContext.current

    NavHost(
        navHostController,
        startDestination = KeyNavigationRoute.MAIN.route,
        //todo 只有主界面需要这个padding
        modifier = Modifier.padding(paddingValues),
        builder = {

            //主页面
            navigation(
                route = KeyNavigationRoute.MAIN.route,
                startDestination = Nav.BottomNavScreen.HomeScreen.route
            ) {

                //首页
                composable(Nav.BottomNavScreen.HomeScreen.route) {

                    //系统颜色的状态栏
                    StatsBarUtil().StatsBarColor(false)

                    HomeCompose(mViewModel,navHostController)

                    //点击两次返回才关闭app
                    BackHandler {
                        TwoBackFinish().execute(context, onFinish)
                    }
                }

            }
            //节点界面
            composable(route = KeyNavigationRoute.NODE.route) {
                //系统颜色的状态栏
                StatsBarUtil().StatsBarColor(false)

                NodeCompose(navHostController)

                BackHandler { navHostController.navigateUp() }
            }


            //购买界面
            composable(route = KeyNavigationRoute.BUY.route) {
                //系统颜色的状态栏
                StatsBarUtil().StatsBarColor(false)

                GuestCompose(navHostController)

                BackHandler { navHostController.navigateUp() }
            }
            //设置页面
            composable(route = KeyNavigationRoute.SETTING.route) {
                //系统颜色的状态栏
                StatsBarUtil().StatsBarColor(false)

                SettingCompose(navHostController)

                BackHandler { navHostController.navigateUp() }
            }
            //登录页面
            composable(route = KeyNavigationRoute.LOGIN.route) {
                //透明/沉浸式状态栏
                StatsBarUtil().StatsBarColor(true)

                LoginCompose(navHostController,mainViewModel)

                BackHandler { navHostController.navigateUp() }
            }

            //注册页面
            composable(route = KeyNavigationRoute.REGISTER.route) {
                //透明/沉浸式状态栏
                StatsBarUtil().StatsBarColor(true)

                RegisterCompose(navHostController)

                BackHandler { navHostController.navigateUp() }
            }

            //选择app代理
            composable(route = KeyNavigationRoute.PERAPPPROXY.route) {
                //透明/沉浸式状态栏
                StatsBarUtil().StatsBarColor(true)

                PerAppCompose(navHostController,mainViewModel)

                BackHandler { navHostController.navigateUp() }
            }



            //H5页面
            composable(
                route = "${KeyNavigationRoute.WEBVIEW.route}?url={url}&title={title}", arguments = listOf(
                    navArgument("url") { defaultValue = "" }, navArgument("title"){defaultValue = ""}
                )
            ) { backStackEntry ->

                //系统颜色的状态栏
                StatsBarUtil().StatsBarColor(false)

                WebViewCompose(
                    navHostController,
                    backStackEntry.arguments?.getString("url") ?: "",
                    backStackEntry.arguments?.getString("title") ?: "",
                    mainViewModel,
                )

                BackHandler { navHostController.navigateUp() }
            }




        }
    )
}


/**
 * 页面跳转关键类
 */
enum class KeyNavigationRoute(
    val route: String
) {
    //主页面
    MAIN("main"),

    //登录页面
    LOGIN("login"),

    //注册页面
    REGISTER("register"),

    //设置
    SETTING("setting"),

    PERAPPPROXY("perappproxy"),
    //购买
    BUY("buy"),
    //节点界面
    NODE("node"),

    //H5
    WEBVIEW("webview"),

    //搜索
    SEARCH("search"),
}



