package com.prodhack.moscow2025.presentation.screens.fillProfile

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
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.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
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.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.prodhack.moscow2025.R
import com.prodhack.moscow2025.domain.models.AuthField
import com.prodhack.moscow2025.presentation.components.standart.BigButton
import com.prodhack.moscow2025.presentation.components.standart.TPhoneCountryList
import com.prodhack.moscow2025.presentation.components.standart.TPhoneField
import com.prodhack.moscow2025.presentation.components.standart.TTTextField
import com.prodhack.moscow2025.presentation.utils.ErrorCollectorScope
import com.prodhack.moscow2025.presentation.utils.UIState
import org.koin.androidx.compose.koinViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ErrorCollectorScope.FillProfileScreen(
	snackbarHostState: SnackbarHostState,
	onSuccess: () -> Unit,
	viewModel: FillProfileViewModel = koinViewModel()
) {
	val typography = MaterialTheme.typography
	val colorScheme = MaterialTheme.colorScheme

	val sheetState = rememberModalBottomSheetState()
	val isSheetOpen = remember { mutableStateOf(false) }

	val formState by viewModel.formStateFillProfile.collectAsState()

	var errorText by remember { mutableStateOf("") }
	val fillProfileState by viewModel.profileFillState.collectAsStateWithCallbacks(
		onInputError = {
			errorText = it.error
		},
		onConnectionError = {
			errorText = "Нет подключения к сети"
		},
		onUnexpectedError = {
			errorText = it.error
		},
		onLoading = {
			errorText = ""
		},
		onSuccess = {
			errorText = ""
		}
	)

	LaunchedEffect(fillProfileState) {
		if (fillProfileState is UIState.Success) {
			onSuccess()
		}
	}

	LaunchedEffect(errorText) {
		if (errorText.isNotEmpty()) {
			snackbarHostState.showSnackbar(
				message = "Ошибка: $errorText",
				duration = SnackbarDuration.Short
			)
		}
	}

	Box(
		modifier = Modifier
			.fillMaxSize()
			.imePadding()
			.systemBarsPadding(),
		contentAlignment = Alignment.BottomStart
	) {
		Image(
			painter = painterResource(R.drawable.lottie),
			contentDescription = null,
			modifier = Modifier.width(130.dp),
			contentScale = ContentScale.Crop
		)

		Column(
			modifier = Modifier
				.fillMaxSize()
				.padding(start = 30.dp, end = 30.dp)
				.verticalScroll(rememberScrollState()),
			horizontalAlignment = Alignment.CenterHorizontally
		) {
			Row(
				modifier = Modifier.fillMaxWidth(),
				horizontalArrangement = Arrangement.SpaceBetween,
				verticalAlignment = Alignment.CenterVertically
			) {
				Text(
					text = "Давайте\nзнакомиться!",
					style = typography.titleLarge,
					fontSize = 31.sp
				)
				Icon(
					painter = painterResource(R.drawable.app_logo),
					contentDescription = null,
					modifier = Modifier.size(140.dp)
				)
			}
			Spacer(Modifier.height(20.dp))
			TTTextField(
				value = formState.firstName,
				onValueChange = viewModel::onFirstNameChange,
				label = "Ваше имя",
				error = formState.errors[AuthField.FirstName],
			)
			Spacer(Modifier.height(12.dp))
			TTTextField(
				value = formState.lastName,
				onValueChange = viewModel::onLastNameChange,
				label = "Ваша фамилия",
				error = formState.errors[AuthField.LastName],
			)
			Spacer(Modifier.height(12.dp))
			TPhoneField(
				currentPattern = viewModel.currentPattern.value,
				currentPhone = formState.phone,
				onPhoneChange = viewModel::onPhoneChange,
				error = formState.errors[AuthField.Phone],
				onOpenCountryList =
					{
						isSheetOpen.value = true
					}
			)

			Spacer(modifier = Modifier.height(20.dp))
			BigButton(
				onClick = viewModel::submit,
				modifier = Modifier.fillMaxWidth(),
				buttonText = "Сохранить данные",
				isLoading = fillProfileState is UIState.Loading
			)
			Spacer(modifier = Modifier.height(80.dp))
		}
	}

	TPhoneCountryList(
		isSheetOpen = isSheetOpen,
		sheetState = sheetState,
		patternList = viewModel.phoneNumberPatterns,
		setPattern = {
			viewModel.currentPattern.value = it
		}
	)
}