package com.vpn.fastestvpnservice.customItems

import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.shape.RoundedCornerShape
import androidx.compose.material.Checkbox
import androidx.compose.material.CheckboxDefaults
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.vpn.fastestvpnservice.R
import com.vpn.fastestvpnservice.beans.SplitTunnelData
import com.vpn.fastestvpnservice.beans.splitTunnelList

@Composable
fun SplitTunnelingItem(app: SplitTunnelData) {
    Surface(
        modifier = Modifier
            .fillMaxWidth()
            .background(
                color = MaterialTheme.colorScheme.background
            )
            .padding(bottom = 1.dp),
        color = if (app.isSelected.value) colorResource(id = R.color.split_background)
                else MaterialTheme.colorScheme.background

    ) {
        val context = LocalContext.current

        Row(
            verticalAlignment = Alignment.Top,
            horizontalArrangement = Arrangement.Start,
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 12.dp, end = 7.dp, top = 12.dp)
                .clickable(
                    indication = null,
                    interactionSource = remember {
                        MutableInteractionSource()
                    },
                ) {
                    app.isSelected.value = !app.isSelected.value
                }
        ) {
            Icon(
                painter = painterResource(id = app.icon),
                contentDescription = "Server Logo",
                tint = Color.Unspecified,
                modifier = Modifier
                    .padding(bottom = 16.dp)
                    .size(40.dp)
            )
            Text(text = app.appName,
                style = TextStyle(
                    fontSize = 14.sp,
                    color = if (app.isSelected.value) colorResource(id = R.color.dark_blue_gray_text)
                            else MaterialTheme.colorScheme.primary
                ),
                modifier = Modifier
                    .padding(start = 12.dp, bottom = 18.dp)
                    .align(Alignment.CenterVertically)
            )
            Spacer(modifier = Modifier.weight(1F))

            var isSelected by rememberSaveable { mutableStateOf(false) }

            Checkbox(
                checked = app.isSelected.value,
                onCheckedChange = {
                    app.isSelected.value = it
                },
                colors = CheckboxDefaults.colors(
                    uncheckedColor = colorResource(id = R.color.switch_gray),
                    checkedColor = colorResource(id = R.color.blue_text),
                ),

                modifier = Modifier
                    .padding(bottom = 18.dp, end = 8.dp)
                    .align(Alignment.CenterVertically)
                    .background(
                        color = if (app.isSelected.value) colorResource(id = R.color.blue_text)
                        else colorResource(id = R.color.switch_gray),
                        shape = RoundedCornerShape(5.dp)
                    )
                    .size(24.dp)
            )
        }
    }
}

@Preview
@Composable
fun SplitTunnelingItemPreview() {
    SplitTunnelingItem(splitTunnelList[0])
}