package com.fastest.pass.masterlogin.presentation.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.statusBarsPadding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
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.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusManager
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.platform.SoftwareKeyboardController
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
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.unit.dp
import androidx.compose.ui.unit.sp
import com.fastest.pass.R

enum class ClickType {
    GO_BACK,
    HOME_SCREEN
}

@Composable
fun MasterLoginScreen(clickType: (ClickType) -> Unit) {
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusManager = LocalFocusManager.current

    Box(
        modifier = Modifier
            .background(colorResource(id = R.color.blue_login))
            .fillMaxSize()
            .statusBarsPadding()
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(top = 0.dp)
                .statusBarsPadding()
        ) {
            ShowMasterLoginHeader(text = stringResource(R.string.login)) { clickType ->
                clickType(clickType)
            }

            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(top = 25.dp)
                    .clip(RoundedCornerShape(topStart = 35.dp, topEnd = 35.dp))
                    .background(
                        colorResource(id = R.color.light_gray_login),
                    ),
            ) {
                ShowWelcomeTextMLS(R.string.enter_master_password)
                Spacer(modifier = Modifier.height(50.dp))
                PasswordTextFieldMLS(keyboardController, focusManager)
                Spacer(modifier = Modifier.height(25.dp))
                MasterLoginButton(buttonText = R.string.unlock_fastestpass) { clickType ->
                    clickType(clickType)
                }
            }
        }
    }
}

@Composable
fun ColumnScope.ShowMasterLoginHeader(text: String, clickType: (ClickType) -> Unit) {
    val c = LocalContext.current
    Row(
        modifier = Modifier
            .align(Alignment.Start)
            .padding(top = 30.dp)
            .fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically
    ) {
        IconButton(
            onClick = {
                clickType.invoke(ClickType.GO_BACK)
            },
            modifier = Modifier
                .padding(start = 30.dp)
                .size(24.dp, 24.dp)
        ) {
            Image(
                painter = painterResource(id = R.drawable.arrow_left),
                contentDescription = "Arrow-Back",
                modifier = Modifier.size(24.dp, 24.dp)
            )
        }
        Surface(
            modifier = Modifier
                .padding(start = 15.dp),
            color = colorResource(id = R.color.transparent)
        ) {
            Text(
                text = text,
                color = colorResource(id = R.color.white),
                style = MaterialTheme.typography.displayLarge.copy(
                    fontSize = 24.sp
                ),
            )
        }
    }
}

@Composable
fun ColumnScope.ShowWelcomeTextMLS(
    masterPasswordText: Int,
) {
    Text(
        text = stringResource(id = R.string.master_password_required),
        color = colorResource(id = R.color.gray_splash),
        style = MaterialTheme.typography.headlineLarge.copy(
            fontSize = 26.sp
        ),
        lineHeight = 20.sp,
        modifier = Modifier
            .align(Alignment.Start)
            .padding(start = 30.dp, top = 35.dp)
    )
    Text(
        text = stringResource(id = masterPasswordText),
        color = colorResource(id = R.color.gray_splash),
        style = MaterialTheme.typography.displayLarge.copy(
            fontSize = 18.sp
        ),
        lineHeight = 25.sp,
        modifier = Modifier
            .align(Alignment.Start)
            .padding(start = 30.dp, top = 5.dp, end = 50.dp)

    )
}

@Composable
fun ColumnScope.PasswordTextFieldMLS(
    keyboardController: SoftwareKeyboardController?,
    focusManager: FocusManager
) {
    var passwordText by remember { mutableStateOf("") }
    var passwordVisible by remember { mutableStateOf(false) }

    TextField(
        value = passwordText,
        onValueChange = {
            passwordText = it
        },
        textStyle = MaterialTheme.typography.displayMedium,
        modifier = Modifier
            .padding(start = 30.dp, end = 30.dp)
            .align(Alignment.CenterHorizontally)
            .fillMaxWidth()
            .height(60.dp)
            .border(
                1.dp,
                color = colorResource(id = R.color.gray_border_textfield),
                shape = RoundedCornerShape(16.dp)
            )
            .background(color = colorResource(id = R.color.transparent)),
        shape = RoundedCornerShape(16.dp),
        placeholder = {
            Text(
                text = stringResource(id = R.string.master_password),
                color = colorResource(id = R.color.gray_splash),
                style = MaterialTheme.typography.displayMedium,
                )
        },
//        label = {
//            Text(text = context.getString(R.string.password),
//                style = MaterialTheme.typography.customTypography.bodyLarge
//            )
//        },
        maxLines = 1,
        colors = TextFieldDefaults.colors(
            focusedLabelColor = colorResource(id = R.color.gray_splash),
            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.gray_splash),
        ),
        keyboardOptions = KeyboardOptions(
            keyboardType = KeyboardType.Password,
            imeAction = ImeAction.Done
        ),
        keyboardActions = KeyboardActions(
            onDone = {
                focusManager.clearFocus()
                keyboardController?.hide()
            }
        ),
        visualTransformation =
        if (passwordVisible) VisualTransformation.Companion.None
        else PasswordVisualTransformation(),

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

            IconButton(onClick = {
                passwordVisible = !passwordVisible
            })
            {
                if (passwordVisible) {
                    Image(
                        painter = painterResource(id = R.drawable.eye_open),
                        contentDescription = description,
                        modifier = Modifier.size(24.dp)
                    )
                }
                else {
                    Image(
                        painter = painterResource(id = R.drawable.eye_slash3x),
                        contentDescription = description,
                        modifier = Modifier
                            .size(24.dp),
                        colorFilter = ColorFilter.tint(
                            colorResource(id = R.color.gray_splash)
                        )
                    )
                }
            }
        }
    )
}

@Composable
fun ColumnScope.MasterLoginButton(
    buttonText: Int,
    clickType: (ClickType) -> Unit
) {
    Button(
        modifier = Modifier
            .padding(start = 30.dp, end = 30.dp,)
            .background(colorResource(id = R.color.transparent))
            .fillMaxWidth()
            .height(60.dp)
            .clickable() { },
        onClick = {
            clickType.invoke(ClickType.HOME_SCREEN)
        },
        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.red_login_button),
        ),
    )
    {
        Text(
            text = stringResource(id = buttonText),
            style = MaterialTheme.typography.bodyMedium.copy(
                fontSize = 20.sp,
                color = colorResource(id = R.color.white)
            ),
            textAlign = TextAlign.Center
        )
    }
}