package com.vpn.fastestvpnservice.screens.settingsScreenAll

import android.Manifest
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.selection.selectable
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.RadioButtonDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.vpn.fastestvpnservice.R
import com.vpn.fastestvpnservice.beans.SelectApplicationEntry
import com.vpn.fastestvpnservice.constants.splitList
import com.vpn.fastestvpnservice.customItems.SplitTunnelingItem
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
import java.util.SortedSet
import java.util.TreeSet

@Composable
fun SplitTunneling(navHostController: NavHostController) {
    Log.d("test_split_compose", "SplitTunneling Screen")

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
    ) {
        HeaderRowST(navHostController = navHostController)

        Column(
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.Start,
            modifier = Modifier
                .padding(top = 120.dp, start = 0.dp, end = 0.dp)
                .fillMaxSize()
                .background(Color.Transparent)
        ) {
            var isExpanded by remember { mutableStateOf(true) }
            val icon = if (isExpanded) R.drawable.dragarrow3x
                        else R.drawable.downarrow3x
//            val rotationState by animateFloatAsState(
//                targetValue = if (isExpanded) 180f else 0f,
//                label = if (isExpanded) "Show Less" else "Show More")

            Row(
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .padding(start = 16.dp, end = 28.dp)
                    .fillMaxWidth()
            ) {
                Text(text = "While VPN is Connected",
                    style = MaterialTheme.typography.titleSmall,
                    color = MaterialTheme.colorScheme.primary
                    )
//                IconButton(onClick = {
//                    isExpanded = !isExpanded
//                },
//                    modifier = Modifier
//                        .background(Color.Transparent)
////                        .rotate(rotationState)
//                        .size(12.dp, 7.dp)
//                ) {
//                    Icon(
//                        painter = painterResource(
//                            id = icon),
//                        contentDescription = "Arrow-DropDown",
//                        tint = MaterialTheme.colorScheme.primary
//                    )
//
//                }
            }

//            if (isExpanded) { }
            ShowRadioButtons()

        }
    }
}

@Composable
fun ColumnScope.ShowRadioButtons() {
    Log.d("test_split_compose", "ShowRadioButtons()")
    val context = LocalContext.current
    val prefHelper = BasePreferenceHelper(context = context)
    var apps = ArrayList<SelectApplicationEntry>()

    var selectedList by remember { mutableStateOf(prefHelper.getSplitPosition()) }

    Spacer(modifier = Modifier.height(15.dp))

    splitList.forEachIndexed { index, list ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(Color.Transparent)
                    .padding(top = 0.dp)
                    .selectable(
                        selected = selectedList == list,
                        onClick = {
                            selectedList = list
                            prefHelper.saveSplitPosition(list)
                            apps = getInstalledApps(context)

                        },
                    )
//                                    .indication(
//                                        indication = null,
//                                        interactionSource = remember {
//                                            MutableInteractionSource()
//                                        }
//                                    )
                ,
                verticalAlignment = Alignment.CenterVertically
            ) {
                RadioButton(selected = list == selectedList,
                    onClick = {
                        selectedList = list
                        prefHelper.saveSplitPosition(list)
                        apps = getInstalledApps(context)
                    },
                    colors = RadioButtonDefaults.colors(
                        selectedColor = colorResource(
                            id = R.color.radio_button_blue),
                        unselectedColor = colorResource(
                            id = R.color.gray_icon),)
                )
                Text(
                    text = list,
                    modifier = Modifier.padding(start = 12.dp),
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.primary
                )
            }
        }

    Surface(
        modifier = Modifier
            .padding(start = 16.dp, end = 16.dp, top = 20.dp)
            .height(1.dp)
            .fillMaxWidth()
            .alpha(0.6F)
        ,
        color = colorResource(id = R.color.gray_icon)
    ) {}

    apps = getInstalledApps(context)
    Log.d("test_split_compose", "apps called : ${apps.size}")

    val type = object : TypeToken<SortedSet<String?>?>() {}.type
    val appsAllow = Gson().fromJson<SortedSet<String>>(
        prefHelper.getSplitTunneledApps(), type
    )

    appsAllow?.let {
        for (app in appsAllow) {
            Log.d("test_split_compose", "apps allow : $app")
        }
    }

    if (selectedList == splitList[1]) {
        ShowSplitTunnelList(apps)
    }
    else if (selectedList == splitList[2]) {
        ShowSplitTunnelList(apps)
    }
}

