package com.vpn.fastestvpnservice.screens.bottomNavBarScreens

import android.content.res.Configuration
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.LocalOverscrollConfiguration
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.isSystemInDarkTheme
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.RowScope
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.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.ripple.LocalRippleTheme
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardColors
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.paint
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.AnnotatedString
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.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.vpn.fastestvpnservice.R
import com.vpn.fastestvpnservice.sealedClass.Screen
import  androidx.compose.runtime.setValue
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.vpn.fastestvpnservice.beans.isDarkTheme
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
import com.vpn.fastestvpnservice.sealedClass.BottomBarScreen
import com.vpn.fastestvpnservice.ui.theme.FastestVPNTheme

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Home(navHostController: NavHostController) {

    val context = LocalContext.current
    var isConnect by remember { mutableStateOf(false) }
    val basePreferenceHelper = BasePreferenceHelper(context)
    Box(
        modifier = Modifier
            .background(MaterialTheme.colorScheme.background)
            .paint(
                if (isConnect) blueBackground() else pinkBackground(),
                alignment = Alignment.TopStart,
                contentScale = ContentScale.FillWidth,
//                colorFilter = ColorFilter.tint(
//                    color = colorResource(id = R.color.pink_color)),
            )
            .fillMaxSize(),
//        contentAlignment = Alignment.TopCenter
    ) {

        if (!isDarkTheme.value) {
            Image(
                modifier = Modifier
                    .fillMaxWidth()
                    .align(Alignment.TopStart)
                    .padding(top = 75.dp),
                painter = painterResource(id = R.drawable.map_home3x),
                contentDescription = "Home Map",
                contentScale = ContentScale.FillWidth,
            )
        }

        IconButton(
            onClick = {
                isConnect = !isConnect
                val status = basePreferenceHelper.getAdBlockState()
            },
            modifier = Modifier
                .align(Alignment.TopCenter)
                .padding(top = 230.dp)
                .size(234.dp),
        )
        {
            if (isConnect) {
                Image(
                    painter = painterResource(id = R.drawable.iv_connect),
                    contentDescription = "Home Map",
                    contentScale = ContentScale.FillWidth,
                )
            } else {
                Image(
                    painter = painterResource(id = R.drawable.iv_disconnect),
                    contentDescription = "Home Map",
                    contentScale = ContentScale.FillWidth,
                )
            }
        }
        Column(
            verticalArrangement = Arrangement.SpaceEvenly,
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier
                .fillMaxWidth()
                .background(Color.Transparent)
                .align(Alignment.TopStart)
                .padding(top = 75.dp)
                .height(125.dp),
        ) {

            AddText(
                text = "IP 4122.762.21",
                size = 18.sp,
                color = MaterialTheme.colorScheme.primary
            )
            Row(
            ) {
                if (isConnect) {
                    Image(
                        painter = painterResource(id = R.drawable.spainlogo3x),
                        contentDescription = "Spain",
                        modifier = Modifier
                            .padding(end = 6.dp)
                            .size(20.dp)
                    )
                    AddText(
                        text = "Madrid, Spain",
                        size = 16.sp,
                        color = MaterialTheme.colorScheme.primary
                    )
                } else {
                    Image(
                        painter = painterResource(id = R.drawable.pklogo3x),
                        contentDescription = "Pakistan",
                        modifier = Modifier
                            .padding(end = 6.dp)
                            .size(20.dp)
                    )
                    AddText(
                        text = "Karachi, Pakistan",
                        size = 16.sp,
                        color = MaterialTheme.colorScheme.primary
                    )
                }
            }
            if (isConnect) {
                AddText(
                    text = "Connected",
                    size = 18.sp,
                    color = colorResource(id = R.color.light_blue_2)
                )
            } else {
                AddText(
                    text = "Disconnected",
                    size = 18.sp,
                    color = colorResource(id = R.color.maroon_text)
                )
            }
        }

        CompositionLocalProvider(
            LocalOverscrollConfiguration provides null
        ) {
            Column(
                modifier = Modifier
                    .align(Alignment.TopCenter)
                    .padding(top = 490.dp, bottom = 20.dp)
                    .verticalScroll(rememberScrollState())
                    .fillMaxSize()
            ) {
                /* Smart Connect Box*/
                if (isConnect) {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(16.dp)
                            .padding(top = 0.dp)
                            .height(110.dp)
//                    .border(
//                        border = BorderStroke(0.dp, Color.Blue),
//                        shape = RoundedCornerShape(28.dp)
//                    )
                            .background(
                                color = Color.Transparent
                            ),
//            contentAlignment = Alignment.TopCenter,
                    ) {
                        Row(
                            modifier = Modifier
                                .padding(0.dp)
                                .background(Color.Transparent),
                            horizontalArrangement = Arrangement.SpaceAround,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Surface(
                                modifier = Modifier
                                    .weight(1f)
                                    .background(Color.Transparent)
                                    .fillMaxHeight()
                                    .padding(end = 5.dp),
                                shape = RoundedCornerShape(24.dp)

                            ) {
                                Column(
                                    modifier = Modifier
                                        .background(Color.White),
                                    verticalArrangement = Arrangement.SpaceEvenly,
                                    horizontalAlignment = Alignment.CenterHorizontally
                                ) {
                                    Image(
                                        painter = painterResource(id = R.drawable.greenarrow3x),
                                        contentDescription = "Green Arrow",
                                        modifier = Modifier
                                            .size(30.dp)
                                    )
                                    Text(
                                        text = "Download",
                                        style = TextStyle(
                                            color = colorResource(id = R.color.text_color_dark_gray),
                                            fontSize = 14.sp
                                        )
                                    )
                                    Row(
                                        horizontalArrangement = Arrangement.SpaceAround,
                                        verticalAlignment = Alignment.CenterVertically
                                    ) {
                                        Text(
                                            text = "30.68",
                                            style = TextStyle(
                                                color = colorResource(id = R.color.black),
                                                fontSize = 18.sp,
                                                textAlign = TextAlign.Right
                                            )
                                        )
                                        Text(
                                            text = "  MB/S",
                                            style = TextStyle(
                                                color = colorResource(id = R.color.text_color_dark_gray),
                                                fontSize = 12.sp,
                                                fontWeight = FontWeight.Medium
                                            )
                                        )
                                    }
                                }
                            }
                            Surface(
                                modifier = Modifier
                                    .weight(1f)
                                    .background(Color.Transparent)
                                    .fillMaxHeight()
                                    .padding(start = 5.dp),
                                shape = RoundedCornerShape(24.dp)
                            ) {
                                Column(
                                    modifier = Modifier
                                        .background(Color.White),
                                    verticalArrangement = Arrangement.SpaceEvenly,
                                    horizontalAlignment = Alignment.CenterHorizontally
                                ) {
                                    Image(
                                        painter = painterResource(id = R.drawable.yellowarrow3x),
                                        contentDescription = "Green Arrow",
                                        modifier = Modifier
                                            .size(30.dp)
                                    )
                                    Text(
                                        text = "Upload",
                                        style = TextStyle(
                                            color = colorResource(id = R.color.text_color_dark_gray),
                                            fontSize = 14.sp
                                        )
                                    )
                                    Row(
                                        horizontalArrangement = Arrangement.SpaceAround,
                                        verticalAlignment = Alignment.CenterVertically
                                    ) {
                                        Text(
                                            text = "12.11",
                                            style = TextStyle(
                                                color = colorResource(id = R.color.black),
                                                fontSize = 18.sp,
                                                textAlign = TextAlign.Right
                                            )
                                        )
                                        Text(
                                            text = "  MB/S",
                                            style = TextStyle(
                                                color = colorResource(id = R.color.text_color_dark_gray),
                                                fontSize = 12.sp,
                                                fontWeight = FontWeight.Medium
                                            )
                                        )
                                    }
                                }
                            }
                        }
                    }
                }
                else {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(20.dp)
                            .padding(top = 0.dp)
                            .height(114.dp)
                            .border(
                                border = BorderStroke(2.dp, Color.White),
                                shape = RoundedCornerShape(28.dp)
                            )
                            .background(
                                shape = RoundedCornerShape(28.dp),
                                color = Color.White
                            ),
//            contentAlignment = Alignment.TopCenter,
                    ) {
                        AddRowSmart(navHostController)
                        Button(
                            onClick = {
                                Log.d("test_button", "onClick Smart Connect")
                                navHostController.navigate(
                                    BottomBarScreen.Settings.route
                                )
                                BottomBarScreen.Settings.isTrue = true

                            },
                            modifier = Modifier
                                .padding(start = 14.dp, end = 14.dp, bottom = 12.dp, top = 16.dp)
                                .align(Alignment.BottomCenter)
                                .background(colorResource(id = R.color.transparent))
                                .fillMaxWidth()
                                .height(35.dp),
                            shape = RoundedCornerShape(16.dp),
                            colors = ButtonDefaults.buttonColors(
                                contentColor = colorResource(id = R.color.white),
                                containerColor = colorResource(id = R.color.blue_text),
                            ),
                        ) {
                            Text(
                                text = "Smart Connect",
                                fontSize = 14.sp,
                                fontWeight = FontWeight.Medium,
                                fontStyle = FontStyle.Normal,
                                modifier = Modifier.background(Color.Transparent)
                            )
                        }
                    }
                }

                /* Select Server Box*/
                if (!isConnect) {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(20.dp)
                            .padding(bottom = 0.dp)
                            .height(70.dp)
                            .border(
                                border = BorderStroke(2.dp, Color.White),
                                shape = RoundedCornerShape(28.dp)
                            )
                            .background(
                                shape = RoundedCornerShape(28.dp),
                                color = Color.White
                            ),
                        contentAlignment = Alignment.CenterStart,
                    ) {
                        AddRowSelectServer(navHostController)
                    }
                }
            }
        }

    }

}

