package com.diegocayo.organizame.presentation.features.welcome.firstStep

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.with
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.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.diegocayo.organizame.R
import com.diegocayo.organizame.presentation.navigation.AppScreen

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun FirstStepScreen(
    navController: NavHostController,
    viewModel: FirstStepViewModel = hiltViewModel()
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(32.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        AnimatedContent(
            targetState = viewModel.step,
            transitionSpec = { fadeIn() with fadeOut() },
            label = "Step Animation"
        ) { step ->
            when (step) {
                0 -> NameStep(
                    name = viewModel.name,
                    error = viewModel.nameError,
                    onNameChange = { viewModel.validateName(it) }
                )

                1 -> SurnameStep(
                    surname = viewModel.surname,
                    error = viewModel.surnameError,
                    onSurnameChange = { viewModel.validateSurname(it) }
                )

                2 -> BirthdateStep(
                    day = viewModel.birthdateDay,
                    month = viewModel.birthdateMonth,
                    year = viewModel.birthdateYear,
                    error = viewModel.birthdateError,
                    maxDay = viewModel.maxDay,
                    maxMonth = viewModel.maxMonth,
                    maxYear = viewModel.maxYear,
                    onDayChange = {
                        viewModel.birthdateDay = it.take(2)
                        viewModel.validateBirthdate()
                    },
                    onMonthChange = {
                        viewModel.birthdateMonth = it.take(2)
                        viewModel.validateBirthdate()
                    },
                    onYearChange = {
                        viewModel.birthdateYear = it.take(4)
                        viewModel.validateBirthdate()
                    }
                )
            }
        }

        Spacer(modifier = Modifier.height(32.dp))

        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.End
        ) {
            if (viewModel.step > 0) {
                OutlinedButton(
                    onClick = { viewModel.previousStep() },
                    modifier = Modifier
                        .weight(1f)
                        .padding(end = 8.dp)
                ) {
                    Text(stringResource(R.string.text_back))
                }
            }
            Button(
                onClick = {
                    if (viewModel.step == 2) {
                        viewModel.saveUserAndLogin {
                            navController.navigate(AppScreen.Main.route) {
                                popUpTo(AppScreen.Welcome.route) {
                                    inclusive = true
                                }
                            }
                        }
                    } else {
                        viewModel.nextStep()
                    }
                },
                modifier = Modifier.weight(1f),
                enabled = viewModel.canProceedToNextStep()
            ) {
                Text(
                    if (viewModel.step == 2) stringResource(R.string.text_complete_btn) else stringResource(
                        R.string.text_next
                    )
                )
            }
        }
    }
}

@Composable
private fun NameStep(
    name: String,
    error: String?,
    onNameChange: (String) -> Unit
) {
    Column {
        Text(stringResource(R.string.g1_your_name), style = MaterialTheme.typography.headlineSmall)
        Spacer(modifier = Modifier.height(16.dp))
        OutlinedTextField(
            value = name,
            onValueChange = onNameChange,
            label = { Text(stringResource(R.string.text_name)) },
            modifier = Modifier.fillMaxWidth(),
            isError = error != null,
            supportingText = { error?.let { Text(it) } }
        )
    }
}

@Composable
private fun SurnameStep(
    surname: String,
    error: String?,
    onSurnameChange: (String) -> Unit
) {
    Column {
        Text(
            stringResource(R.string.g2_your_surname),
            style = MaterialTheme.typography.headlineSmall
        )
        Spacer(modifier = Modifier.height(16.dp))
        OutlinedTextField(
            value = surname,
            onValueChange = onSurnameChange,
            label = { Text(stringResource(R.string.text_surname)) },
            modifier = Modifier.fillMaxWidth(),
            isError = error != null,
            supportingText = { error?.let { Text(it) } }
        )
    }
}

@Composable
private fun BirthdateStep(
    day: String,
    month: String,
    year: String,
    error: Int?,
    maxDay: Int,
    maxMonth: Int,
    maxYear: Int,
    onDayChange: (String) -> Unit,
    onMonthChange: (String) -> Unit,
    onYearChange: (String) -> Unit
) {
    Column {
        Text(
            stringResource(R.string.g2_your_birthday),
            style = MaterialTheme.typography.headlineSmall
        )
        Spacer(modifier = Modifier.height(16.dp))

        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            OutlinedTextField(
                value = day,
                onValueChange = { value ->
                    if (value.isEmpty() || value.all { it.isDigit() }) {
                        onDayChange(value)
                    }
                },
                label = { Text(stringResource(R.string.text_day)) },
                modifier = Modifier.weight(1f),
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                singleLine = true,
                isError = error != null
            )

            Spacer(modifier = Modifier.width(8.dp))

            OutlinedTextField(
                value = month,
                onValueChange = { value ->
                    if (value.isEmpty() || value.all { it.isDigit() }) {
                        onMonthChange(value)
                    }
                },
                label = { Text(stringResource(R.string.text_month)) },
                modifier = Modifier.weight(1f),
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                singleLine = true,
                isError = error != null
            )

            Spacer(modifier = Modifier.width(8.dp))

            OutlinedTextField(
                value = year,
                onValueChange = { value ->
                    if (value.isEmpty() || value.all { it.isDigit() }) {
                        onYearChange(value)
                    }
                },
                label = { Text(stringResource(R.string.text_year)) },
                modifier = Modifier.weight(1f),
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                singleLine = true,
                isError = error != null
            )
        }

        if (error != null) {
            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = stringResource(error),
                color = MaterialTheme.colorScheme.error,
                style = MaterialTheme.typography.bodySmall
            )
        }
    }
}

@Composable
fun navigateToNextScreen() {

}