package com.isidroid.compose_component

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.isImeVisible
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text2.BasicTextField2
import androidx.compose.foundation.text2.input.TextFieldLineLimits
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Color.Companion.Red
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.isidroid.compose_components.Styler

@OptIn(ExperimentalFoundationApi::class, ExperimentalLayoutApi::class)
@Composable
fun FormInputComment(
    hint: String,
    value: String,
    modifier: Modifier = Modifier,
    isRequired: Boolean = false,
    onAvailableSubmitChanged: (Boolean) -> Unit = {},
    onEnterTextChanged: (String) -> Unit = {},
    colorStyler: Styler = Styler(),
    trailingIconsContent: @Composable () -> Unit = {},
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions.Default,
    lineLimits: TextFieldLineLimits = TextFieldLineLimits.Default,
) {
    val minCharacterLimit = 3
    var inputValue by remember { mutableStateOf(value) }
    val isAvailableSubmit = inputValue.length >= minCharacterLimit

    LaunchedEffect(isAvailableSubmit) { onAvailableSubmitChanged(isAvailableSubmit) }
    val shape = RoundedCornerShape(100.dp)

    val focusRequester = remember { FocusRequester() }
    val focusManager = LocalFocusManager.current

    val isImeVisible = WindowInsets.isImeVisible
    LaunchedEffect(isImeVisible) {
        if(!isImeVisible)
            focusManager.clearFocus(true)
    }

    Row(
        modifier = modifier
            .fillMaxWidth()
            .border(1.dp, colorStyler.colorBorder, shape)
            .clip(shape)
            .background(colorStyler.colorContainer)
            .padding(vertical = 14.dp)
            .padding(start = 14.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Box(
            modifier = Modifier.weight(1f),
            contentAlignment = Alignment.Center
        ) {
            BasicTextField2(
                value = inputValue,
                keyboardOptions = keyboardOptions,
                keyboardActions = keyboardActions,
                lineLimits = lineLimits,
                onValueChange = {
                    inputValue = it
                    onEnterTextChanged(it)
                },
                textStyle = MaterialTheme.typography.bodyMedium.copy(fontSize = 14.sp, letterSpacing = (-.15).sp, lineHeight = 18.sp),
                modifier = Modifier
                    .fillMaxWidth()
                    .focusRequester(focusRequester)
                ,
            )

            if (inputValue.isBlank())
                LabelComponent(text = hint, isRequired = isRequired, color = colorStyler.colorBorder, modifier = Modifier.fillMaxWidth())
        }

        trailingIconsContent()
    }
}

@Composable
private fun LabelComponent(text: String, isRequired: Boolean, color: Color, modifier: Modifier = Modifier) {
    Row(modifier = modifier) {
        Text(
            text = text,
            style = MaterialTheme.typography.bodySmall,
            color = color,
            fontSize = 14.sp,
            letterSpacing = (-.15).sp
        )

        if (isRequired)
            RequireIndicatorComponent()
    }
}


@Composable
fun RequireIndicatorComponent(modifier: Modifier = Modifier) {
    Box(
        modifier = modifier
            .padding(start = 2.dp)
            .clip(CircleShape)
            .background(Red)
            .size(5.dp),
    )
}