package com.pixelized.desktop.lwa.ui.screen.settings.composable

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.screen.settings.SettingsScreenDefault
import com.pixelized.desktop.lwa.ui.theme.lwa
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.StringResource
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource

@Stable
data class SettingNumberItemUio(
    val icon: DrawableResource,
    val title: StringResource,
    val description: StringResource,
    val enable: State<Boolean>,
    val value: State<Int>,
    val onValueChange: (Int) -> Unit,
) : SettingItemUio

@Composable
fun SettingNumberItem(
    modifier: Modifier = Modifier,
    margin: PaddingValues = SettingsScreenDefault.margin,
    padding: PaddingValues = SettingsScreenDefault.padding,
    spacing: DpSize = DpSize(width = 16.dp, height = 8.dp),
    item: SettingNumberItemUio,
) {
    val colorScheme = MaterialTheme.lwa.colorScheme

    Surface(
        modifier = modifier.padding(paddingValues = margin),
        shape = MaterialTheme.lwa.shapes.settings,
        elevation = 1.dp,
    ) {
        Row(
            modifier = Modifier.padding(paddingValues = padding),
            horizontalArrangement = Arrangement.spacedBy(space = spacing.width),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Icon(
                modifier = Modifier.size(size = 24.dp),
                painter = painterResource(item.icon),
                contentDescription = null,
            )

            Column(
                modifier = Modifier.weight(weight = 1f),
                verticalArrangement = Arrangement.spacedBy(space = spacing.height),
            ) {
                Text(
                    style = MaterialTheme.lwa.typography.settings.title,
                    maxLines = 1,
                    text = stringResource(resource = item.title),
                )
                Text(
                    style = MaterialTheme.lwa.typography.settings.description,
                    fontStyle = FontStyle.Italic,
                    text = stringResource(resource = item.description),
                )
            }

            Column(
                modifier = Modifier.padding(end = 8.dp),
                verticalArrangement = Arrangement.spacedBy(space = 4.dp),
            ) {
                val focused = remember {
                    mutableStateOf(false)
                }
                val textColor = remember(item) {
                    derivedStateOf {
                        when (item.enable.value) {
                            true -> colorScheme.base.primary
                            else -> colorScheme.base.onSurface.copy(alpha = 0.3f)
                        }
                    }
                }
                val borderColor = remember(item, focused) {
                    derivedStateOf {
                        when (focused.value) {
                            true -> colorScheme.base.primary
                            else -> colorScheme.base.onSurface.copy(alpha = 0.3f)
                        }
                    }
                }
                BasicTextField(
                    modifier = Modifier
                        .onFocusChanged { focused.value = it.isFocused }
                        .width(width = 64.dp)
                        .padding(horizontal = 2.dp),
                    textStyle = MaterialTheme.lwa.typography.settings.input.copy(
                        color = textColor.value,
                    ),
                    cursorBrush = SolidColor(MaterialTheme.colors.primary),
                    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
                    singleLine = true,
                    enabled = item.enable.value,
                    value = "${item.value.value}",
                    onValueChange = { item.onValueChange(it.toIntOrNull() ?: 0) },
                )
                Box(
                    modifier = Modifier
                        .background(color = borderColor.value)
                        .size(width = 64.dp, height = 2.dp),
                )
            }
        }
    }
}