package com.vpn.fastestvpnservice.screens

import android.content.res.Configuration
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.Column
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.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
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.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.paint
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
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.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
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.sealedClass.Screen

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ForgotPassword(navHostController: NavHostController) {
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusManager = LocalFocusManager.current

    Box(
        modifier = Modifier
            .paint(
                painter = painterResource(id = R.drawable.bg_img3),
                contentScale = ContentScale.FillBounds)
//            .background(
//                if (isSystemInDarkTheme()) Color.Black
//                else Color.Transparent
//            )
            .fillMaxSize()
            .pointerInput(Unit) {
                detectTapGestures {
                    focusManager.clearFocus()
                    keyboardController?.hide()
                }
            }
        ,
//        contentAlignment = Alignment.Center

    ) {
        Column (
            modifier = Modifier
                .fillMaxHeight()
                .fillMaxWidth()
            ,
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {

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

            IconButton(
                onClick = {
                    navHostController.popBackStack()
                },
                modifier = Modifier
                    .padding(start = 16.dp, top = 52.dp)
                    .align(Alignment.Start)
                )
            {
                Icon(painter = painterResource(
                    id = R.drawable.back_arrow3x),
                    contentDescription = "Back Button",
                    tint = colorResource(id = R.color.white),
                    modifier = Modifier
                        .height(36.dp)
                        .width(36.dp)
                        .align(Alignment.Start),
                )
            }
            Spacer(modifier = Modifier.height(45.dp))
            Image(
                painter = painterResource(id = R.drawable.fastestapp_logo3x),
                contentDescription = "FastestVPN",
                modifier = Modifier
                    .size(102.dp, 75.dp)
                    .background(Color.Transparent)
                    .padding(top = 0.dp)
                ,
            )
            Spacer(modifier = Modifier.height(50.dp))
            Text(
                modifier = Modifier
                    .padding(start = 16.dp)
                    .align(Alignment.Start)
                ,
                fontSize = 28.sp,
                fontWeight = FontWeight.Medium,
                text = "Forgot Password!",
                color = Color.White,
            )
            Text(
                modifier = Modifier
                    .padding(start = 16.dp)
                    .align(Alignment.Start),
                fontSize = 14.sp,
                fontWeight = FontWeight.Normal,
                text = "Enter the email address associated with your account.",
                color = colorResource(id = R.color.light_grey),
                maxLines = 2,
            )
            Spacer(modifier = Modifier.height(20.dp))

            var textChanged by remember { mutableStateOf("") }


            TextField(
                value = textChanged,
                onValueChange = {
                    textChanged = it
                },

                textStyle = TextStyle(
                    fontSize = 14.sp,
                    color = colorResource(id = R.color.white),

                    ),
                modifier = Modifier
                    .padding(start = 16.dp, end = 15.dp)
                    .align(Alignment.Start)
                    .fillMaxWidth()
                    .height(60.dp)
                    .border(
                        1.dp,
                        color = colorResource(id = R.color.white),
                        shape = RoundedCornerShape(16.dp)
                    ),

//                placeholder = {
//                    Text(text = "Enter email address",
//                        color = colorResource(id = R.color.white),
//                        fontSize = 14.sp
//                    )
//                },
                label = {
                    Text(text = "Email",
                        style = TextStyle(
                            colorResource(id = R.color.white),
                            fontSize = 12.sp
                        )
                    )
                },
                leadingIcon = {
                    Icon(
                        painter = painterResource(id = R.drawable.sms3x),
                        contentDescription = "Email Logo",
                        tint = colorResource(id = R.color.white),
                        modifier = Modifier
                            .size(24.dp, 24.dp)
                    )
                },
                maxLines = 1,
                colors = TextFieldDefaults.colors(
                    focusedLabelColor = Color.Blue,
                    unfocusedContainerColor = colorResource(id = R.color.transparent),
                    focusedContainerColor = colorResource(id = R.color.transparent),
                    focusedIndicatorColor = colorResource(id = R.color.transparent),
                    disabledIndicatorColor = colorResource(id = R.color.transparent),
                    unfocusedIndicatorColor = colorResource(id = R.color.transparent),
                    cursorColor = colorResource(id = R.color.white)
                ),
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.Email,
                    imeAction = ImeAction.Done
                ),
                keyboardActions = KeyboardActions(
                    onDone = {
                        focusManager.clearFocus()
                        keyboardController?.hide()
                    }
                ),

                )

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

            Button(
                onClick = {
                    Log.d("test_button", "onClick Send Code")
                },
                modifier = Modifier
                    .padding(start = 16.dp, end = 16.dp, bottom = 100.dp)
                    .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.light_blue),
                    containerColor = colorResource(id = R.color.white),
                ),
            )
            {
                Text(text = "Send Code",
                    style = TextStyle(
                        fontSize = 18.sp,
                        fontFamily = FontFamily.Default,
                        fontWeight = FontWeight.Medium,
                        color = colorResource(id = R.color.blue_text)
                    )
                )
                Log.d("test_button", "RowScope")
            }
        }



    }
}

@Preview
@Composable
fun ForgotPasswordPreview() {
    ForgotPassword(rememberNavController())
}

@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun ForgotPasswordPreviewDark() {
    ForgotPassword(rememberNavController())
}