package com.example.libary

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.AbsoluteRoundedCornerShape
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.libary.theme.BlackColor
import com.example.libary.theme.CaptionColor
import com.example.libary.theme.InputBGColor
import com.example.libary.theme.InputStroke

@Composable
fun Select(
    modifier: Modifier = Modifier,
    value: MutableState<String>,
    data: List<String>,
    placeholder: String = "",
    leadingIcon: @Composable (() -> Unit)? = null,
    ) {
    var modal by remember { mutableStateOf(false) }

    OutlinedTextField(
        value = value.value,
        onValueChange = {},
        modifier = modifier,
        shape = AbsoluteRoundedCornerShape(10.dp),
        leadingIcon = leadingIcon,
        placeholder = {
            Text(
                text = placeholder,
                fontWeight = FontWeight.W400,
                fontSize = 16.sp,
                color = CaptionColor
            )
        },
        colors = TextFieldDefaults.colors(
            focusedPlaceholderColor = CaptionColor,
            unfocusedPlaceholderColor = CaptionColor,
            focusedTextColor = BlackColor,
            unfocusedTextColor = BlackColor,
            focusedIndicatorColor = InputStroke,
            unfocusedIndicatorColor = InputStroke,
            focusedContainerColor = InputBGColor,
            unfocusedContainerColor = InputBGColor
        ),
        trailingIcon = {
            Image(
                painter = painterResource(R.drawable.dr_1),
                contentDescription = null,
                modifier = Modifier.testTag("dr_1").clickable {
                    modal = true
                }
            )
        }
    )

    if (modal) {
        BottomSheet {
            LazyColumn {
                items(data) {
                    Text(
                        text = it,
                        fontSize = 20.sp,
                        fontWeight = FontWeight.W600,
                        modifier = Modifier
                            .padding(5.dp)
                            .clickable {
                                value.value = it
                                modal = false
                            }
                    )
                    HorizontalDivider()
                }
            }
        }
    }
}

@Preview
@Composable
private fun SelectPrev() {
    val value = remember { mutableStateOf("") }

    Select(
        value = value,
        data = listOf("test")
    )
}