package com.prodhack.moscow2025.presentation.components.standart

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.SheetState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import com.prodhack.moscow2025.R
import com.prodhack.moscow2025.presentation.screens.fillProfile.UIPhoneNumberPattern
import com.prodhack.moscow2025.presentation.theme.Paddings
import com.prodhack.moscow2025.presentation.theme.Shapes
import com.prodhack.moscow2025.presentation.utils.PhoneVisualTransformation

@Composable
fun TPhoneField(
	modifier: Modifier = Modifier,
	currentPattern: UIPhoneNumberPattern?,
	currentPhone: String,
	onPhoneChange: (String) -> Unit,
	error: String?,
	onOpenCountryList: () -> Unit,
) {
	val colorScheme = MaterialTheme.colorScheme
	Row(
		modifier = modifier.height(IntrinsicSize.Min),
		verticalAlignment = Alignment.CenterVertically,
		horizontalArrangement = Arrangement.spacedBy(
			Paddings.medium
		)
	) {
		FieldWrapper(
			modifier = Modifier
				.width(IntrinsicSize.Min)
				.fillMaxHeight()
		) {
			BasicTextField(
				modifier = Modifier
					.fillMaxSize()
					.offset(y = 5.dp)
					.padding(bottom = 16.dp)
					.background(colorScheme.primary, Shapes.smallRoundedBox)
					.clip(Shapes.smallRoundedBox),
				value = currentPattern?.prefix ?: "",
				onValueChange = {},
				readOnly = true,
				textStyle = TextStyle(
					color = colorScheme.onPrimary
				),
				decorationBox = { innerTextField ->
					Row(
						modifier = Modifier
							.clickable {
								onOpenCountryList()
							}
							.padding(8.dp),
						verticalAlignment = Alignment.CenterVertically
					) {
						Box(modifier = Modifier.weight(1f)) {
							innerTextField()
						}
						Icon(
							modifier = Modifier.size(15.dp),
							painter = painterResource(R.drawable.ic_arr_dropdown),
							tint = colorScheme.onPrimary,
							contentDescription = null
						)
					}
				}
			)
		}

		TTTextField(
			value = currentPhone,
			onValueChange = onPhoneChange,
			label = "Ваш телефон",
			keyboardOptions = KeyboardOptions(
				keyboardType = KeyboardType.Phone
			),
			visualTransformation = currentPattern?.let {
				PhoneVisualTransformation(
					it.pattern,
					'0'
				)
			} ?: VisualTransformation.None,
			error = error
		)
	}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TPhoneCountryList(
	modifier: Modifier = Modifier,
	isSheetOpen: MutableState<Boolean>,
	sheetState: SheetState,
	patternList: List<UIPhoneNumberPattern>,
	setPattern: (UIPhoneNumberPattern) -> Unit
) {
	if (isSheetOpen.value) {
		ModalBottomSheet(
			modifier = modifier,
			sheetState = sheetState,
			onDismissRequest = {
				isSheetOpen.value = false
			},
		) {
			Column(
				horizontalAlignment = Alignment.CenterHorizontally,
				verticalArrangement = Arrangement.Center
			) {
				LazyColumn(modifier = Modifier.fillMaxSize()) {
					items(patternList) { pattern ->
						Text(
							text = pattern.name,
							modifier = Modifier
								.fillMaxWidth()
								.padding(10.dp)
								.clickable {
									setPattern(pattern)
									isSheetOpen.value = false
								}
						)
					}
				}
			}
		}
	}
}