package com.vpn.fastestvpnservice.screens.accountScreensAll

import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.LocalOverscrollConfiguration
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.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.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
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.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.NavHostController
import androidx.navigation.compose.rememberNavController
import com.vpn.fastestvpnservice.R
import com.vpn.fastestvpnservice.beans.subscriptionList
import com.vpn.fastestvpnservice.beans.subscriptionPackageList
import com.vpn.fastestvpnservice.customItems.SubscriptionDataItem
import com.vpn.fastestvpnservice.customItems.SubscriptionPackageItem
import com.vpn.fastestvpnservice.customItems.getSelectedPosition
import com.vpn.fastestvpnservice.sealedClass.Screen

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun SubscriptionScreen(navHostController: NavHostController) {
    var selectedPosition: Int = 0
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
    ) {
        HeaderRowSS(navHostController = navHostController)

        Column(
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.Start,
            modifier = Modifier
                .padding(
                    top = 120.dp,
                    start = 16.dp, end = 16.dp,
                    bottom = 120.dp
                )
                .fillMaxSize()
                .background(Color.Transparent)
        ) {
            Text(text = "Upgrade Your Account",
                style = TextStyle(
                    color = MaterialTheme.colorScheme.primary,
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Medium
                )
            )

            Column(
                verticalArrangement = Arrangement.Top,
                horizontalAlignment = Alignment.Start,
                modifier = Modifier.padding(top = 30.dp)
            ) {
                CompositionLocalProvider(
                    LocalOverscrollConfiguration provides null
                ) {
                    LazyColumn() {
                        items(items = subscriptionList) {item ->
                            SubscriptionDataItem(item = item)
                        }
                        item { Spacer(modifier = Modifier.height(20.dp)) }
                        itemsIndexed(items = subscriptionPackageList) {position, plan ->
                            SubscriptionPackageItem(item = plan, position)
                        }
                        item { Spacer(modifier = Modifier.height(20.dp)) }
                    }
                }
            }
        }

        val context = LocalContext.current
        Button(
            onClick = {
                Log.d("test_button", "onClick")
                selectedPosition = getSelectedPosition()
                Toast
                    .makeText(
                        context, subscriptionPackageList[selectedPosition].packageDuration, Toast.LENGTH_SHORT
                    )
                    .show()
            },
            modifier = Modifier
                .padding(
                    top = 29.dp,
                    start = 16.dp, end = 16.dp,
                    bottom = 34.dp
                )
                .align(Alignment.BottomCenter)
                .background(colorResource(id = R.color.transparent))
                .fillMaxWidth()
                .height(60.dp),
            shape = RoundedCornerShape(15.dp),
//            border = BorderStroke(25.dp, colorResource(id = R.color.black)),
            colors = ButtonDefaults.buttonColors(
                contentColor = colorResource(id = R.color.white),
                containerColor = colorResource(id = R.color.blue_text),
            ),
        )
        {
            Text(text = "Subscribe",
                style = TextStyle(
                    color = colorResource(id = R.color.white),
                    textAlign = TextAlign.Center,
                    fontSize = 20.sp,
                    fontWeight = FontWeight.SemiBold,
                    fontStyle = FontStyle.Normal
                )
            )
        }
    }
}

@Composable
fun BoxScope.HeaderRowSS(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 = "Subscription",
            style = TextStyle(
                fontSize = 18.sp,
                color = MaterialTheme.colorScheme.primary,
                textAlign = TextAlign.Center,
                fontWeight = FontWeight.Medium
            ),
            modifier = Modifier.fillMaxHeight()
        )
    }
}

@Preview
@Composable
fun SubscriptionScreenPreview() {
    SubscriptionScreen(rememberNavController())
}