package com.vpn.fastestvpnservice.customItems

import android.content.res.Configuration
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectTapGestures
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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
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.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontStyle
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.compose.rememberNavController
import com.vpn.fastestvpnservice.R
import com.vpn.fastestvpnservice.beans.SubscriptionPackageList
import com.vpn.fastestvpnservice.beans.subscriptionPackageList
import java.util.Random

var getPosition: Int = 0

@Composable
fun SubscriptionPackageItem(item: SubscriptionPackageList, position: Int) {

    val context = LocalContext.current
    var selectedPosition by remember { mutableIntStateOf(0) }
    var items by remember { mutableStateOf(item) }
    var isSelected by remember { mutableStateOf(true) }
    val random = kotlin.random.Random.nextInt(1, 999)

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 16.dp)
            .padding(bottom = 0.dp)
            .height(112.dp)
            .border(
                border = BorderStroke(
                    2.dp,
                    if (item.isSelected.value) colorResource(id = R.color.blue_text)
                    else Color.White
                ),
                shape = RoundedCornerShape(24.dp)
            )
            .background(
                shape = RoundedCornerShape(24.dp),
                color = Color.White
            )
            .pointerInput(Unit) {
                detectTapGestures {

//                    if (selectedPosition == position)
//                    {
//                        Log.d("test_plan", "True : selectedPosition = $selectedPosition position = $position" )
//                    }
//                    else {
//                        Log.d("test_plan", "False : selectedPosition = $selectedPosition position = $position" )
//                        selectedPosition = position
//                        Log.d("test_plan", "Update  selectedPosition: $selectedPosition" )
//                        subscriptionPackageList.forEachIndexed { index, subscriptionPackageList ->
//                            if (selectedPosition == index) {
//                                Log.d("test_plan", "True Inside : selectedPosition = $selectedPosition index = $index" )
//                                subscriptionPackageList.isSelected.value = true
//                            }
//                            else {
//                                Log.d("test_plan", "False Inside : selectedPosition = $selectedPosition index = $index" )
//                                subscriptionPackageList.isSelected.value = false
//                            }
//                        }
//                    }

                    if (!item.isSelected.value) {
//                        Log.d("test_plan", "False : selectedPosition = $selectedPosition position = $position" )
                        selectedPosition = position
                        subscriptionPackageList.forEachIndexed { index, subscriptionPackageList ->
//                            if (selectedPosition == index) {
//                                Log.d("test_plan", "True Inside : selectedPosition = $selectedPosition index = $index" )
//                                subscriptionPackageList.isSelected.value = true
//                            }
//                            else {
//                                Log.d("test_plan", "False Inside : selectedPosition = $selectedPosition index = $index" )
//                                subscriptionPackageList.isSelected.value = false
//                            }
//                            Log.d("test_plan", "Update ${subscriptionPackageList.packageDuration} $index $position" )

                            subscriptionPackageList.isSelected.value = index == position
                            getPosition = position
//                            Log.d("test_plan", "Update ${subscriptionPackageList.packageDuration} ${subscriptionPackageList.isSelected.value}" )

                        }
                    }


                }
            },
    )
    {
        Column(
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.Start,
            modifier = Modifier
                .fillMaxSize()
                .padding(20.dp)
                .background(Color.White)
        ) {
            Row(
                verticalAlignment = Alignment.Top,
                horizontalArrangement = Arrangement.SpaceBetween,
                modifier = Modifier
                    .fillMaxWidth()
            ) {
                Text(text = item.packageDuration,
                    style = TextStyle(
                        color = if (item.isSelected.value) colorResource(
                            id = R.color.blue_text) else colorResource(
                            id = R.color.dark_blue_gray_text),
                        fontSize = 18.sp,
                        fontStyle = FontStyle.Normal,
                        fontWeight = FontWeight.Medium
                    ),
                    maxLines = 1,
                    modifier = Modifier
                        .padding(start = 0.dp, end = 0.dp)
                        .alpha(if (item.isSelected.value) 1F else 0.6F)
                )
                Text(text = item.price,
                    style = TextStyle(
                        color = if (item.isSelected.value) colorResource(
                            id = R.color.light_blue_2) else colorResource(
                            id = R.color.text_color_dark_gray),
                        fontSize = 20.sp,
                        fontStyle = FontStyle.Normal,
                        fontWeight = FontWeight.SemiBold,
                        textAlign = TextAlign.Right
                    ),
                    maxLines = 1,
                    modifier = Modifier
                        .padding(start = 0.dp, end = 0.dp)
                        .alpha(if (item.isSelected.value) 1F else 0.6F)
                )
            }
            Text(text = item.planType,
                style = TextStyle(
                    color = colorResource(id = R.color.dark_blue_gray_text),
                    fontSize = 14.sp,
                    fontStyle = FontStyle.Normal,
                    fontWeight = FontWeight.Medium,
                ),
                maxLines = 1,
                modifier = Modifier
                    .padding(top = 6.dp, end = 0.dp)
            )
            Text(text = item.desc,
                style = TextStyle(
                    color = colorResource(id = R.color.dark_blue_gray_text),
                    fontSize = 14.sp,
                    fontStyle = FontStyle.Normal,
                ),
                maxLines = 1,
                modifier = Modifier
                    .padding(top = 8.dp, end = 0.dp),
                lineHeight = 30.sp
            )
        }
    }
}

fun getSelectedPosition(): Int {
    return getPosition
}

@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun SubscriptionPackageItemPreview() {
    SubscriptionPackageItem(subscriptionPackageList[1], 0)
}