package com.vpn.fastestvpnservice.screens.accountScreensAll

import android.content.res.Configuration
import android.util.Log
import android.widget.Toast
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.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
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.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.outlined.Visibility
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
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.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.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
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.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.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.subscriptionPackageList
import com.vpn.fastestvpnservice.customItems.getSelectedPosition

@Composable
fun ChangePassword(navHostController: NavHostController) {
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusManager = LocalFocusManager.current

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
            .pointerInput(Unit) {
                detectTapGestures {
                    focusManager.clearFocus()
                    keyboardController?.hide()
                }
            }
    ) {
        HeaderRowCP(navHostController = navHostController)

        Column(
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier
                .padding(top = 130.dp)
                .fillMaxWidth()
                .background(Color.Transparent)

        ) {
            TextFieldCurrPass()
            Spacer(modifier = Modifier.height(25.dp))
            TextFieldNewPass()
            Spacer(modifier = Modifier.height(25.dp))
            TextFieldReWriteNewPass()

            Button(
                onClick = {
                    Log.d("test_button", "onClick")
                },
                modifier = Modifier
                    .padding(
                        top = 30.dp,
                        start = 16.dp, end = 16.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.white),
                    containerColor = colorResource(id = R.color.blue_text),
                ),
            )
            {
                Text(text = "Save",
                    style = TextStyle(
                        color = colorResource(id = R.color.white),
                        textAlign = TextAlign.Center,
                        fontSize = 20.sp,
                        fontWeight = FontWeight.SemiBold,
                        fontStyle = FontStyle.Normal
                    )
                )
            }
        }
    }
}

@Composable
fun ColumnScope.TextFieldCurrPass() {
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusManager = LocalFocusManager.current

    var passwordChanged by remember { mutableStateOf("") }
    var passwordVisible by remember { mutableStateOf(false) }

    TextField(
        value = passwordChanged,
        onValueChange = {
            Log.d("onClick_test", "onValueChange -> ")
            passwordChanged = it
        },

        textStyle = TextStyle(
            fontSize = 20.sp,
            color = colorResource(id = R.color.dark_blue_gray_text),
            ),
        modifier = Modifier
            .padding(start = 15.dp, end = 15.dp)
            .align(Alignment.Start)
            .fillMaxWidth()
            .height(60.dp)
            .border(
                1.dp,
                color = colorResource(id = R.color.white),
                shape = RoundedCornerShape(16.dp)
            )
            .background(color = colorResource(id = R.color.transparent)),

        shape = RoundedCornerShape(15.dp),
//                placeholder = {
//                    Text(text = "Enter password",
//                        color = colorResource(id = R.color.white))
//                },
        label = {
            androidx.compose.material3.Text(text = "Current Password",
                style = TextStyle(
                    colorResource(id = R.color.dark_blue_gray_text),
                    fontSize = 16.sp,
                    textAlign = TextAlign.Center
                    )
            )
        },
        leadingIcon = {
            androidx.compose.material3.Icon(
                painter = painterResource(id = R.drawable.lock3x),
                contentDescription = "Password Logo",
                tint = colorResource(id = R.color.dark_blue_gray_text),
                modifier = Modifier
                    .size(24.dp, 24.dp)
            )
        },
        maxLines = 1,
        colors = TextFieldDefaults.colors(
            focusedLabelColor = Color.Blue,
            unfocusedContainerColor = colorResource(id = R.color.white),
            focusedContainerColor = colorResource(id = R.color.white),
            focusedIndicatorColor = colorResource(id = R.color.transparent),
            disabledIndicatorColor = colorResource(id = R.color.transparent),
            unfocusedIndicatorColor = colorResource(id = R.color.transparent),
            cursorColor = colorResource(id = R.color.dark_blue_gray_text)
        ),
        keyboardOptions = KeyboardOptions(
            keyboardType = KeyboardType.Password,
            imeAction = ImeAction.Done
        ),
        keyboardActions = KeyboardActions(
            onDone = {
                focusManager.clearFocus()
                keyboardController?.hide()
            }
        ),
        visualTransformation =
        if (passwordVisible) VisualTransformation.None
        else PasswordVisualTransformation(),

        trailingIcon = {
//                    val image
//                            = if (passwordVisible) Icons.Filled.Visibility
//                    else Icons.Filled.VisibilityOff

            val description = if (passwordVisible) "Hide Password"
            else "Show Password"

            androidx.compose.material3.IconButton(onClick = {
                passwordVisible = !passwordVisible
            })
            {
                if (passwordVisible) {
                    androidx.compose.material3.Icon(
                        imageVector = Icons.Outlined.Visibility,
                        contentDescription = description,
                        tint = colorResource(id = R.color.dark_blue_gray_text),
                        modifier = Modifier
                            .size(24.dp)
                            .alpha(0.5F)
                    )
                }
                else {
                    androidx.compose.material3.Icon(
                        painter = painterResource(id = R.drawable.eye_slash3x),
                        contentDescription = description,
                        tint = colorResource(id = R.color.dark_blue_gray_text),
                        modifier = Modifier
                            .size(24.dp)
                            .alpha(0.5F)
                    )
                }
            }
        }
    )
}

