package com.pixelized.desktop.lwa.ui.screen.admin.character.edit.item

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.composable.checkbox.LwaCheckBox
import com.pixelized.desktop.lwa.ui.composable.checkbox.LwaCheckBoxUio
import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextField
import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio
import com.pixelized.desktop.lwa.ui.theme.color.component.LwaCheckboxColors
import com.pixelized.desktop.lwa.utils.extention.moveFocusOnTab

@Stable
data class GMSkillFieldUio(
    val key: String,
    val id: LwaTextFieldUio,
    val label: LwaTextFieldUio,
    val description: LwaTextFieldUio?,
    val base: LwaTextFieldUio,
    val bonus: LwaTextFieldUio,
    val level: LwaTextFieldUio,
    val occupation: LwaCheckBoxUio,
    val used: Boolean,
)

@Stable
object GMSkillFieldDefault {
    @Stable
    val spacing: DpSize = DpSize(4.dp, 4.dp)
}

@Composable
fun GMSkillField(
    modifier: Modifier = Modifier,
    spacing: DpSize = GMSkillFieldDefault.spacing,
    skill: GMSkillFieldUio,
    onDelete: ((GMSkillFieldUio) -> Unit)?,
    onOccupation: (GMSkillFieldUio) -> Unit,
) {
    val focus = LocalFocusManager.current
    val showMenu = remember { mutableStateOf(false) }
    val onDismissRequest = remember {
        {
            focus.clearFocus(force = true)
            showMenu.value = false
        }
    }

    Row(
        modifier = modifier,
        horizontalArrangement = Arrangement.spacedBy(space = spacing.width),
    ) {
        Column(
            modifier = Modifier.weight(weight = 1f),
            verticalArrangement = Arrangement.spacedBy(space = spacing.height),
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(space = spacing.width),
            ) {
                LwaTextField(
                    modifier = Modifier.weight(weight = 1f),
                    field = skill.id,
                )
                LwaTextField(
                    modifier = Modifier.weight(weight = 1f),
                    field = skill.label,
                )
            }
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(space = spacing.width),
            ) {
                LwaTextField(
                    modifier = Modifier.weight(weight = 1f),
                    field = skill.base,
                )
                LwaTextField(
                    modifier = Modifier.weight(weight = 1f),
                    field = skill.bonus,
                )
                LwaTextField(
                    modifier = Modifier.weight(weight = 1f),
                    field = skill.level,
                )
            }
            skill.description?.let { description ->
                LwaTextField(
                    modifier = Modifier
                        .fillMaxWidth()
                        .moveFocusOnTab(focusManager = LocalFocusManager.current),
                    field = description,
                    singleLine = false,
                )
            }
        }
        Box {
            IconButton(
                onClick = { showMenu.value = showMenu.value.not() },
            ) {
                Icon(
                    imageVector = Icons.Default.MoreVert,
                    tint = MaterialTheme.colors.primary,
                    contentDescription = null,
                )
            }
            DropdownMenu(
                expanded = showMenu.value,
                offset = DpOffset(x = (-48).dp - spacing.width, y = (-48).dp),
                onDismissRequest = onDismissRequest,
            ) {
                DropdownMenuItem(
                    modifier = modifier,
                    enabled = onDelete != null,
                    onClick = {
                        onDismissRequest()
                        onDelete?.invoke(skill)
                    },
                ) {
                    Icon(
                        imageVector = Icons.Default.Delete,
                        tint = MaterialTheme.colors.primary,
                        contentDescription = null,
                    )
                    Text(
                        modifier = Modifier.padding(start = 8.dp),
                        text = "Supprimer",
                    )
                }
                DropdownMenuItem(
                    modifier = modifier,
                    onClick = {
                        onDismissRequest()
                        onOccupation(skill)
                    },
                ) {
                    LwaCheckBox(
                        modifier = Modifier.size(size = 24.dp),
                        colors = LwaCheckboxColors(disabledColor = MaterialTheme.colors.primary),
                        field = skill.occupation,
                        enabled = false,
                    )
                    Text(
                        modifier = Modifier.padding(start = 8.dp),
                        text = "Compétence d'occupation",
                    )
                }
            }
        }
    }
}