@Composable
fun ColumnScope.ColumnText(text: String, color: Color, size: TextUnit) {
    Surface(
        modifier = Modifier.padding(start = 0.dp),
        color = Color.Transparent
    ) {
        Text(text = text,
            style = TextStyle(
                color = color,
                fontSize = size,
                fontStyle = FontStyle.Normal,
                fontWeight = FontWeight.Medium
            ),
            maxLines = 1,
            modifier = Modifier
                .padding(start = 0.dp, end = 0.dp)
                .weight(1f)
        )
    }
}

@Composable
fun BoxScope.AddRowSmart(navHostController: NavHostController) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(15.dp)
            .background(Color.Transparent),
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically
    ) {

        Surface(
            modifier = Modifier.padding(start = 0.dp),
            color = Color.Transparent
        ) {
            Image(
                painter = painterResource(id = R.drawable.dubai3x),
                contentDescription = "Country Logo",
                modifier = Modifier
                    .padding(start = 0.dp)
                    .size(40.dp)
                    .weight(1f)
            )
        }
        Column(
            modifier = Modifier
                .padding(start = 12.dp)
        ) {
            ColumnText(
                text = "Fastest Location",
                color = colorResource(id = R.color.blue_text),
                size = 12.sp
            )
            ColumnText(
                text = "United Arab Emirates",
                color = colorResource(id = R.color.dark_blue_gray_text),
                size = 16.sp
            )
        }
        Spacer(modifier = Modifier.weight(1F))
        Surface(
            modifier = Modifier.padding(start = 15.dp),
            color = Color.Transparent
        ) {
            ClickableText(
                text = AnnotatedString("Change"),
                style = TextStyle(
                    color = colorResource(id = R.color.light_blue_2),
                    fontSize = 14.sp,
                    textAlign = TextAlign.Right
                ),
                onClick = {
                    navHostController.navigate(
                        Screen.ServerList.route
                    )
                    Screen.ServerList.isTrue = false
                    Log.d("button_click_change", "Pressed")
                },
                )
        }
    }
}