fun getInstalledApps(context: Context): ArrayList<SelectApplicationEntry> {
    val prefHelper = BasePreferenceHelper(context)
    val apps: ArrayList<SelectApplicationEntry> = ArrayList()
//    val apps2: ArrayList<SelectApplicationEntry> = ArrayList()
    val seen: SortedSet<String> = TreeSet()
    var selectedAppsList = ArrayList<String>()
    var selectedAppsListNoVpn = ArrayList<String>()

    if (!prefHelper.getSplitTunneledApps().equals("")) {
        val type = object : TypeToken<ArrayList<String>>() {}.type
        selectedAppsList = Gson().fromJson(prefHelper.getSplitTunneledApps(), type)
    }

    if (!prefHelper.getSplitTunneledAppsNoVpn().equals("")) {
        val type = object : TypeToken<ArrayList<String>>() {}.type
        selectedAppsListNoVpn = Gson().fromJson(prefHelper.getSplitTunneledAppsNoVpn(), type)
    }

    val mPackageManager: PackageManager = context.packageManager

    for (info in mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA)) { /* skip apps that can't access the network anyway */

        if (info.flags and ApplicationInfo.FLAG_UPDATED_SYSTEM_APP != 0) {
            Log.d("--package names !0", "" + info.packageName)
            if (mPackageManager.checkPermission(
                    Manifest.permission.INTERNET, info.packageName
                ) == PackageManager.PERMISSION_GRANTED
            ) {
                if (info.packageName == context.packageName) continue
                val entry = SelectApplicationEntry(mPackageManager, info, false)

                prefHelper.getSplitPosition().let {
                    when (it){
                        splitList[1] -> {
                            if (!prefHelper.getSplitTunneledApps().equals("")) {
                                entry.setSelected(mutableStateOf(selectedAppsList.contains(info.packageName))) //= selectedAppsList!!.contains(info.packageName)
                            }
                        }
                        splitList[2] -> {
                            if (!prefHelper.getSplitTunneledAppsNoVpn().equals("")) {
                                entry.setSelected(mutableStateOf(selectedAppsListNoVpn.contains(info.packageName))) //= selectedAppsList!!.contains(info.packageName)
                            }
                        }
                    }
                }

                Log.d("test_splittunnel_data", "${entry.getInfo()?.name}")

                Log.d("entry split", entry.toString())

                apps.add(entry)
//                apps2.add(entry)
                seen.add(info.packageName)

            }
        }
        if (info.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
            Log.d("--package names 0 ", "" + info.packageName)
            if (mPackageManager.checkPermission(
                    Manifest.permission.INTERNET, info.packageName
                ) == PackageManager.PERMISSION_GRANTED
            ) {
                if (info.packageName == context.packageName) continue
                val entry = SelectApplicationEntry(mPackageManager, info, false)

                prefHelper.getSplitPosition().let {
                    when (it){
                        splitList[1] -> {
                            if (!prefHelper.getSplitTunneledApps().equals("")) {
                                entry.setSelected(mutableStateOf(selectedAppsList.contains(info.packageName))) //= selectedAppsList!!.contains(info.packageName)
                            }
                        }
                        splitList[2] -> {
                            if (!prefHelper.getSplitTunneledAppsNoVpn().equals("")) {
                                entry.setSelected(mutableStateOf(selectedAppsListNoVpn.contains(info.packageName))) //= selectedAppsList!!.contains(info.packageName)
                            }
                        }
                    }
                }

                apps.add(entry)
//                apps2.add(entry)
                seen.add(info.packageName)
            }

        }
    }

    apps.sort()
//    apps2.sort()

    apps.forEach {
        Log.d("test_split_apps", "$it ${it.getIcon()}")
    }
//    apps2.forEach {
//        Log.d("test_split_apps 2", "$$it ${it.getIcon()}")
//    }

    return apps
}

@Composable
fun ColumnScope.ShowSplitTunnelList(list: List<SelectApplicationEntry>) {

    Box(modifier = Modifier
        .padding(top = 5.dp)
        .fillMaxSize()
        .background(Color.Transparent)
    ) {
        LazyColumn() {
            itemsIndexed(items = list) {position, app ->
                SplitTunnelingItem(app, list, position)
            }
        }
    }
}

@Composable
fun BoxScope.HeaderRowST(navHostController: NavHostController) {
    IconButton(
        onClick = {
            navHostController.popBackStack()
//                    navHostController.navigate(BottomBarScreen.Help.route)
        },
        modifier = Modifier
            .align(Alignment.TopStart)
            .padding(top = 50.dp)
            .padding(start = 16.dp)
            .size(30.dp, 32.dp)
    ) {
        Icon(
            painter = painterResource(id = R.drawable.backarrow3x),
            contentDescription = "Arrow-Back",
            tint = MaterialTheme.colorScheme.primary,
            modifier = Modifier.size(18.dp, 12.dp)
        )
    }
    Surface(
        modifier = Modifier
            .padding(top = 50.dp)
            .height(32.dp)
            .align(Alignment.TopCenter)
            .padding(5.dp),
        color = colorResource(id = R.color.transparent)
    ) {
        Text(text = "Split Tunneling",
            color = MaterialTheme.colorScheme.primary,
            style = MaterialTheme.typography.bodyMedium,
            modifier = Modifier.fillMaxHeight()

        )
    }
}

@Preview
@Composable
fun SplitTunnelingPreview() {
    SplitTunneling(rememberNavController())
}

