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

import androidx.compose.foundation.layout.Arrangement
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.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Switch
import androidx.compose.material.SwitchDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.text.font.FontStyle
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 SettingToggleItemUio(
    val icon: DrawableResource,
    val title: StringResource,
    val description: StringResource,
    val checked: State<Boolean>,
    val onToggle: (Boolean) -> Unit,
) : SettingItemUio

@Composable
fun SettingToggleItem(
    modifier: Modifier = Modifier,
    margin: PaddingValues = SettingsScreenDefault.margin,
    padding: PaddingValues = SettingsScreenDefault.padding,
    spacing: DpSize = DpSize(width = 16.dp, height = 8.dp),
    item: SettingToggleItemUio,
) {
    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),
                )
            }

            Switch(
                colors = SwitchDefaults.colors(
                    checkedThumbColor = MaterialTheme.lwa.colorScheme.base.primary,
                ),
                onCheckedChange = item.onToggle,
                checked = item.checked.value
            )
        }
    }
}