package com.fastest.pass.login.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.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
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.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.material3.Button
import androidx.compose.material3.ButtonDefaults
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.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.input.pointer.pointerInput
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.AnnotatedString
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 {
    SIGNUP_CLICK,
    FORGOT_PASSWORD_CLICK,
    GO_BACK,
    MASTER_LOGIN,
    HOME_SCREEN
}

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

    Box(
        modifier = Modifier
            .background(colorResource(id = R.color.blue_login))
            .fillMaxSize()
            .statusBarsPadding()
            .pointerInput(Unit) {
                detectTapGestures {
                    focusManager.clearFocus()
                    keyboardController?.hide()
                }
            }
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(top = 0.dp)
                .statusBarsPadding()
        ) {
            ShowHeaderLogin(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),
                    )
            ) {
                ShowWelcomeText(R.string.login_to_continue)
                LoginTextField(keyboardController, focusManager)
                Spacer(modifier = Modifier.height(20.dp))
                PasswordTextField(keyboardController, focusManager)
                Spacer(modifier = Modifier.height(25.dp))
                LoginButton(buttonText = R.string.login) { clickType ->
                    clickType(clickType)
                }
                ForgotPasswordText() { clickType ->
                    clickType(clickType)
                }
                CreateAccountText { clickType ->
                    clickType(clickType)
                }
            }
        }
    }
}

@Composable
fun ColumnScope.ShowHeaderLogin(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.ShowWelcomeText(
    masterPasswordText: Int,
) {
    Text(
        text = stringResource(id = R.string.welcome_back),
        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.LoginTextField(
    keyboardController: SoftwareKeyboardController?,
    focusManager: FocusManager
) {
    var emailText by remember { mutableStateOf("") }

    TextField(
        value = emailText,
        onValueChange = {
            emailText = it
        },
        textStyle = MaterialTheme.typography.displayMedium,
        modifier = Modifier
            .padding(start = 30.dp, end = 30.dp, top = 50.dp)
            .align(Alignment.Start)
            .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.enter_email_address),
                color = colorResource(id = R.color.gray_splash),
                style = MaterialTheme.typography.displayMedium
            )
        },
//        label = {
//            Text(text = context.getString(R.string.email),
//                style = MaterialTheme.typography.customTypography.bodySmall
//            )
//        },
        leadingIcon = {
            Icon(
                painter = painterResource(id = R.drawable.sms3x),
                contentDescription = "Email Logo",
                modifier = Modifier
                    .size(24.dp, 24.dp),
                tint = colorResource(id = R.color.gray_splash)
            )
        },
        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.Email,
            imeAction = ImeAction.Done
        ),
        keyboardActions = KeyboardActions(
            onDone = {
                focusManager.clearFocus()
                keyboardController?.hide()
            }
        ),
    )
}

@Composable
fun ColumnScope.PasswordTextField(
    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.Start)
            .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.enter_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(),
        leadingIcon = {
            Icon(
                painter = painterResource(id = R.drawable.lock3x),
                contentDescription = "Email Logo",
                modifier = Modifier
                    .size(24.dp, 24.dp),
                tint = colorResource(id = R.color.gray_splash)
            )
        },
        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.LoginButton(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
        )
    }
}

@Composable
fun ColumnScope.ForgotPasswordText(clickType: (ClickType) -> Unit) {
    ClickableText(
        text = AnnotatedString(stringResource(id = R.string.forgot_your_password)),
        onClick = {
            clickType.invoke(ClickType.FORGOT_PASSWORD_CLICK)
        },
        style = MaterialTheme.typography.displayLarge.copy(
            fontSize = 16.sp,
            lineHeight = 25.sp,
            color = colorResource(id = R.color.sky_green),
        ),
        modifier = Modifier
            .align(Alignment.End)
            .padding(start = 30.dp, top = 15.dp, end = 35.dp)
    )
}

@Composable
fun ColumnScope.CreateAccountText(clickType: (ClickType) -> Unit) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 25.dp)
            .padding(horizontal = 30.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center
    ) {
        Text(
            text = stringResource(id = R.string.dont_have_account),
            color = colorResource(id = R.color.gray_splash),
            style = MaterialTheme.typography.displayLarge.copy(
                fontSize = 16.sp
            ),
            lineHeight = 25.sp,
            modifier = Modifier
        )
        Spacer(modifier = Modifier.width(3.dp))
        ClickableText(
            text = AnnotatedString(stringResource(id = R.string.create_account)),
            onClick = {
                clickType.invoke(ClickType.SIGNUP_CLICK)
            },
            style = MaterialTheme.typography.displayLarge.copy(
                fontSize = 16.sp,
                lineHeight = 25.sp,
                color = colorResource(id = R.color.sky_green),
            ),
            modifier = Modifier,
        )
    }
}