@Composable
fun ColumnScope.TextFieldNewPass() {
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusManager = LocalFocusManager.current

    var passwordChanged by remember { mutableStateOf("") }
    var passwordVisible by remember { mutableStateOf(false) }

    TextField(
        value = passwordChanged,
        onValueChange = {
            Log.d("onClick_test", "onValueChange -> ")
            passwordChanged = it
        },

        textStyle = TextStyle(
            fontSize = 20.sp,
            color = colorResource(id = R.color.dark_blue_gray_text),
        ),
        modifier = Modifier
            .padding(start = 15.dp, end = 15.dp)
            .align(Alignment.Start)
            .fillMaxWidth()
            .height(60.dp)
            .border(
                1.dp,
                color = colorResource(id = R.color.white),
                shape = RoundedCornerShape(16.dp)
            )
            .background(color = colorResource(id = R.color.transparent)),

        shape = RoundedCornerShape(15.dp),
//                placeholder = {
//                    Text(text = "Enter password",
//                        color = colorResource(id = R.color.white))
//                },
        label = {
            androidx.compose.material3.Text(text = "New Password",
                style = TextStyle(
                    colorResource(id = R.color.dark_blue_gray_text),
                    fontSize = 16.sp,
                    textAlign = TextAlign.Center
                )
            )
        },
        leadingIcon = {
            androidx.compose.material3.Icon(
                painter = painterResource(id = R.drawable.lock3x),
                contentDescription = "Password Logo",
                tint = colorResource(id = R.color.dark_blue_gray_text),
                modifier = Modifier
                    .size(24.dp, 24.dp)
            )
        },
        maxLines = 1,
        colors = TextFieldDefaults.colors(
            focusedLabelColor = Color.Blue,
            unfocusedContainerColor = colorResource(id = R.color.white),
            focusedContainerColor = colorResource(id = R.color.white),
            focusedIndicatorColor = colorResource(id = R.color.transparent),
            disabledIndicatorColor = colorResource(id = R.color.transparent),
            unfocusedIndicatorColor = colorResource(id = R.color.transparent),
            cursorColor = colorResource(id = R.color.dark_blue_gray_text)
        ),
        keyboardOptions = KeyboardOptions(
            keyboardType = KeyboardType.Password,
            imeAction = ImeAction.Done
        ),
        keyboardActions = KeyboardActions(
            onDone = {
                focusManager.clearFocus()
                keyboardController?.hide()
            }
        ),
        visualTransformation =
        if (passwordVisible) VisualTransformation.None
        else PasswordVisualTransformation(),

        trailingIcon = {
//                    val image
//                            = if (passwordVisible) Icons.Filled.Visibility
//                    else Icons.Filled.VisibilityOff

            val description = if (passwordVisible) "Hide Password"
            else "Show Password"

            androidx.compose.material3.IconButton(onClick = {
                passwordVisible = !passwordVisible
            })
            {
                if (passwordVisible) {
                    androidx.compose.material3.Icon(
                        imageVector = Icons.Outlined.Visibility,
                        contentDescription = description,
                        tint = colorResource(id = R.color.dark_blue_gray_text),
                        modifier = Modifier
                            .size(24.dp)
                            .alpha(0.5F)
                    )
                }
                else {
                    androidx.compose.material3.Icon(
                        painter = painterResource(id = R.drawable.eye_slash3x),
                        contentDescription = description,
                        tint = colorResource(id = R.color.dark_blue_gray_text),
                        modifier = Modifier
                            .size(24.dp)
                            .alpha(0.5F)
                    )
                }
            }
        }
    )
}

