package com.syaroful.agrilinkvocpro.core.components

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.syaroful.agrilinkvocpro.R
import com.syaroful.agrilinkvocpro.presentation.theme.LightGrey
import com.syaroful.agrilinkvocpro.presentation.theme.MainGreen


@OptIn(ExperimentalFoundationApi::class)
@Composable
fun AppTextField(
//    modifier: Modifier = Modifier,
    leadingIcon: Painter? = null,
    hint: String,
    keyboardType: KeyboardType,
    value: String,
    onValueChange: (String) -> Unit
) {
    OutlinedTextField(
        value = value,
        onValueChange = onValueChange,
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp)
            .border(BorderStroke(1.dp, LightGrey), RoundedCornerShape(8.dp)),
        leadingIcon = {
            if (leadingIcon != null) {
                Icon(
                    painter = leadingIcon,
                    contentDescription = "Email Icon",
                    tint = LightGrey,
                    modifier = Modifier.size(16.dp)
                )
            }
        },
        keyboardOptions = KeyboardOptions(
            keyboardType = keyboardType,
            imeAction = ImeAction.Done
        ),
        shape = RoundedCornerShape(8.dp),
        colors = TextFieldDefaults.colors(
            focusedContainerColor = Color.Transparent,
            unfocusedContainerColor = Color.Transparent,
            focusedIndicatorColor = MainGreen,
            unfocusedIndicatorColor = LightGrey
        ),
        singleLine = true,
        placeholder = {
            Text(
                hint,
                style = textTheme.bodyMedium
            )
        }
    )
}


@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable

fun TextFieldPreview() {
    AppTextField(
        hint = "Enter Your Email",
        leadingIcon = painterResource(R.drawable.icon_email),
        keyboardType = KeyboardType.Email,
        value = "",
        onValueChange = {}
    )
}