@Composable
fun BoxScope.AddRowSelectServer(navHostController: NavHostController) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(15.dp)
            .background(Color.Transparent)
            .pointerInput(Unit) {
                detectTapGestures {
                    navHostController.navigate(
                        Screen.ServerList.route
                    )
                    Screen.ServerList.isTrue = true
                    Log.d("button_click_change", "Pressed")
                }
            }
//            .clickable {
//                Log.d("test_server_button", "ServerTab Clicked")
//            }
        ,
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically
    ) {

        Surface(
            modifier = Modifier.padding(start = 0.dp),
            color = Color.Transparent
        ) {
            Image(
                painter = painterResource(id = R.drawable.worldmap3x),
                contentDescription = "World",
                modifier = Modifier
                    .padding(start = 0.dp)
                    .size(40.dp)
                    .weight(1f)
            )
        }
        Surface(
            modifier = Modifier.padding(start = 0.dp),
            color = Color.Transparent
        ) {
            Text(text = "See All Locations",
                style = TextStyle(
                    color = colorResource(id = R.color.dark_blue_gray_text),
                    fontSize = 16.sp,
                    fontStyle = FontStyle.Normal,
                    fontWeight = FontWeight.Medium
                ),
                maxLines = 2,
                modifier = Modifier
                    .padding(start = 12.dp, end = 0.dp)
                    .weight(1f)
            )
        }
        Spacer(modifier = Modifier.weight(1F))
        Surface(
            modifier = Modifier.padding(start = 15.dp),
            color = Color.Transparent

        ) {
            Image(
                painter = painterResource(id = R.drawable.frontarrow3x),
                contentDescription = "Front_Arrow",
                modifier = Modifier
                    .padding(start = 0.dp, end = 5.dp)
                    .size(10.dp, 18.dp)
                    .weight(1f)
            )
        }

    }
}

@Composable
fun ColumnScope.AddText(text: String, size: TextUnit, color: Color) {
    Text(
        text = text,
        style = TextStyle(
            fontSize = size,
            color = color,
            textAlign = TextAlign.Center,
            fontStyle = FontStyle.Normal,
            fontWeight = FontWeight.Medium
        )
    )
}

@Composable
fun RowScope.AddText(text: String, size: TextUnit, color: Color) {
    Text(
        text = text,
        style = TextStyle(
            fontSize = size,
            color = color,
            textAlign = TextAlign.Center,
            fontStyle = FontStyle.Normal,
            fontWeight = FontWeight.Medium
        )
    )
}

@Composable
fun pinkBackground(): Painter{
    return painterResource(id = R.drawable.pinkbackground3x) 
}

@Composable
fun blueBackground(): Painter{
    return painterResource(id = R.drawable.bluebackground3x)
}

@Preview(showSystemUi = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Composable
fun HomePreview() {
    Home(rememberNavController())
}

@Preview(showSystemUi = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun HomePreviewDark() {
    Home(rememberNavController())
}