package com.jihe.punchnet.screen

import android.app.Activity.RESULT_CANCELED
import android.app.Activity.RESULT_OK
import android.content.Context
import android.content.Intent
import android.net.VpnService
import android.os.Build
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
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.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.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.jihe.punchnet.PunchnetService
import com.jihe.punchnet.PunchnetServiceArgument
import com.jihe.punchnet.R
import com.jihe.punchnet.RouteInfo
import com.jihe.punchnet.data.RouteViewModel
import com.jihe.punchnet.data.ServiceViewModel

@Composable
fun HomeScreen(
    serviceViewModel: ServiceViewModel,
    routeViewModel: RouteViewModel,
    // started: MutableState<Boolean>,
    modifier: Modifier = Modifier,
) {
    val context = LocalContext.current
    val allroutes = routeViewModel.allRoutes.collectAsState(emptyList())
    val vpnLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartActivityForResult()
    ) {result ->
        when(result.resultCode) {
            RESULT_OK -> {
                // serviceViewModel.startService(context)
                // serviceViewModel.setVPNPermission(true)
                serviceViewModel.startService(context, allroutes.value.toTypedArray())
            }
            RESULT_CANCELED -> {
                Toast.makeText(context, "vpn permission denied", Toast.LENGTH_SHORT).show()
            }
        }
    }

    CustomHeaderScreen(
        "",
        onBack = null,
        onMenu = {
            Icon(
                Icons.Default.Settings,
                contentDescription = "settings",
                modifier = Modifier.fillMaxHeight()
            )
            // NavListMenu(navController, dbdao)
            /*
            navController.navigate(
                Screen.ActionDetail.route.replace(
                    "{action_name}",
                    ""
                )
            )
             */

        }
    ) {
        Column(
            modifier = Modifier.fillMaxWidth()
                .padding(top=50.dp),
            horizontalAlignment = Alignment.CenterHorizontally,

            ) {
            Image(
                painter = painterResource(R.drawable.punchnet_log),
                contentDescription = "logo",
                modifier = Modifier.size(150.dp)
            )

            Text(
                "Connecting the Infinite",
                fontSize = 30.sp,
                style = MaterialTheme.typography.titleLarge,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(top=24.dp)
            )

            Text(
                "Welcome to PunchNet",
                style = MaterialTheme.typography.titleSmall,
                modifier = Modifier.padding(top=8.dp)
            )

            Button(
                onClick = {
                    if (serviceViewModel.isRunning.value) {
                        // if is running, should stop service
                        serviceViewModel.stopVpnService(context)
                    } else {
                        val intent = VpnService.prepare(context)
                        if (intent != null) {
                            vpnLauncher.launch(intent)
                        } else {
                            serviceViewModel.startService(context, allroutes.value.toTypedArray())
                        }
                    }
                    // started.value = !started.value
                },
                shape = RoundedCornerShape(10.dp),
                modifier = Modifier.padding(top=48.dp)
                    .width(120.dp)
                    .height(40.dp)
            ) {
                Text(
                    if (serviceViewModel.isRunning.value) {
                        "停止"
                    } else {
                        "启动"
                    }
                )
            }
        }
        // BasicCardList(dbdao = dbdao, navController)
    }
}

