package com.syaroful.agrilinkvocpro.presentation.screen.register

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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
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.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.CustomSnackBarComponent
import com.syaroful.agrilinkvocpro.core.components.textTheme
import com.syaroful.agrilinkvocpro.core.utils.ResultState
import com.syaroful.agrilinkvocpro.presentation.theme.DarkGrey
import com.syaroful.agrilinkvocpro.presentation.theme.MainGreen
import org.koin.androidx.compose.koinViewModel

@Composable
fun RegisterScreen(
    registerViewModel: RegisterViewModel = koinViewModel(),
    onRegisterSuccess: () -> Unit,
    onNavigateToLogin: () -> Unit,
) {

    val signupState by registerViewModel.signupState.collectAsState()

    val focusManager = LocalFocusManager.current

    val snackBarHostState = remember { SnackbarHostState() }

    var name by remember { mutableStateOf("") }
    var email by remember { mutableStateOf("") }
    var username by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }

    Scaffold(
        snackbarHost = {
            SnackbarHost(hostState = snackBarHostState) { data ->
                CustomSnackBarComponent(
                    modifier = Modifier.padding(16.dp),
                    message = data.visuals.message,
                    icon = if (data.visuals.message.contains("Failed")) Icons.Default.Clear else Icons.Default.Check
                )
            }
        },
        modifier = Modifier.fillMaxSize()
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .verticalScroll(rememberScrollState())
                .fillMaxWidth()
                .padding(innerPadding)
                .imePadding(),
            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 = "Name",
                keyboardType = KeyboardType.Email,
                leadingIcon = painterResource(
                    R.drawable.solar_user_circle
                ),
                value = name,
                onValueChange = { name = it }
            )
            AppTextField(
                hint = "Username",
                keyboardType = KeyboardType.Email,
                leadingIcon = painterResource(
                    R.drawable.solar_user
                ),
                value = username,
                onValueChange = { username = it }
            )
            AppTextField(
                hint = "Email",
                keyboardType = KeyboardType.Email,
                leadingIcon = painterResource(
                    R.drawable.icon_email
                ),
                value = email,
                onValueChange = { email = it }
            )
            AppPasswordField(
                hint = "Password",
                keyboardType = KeyboardType.Password,
                value = password,
                onValueChange = { password = it }
            )

            AppButton(
                label = if (signupState is ResultState.Loading) "Memuat..." else "Daftar",
                isEnable = signupState !is ResultState.Loading,
            ) {
                focusManager.clearFocus()
                registerViewModel.signup(name, username, email, password)
            }
            if (signupState is ResultState.Error) {
                Text(
                    modifier = Modifier.padding(horizontal = 16.dp),
                    text = (signupState as ResultState.Error).message,
                    color = Color.Red
                )
            }

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.Center
            ) {
                Text(
                    text = "Sudah punya akun? ",
                    style = textTheme.titleSmall.copy(color = DarkGrey),
                    textAlign = TextAlign.Center
                )
                Text(
                    modifier = Modifier.clickable { onNavigateToLogin() },
                    text = "Masuk",
                    style = textTheme.titleSmall.copy(color = MainGreen),
                    textAlign = TextAlign.Center
                )
            }

            LaunchedEffect(signupState) {
                if (signupState is ResultState.Success<*>) {
                    snackBarHostState.showSnackbar(message = "Pendaftaran Akun Berhasil")
                    // Navigate to Home if Success
                    onRegisterSuccess()
                    registerViewModel.resetRegisterState()
                } else if (signupState is ResultState.Error) {
                    snackBarHostState.showSnackbar(message = (signupState as ResultState.Error).message)
                }
            }
        }
    }
}