package com.syaroful.agrilinkvocpro.presentation.screen.login

import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
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.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
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.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.syaroful.agrilinkvocpro.R
import com.syaroful.agrilinkvocpro.core.components.AppButton
import com.syaroful.agrilinkvocpro.core.components.AppPasswordField
import com.syaroful.agrilinkvocpro.core.components.AppTextField
import com.syaroful.agrilinkvocpro.core.components.textTheme
import com.syaroful.agrilinkvocpro.core.utils.ResultState
import com.syaroful.agrilinkvocpro.presentation.theme.AgrilinkVocproTheme
import com.syaroful.agrilinkvocpro.presentation.theme.DarkGrey
import com.syaroful.agrilinkvocpro.presentation.theme.MainGreen
import org.koin.androidx.compose.koinViewModel


@Composable
fun LoginScreen(
    loginViewModel: LoginViewModel,
    onLoginSuccess: () -> Unit,
    onNavigateToRegister: () -> Unit,
) {

    val loginState by loginViewModel.loginState.collectAsState()

    val focusManager = LocalFocusManager.current
    var username by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }

    Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(innerPadding),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            Image(
                painter = painterResource(id = R.drawable.greenhouse_banner),
                contentDescription = "Banner",
                modifier = Modifier
                    .height(180.dp)
                    .align(Alignment.CenterHorizontally),
                contentScale = ContentScale.Crop
            )
            Text(
                modifier = Modifier.fillMaxWidth(),
                text = "Login", style = textTheme.titleMedium, textAlign = TextAlign.Center
            )
            Text(
                modifier = Modifier.fillMaxWidth(),
                text = "Halo! yuk masuk ke dalam akunmu",
                style = textTheme.titleSmall.copy(color = DarkGrey),
                textAlign = TextAlign.Center
            )
            AppTextField(
                hint = "Username",
                keyboardType = KeyboardType.Email,
                leadingIcon = painterResource(
                    R.drawable.icon_email
                ),
                value = username,
                onValueChange = { username = it }
            )
            AppPasswordField(
                hint = "Password",
                keyboardType = KeyboardType.Password,
                value = password,
                onValueChange = { password = it }
            )
            AppButton(
                label = if (loginState is ResultState.Loading) "Memuat..." else "Login",
                isEnable = loginState !is ResultState.Loading,
            ) {
                focusManager.clearFocus()
                loginViewModel.login(username = username, password = password)

            }
            // Error Message
            if (loginState is ResultState.Error) {
                Text(
                    modifier = Modifier.padding(horizontal = 16.dp),
                    text = (loginState as ResultState.Error).message,
                    color = Color.Red
                )
            }
            Spacer(modifier = Modifier.height(16.dp))
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.Center
            ) {
                Text(
                    text = "Belum punya akun? ",
                    style = textTheme.titleSmall.copy(color = DarkGrey),
                    textAlign = TextAlign.Center
                )
                Text(
                    modifier = Modifier.clickable { onNavigateToRegister() },
                    text = "Daftar disini",
                    style = textTheme.titleSmall.copy(color = MainGreen),
                    textAlign = TextAlign.Center
                )
            }
            // Navigate to Home if Success
            LaunchedEffect(loginState) {
                if (loginState is ResultState.Success<*>) {
                    onLoginSuccess()
                    loginViewModel.resetLoginState()
                }
            }
        }
    }
}

@Preview(showBackground = true, name = "Light Mode")
@Preview(showBackground = true, name = "Dark Mode", uiMode = UI_MODE_NIGHT_YES)
@Composable
fun LoginScreenPreview() {
    AgrilinkVocproTheme {
        LoginScreen(
            loginViewModel = koinViewModel(),
            onLoginSuccess = {},
            onNavigateToRegister = {}
        )
    }
}