@Composable
fun ColumnScope.TextFieldReWriteNewPass() {
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusManager = LocalFocusManager.current

    var passwordChanged by remember { mutableStateOf("") }
    var passwordVisible by remember { mutableStateOf(false) }

    TextField(
        value = passwordChanged,
        onValueChange = {
            Log.d("onClick_test", "onValueChange -> ")
            passwordChanged = it
        },

        textStyle = TextStyle(
            fontSize = 20.sp,
            color = colorResource(id = R.color.dark_blue_gray_text),
        ),
        modifier = Modifier
            .padding(start = 15.dp, end = 15.dp)
            .align(Alignment.Start)
            .fillMaxWidth()
            .height(60.dp)
            .border(
                1.dp,
                color = colorResource(id = R.color.white),
                shape = RoundedCornerShape(16.dp)
            )
            .background(color = colorResource(id = R.color.transparent)),

        shape = RoundedCornerShape(15.dp),
//                placeholder = {
//                    Text(text = "Enter password",
//                        color = colorResource(id = R.color.white))
//                },
        label = {
            androidx.compose.material3.Text(text = "Rewrite New Password",
                style = TextStyle(
                    colorResource(id = R.color.dark_blue_gray_text),
                    fontSize = 16.sp,
                    textAlign = TextAlign.Center
                )
            )
        },
        leadingIcon = {
            androidx.compose.material3.Icon(
                painter = painterResource(id = R.drawable.lock3x),
                contentDescription = "Password Logo",
                tint = colorResource(id = R.color.dark_blue_gray_text),
                modifier = Modifier
                    .size(24.dp, 24.dp)
            )
        },
        maxLines = 1,
        colors = TextFieldDefaults.colors(
            focusedLabelColor = Color.Blue,
            unfocusedContainerColor = colorResource(id = R.color.white),
            focusedContainerColor = colorResource(id = R.color.white),
            focusedIndicatorColor = colorResource(id = R.color.transparent),
            disabledIndicatorColor = colorResource(id = R.color.transparent),
            unfocusedIndicatorColor = colorResource(id = R.color.transparent),
            cursorColor = colorResource(id = R.color.dark_blue_gray_text)
        ),
        keyboardOptions = KeyboardOptions(
            keyboardType = KeyboardType.Password,
            imeAction = ImeAction.Done
        ),
        keyboardActions = KeyboardActions(
            onDone = {
                focusManager.clearFocus()
                keyboardController?.hide()
            }
        ),
        visualTransformation =
        if (passwordVisible) VisualTransformation.None
        else PasswordVisualTransformation(),

        trailingIcon = {
//                    val image
//                            = if (passwordVisible) Icons.Filled.Visibility
//                    else Icons.Filled.VisibilityOff

            val description = if (passwordVisible) "Hide Password"
            else "Show Password"

            androidx.compose.material3.IconButton(onClick = {
                passwordVisible = !passwordVisible
            })
            {
                if (passwordVisible) {
                    androidx.compose.material3.Icon(
                        imageVector = Icons.Outlined.Visibility,
                        contentDescription = description,
                        tint = colorResource(id = R.color.dark_blue_gray_text),
                        modifier = Modifier
                            .size(24.dp)
                            .alpha(0.5F)
                    )
                }
                else {
                    androidx.compose.material3.Icon(
                        painter = painterResource(id = R.drawable.eye_slash3x),
                        contentDescription = description,
                        tint = colorResource(id = R.color.dark_blue_gray_text),
                        modifier = Modifier
                            .size(24.dp)
                            .alpha(0.5F)
                    )
                }
            }
        }
    )
}

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

        )
    }
}

@Preview
@Composable
fun ChangePasswordPreview() {
    ChangePassword(rememberNavController())
}

@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun ChangePasswordPreviewDark() {
    ChangePassword(rememberNavController())
}