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

import androidx.compose.animation.animateContentSize
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.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.collectAsState
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.Dp
import androidx.compose.ui.unit.DpOffset
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.utils.extention.moveFocusOnTab

@Stable
data class GMActionFieldUio(
    val id: LwaTextFieldUio,
    val label: LwaTextFieldUio,
    val description: LwaTextFieldUio,
    val canBeCritical: LwaCheckBoxUio,
    val default: LwaTextFieldUio,
    val special: LwaTextFieldUio?,
    val critical: LwaTextFieldUio?,
)

@Stable
object GMActionFieldDefault {
    @Stable
    val spacing: Dp = 4.dp
}

@Composable
fun GMActionField(
    modifier: Modifier = Modifier,
    space: Dp = GMActionFieldDefault.spacing,
    action: GMActionFieldUio,
    onDelete: (GMActionFieldUio) -> Unit,
) {
    val focus = LocalFocusManager.current
    val showMenu = remember { mutableStateOf(false) }
    val onDismissRequest = remember {
        {
            focus.clearFocus(force = true)
            showMenu.value = false
        }
    }
    val canBeCritical = action.canBeCritical.checked.collectAsState()

    Column(
        modifier = modifier,
        verticalArrangement = Arrangement.spacedBy(space = space),
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.spacedBy(space = space),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            LwaTextField(
                modifier = Modifier.weight(1f),
                field = action.id,
            )
            LwaTextField(
                modifier = Modifier.weight(1f),
                field = action.label,
            )
            LwaCheckBox(
                field = action.canBeCritical,
            )
            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 - space, y = (-48).dp),
                    onDismissRequest = onDismissRequest,
                ) {
                    DropdownMenuItem(
                        modifier = modifier,
                        onClick = {
                            onDismissRequest()
                            onDelete(action)
                        },
                    ) {
                        Icon(
                            imageVector = Icons.Default.Delete,
                            tint = MaterialTheme.colors.primary,
                            contentDescription = null,
                        )
                        Text(
                            modifier = Modifier.padding(start = 8.dp),
                            text = "Supprimer",
                        )
                    }
                }
            }
        }
        Row(
            modifier = Modifier.fillMaxWidth().animateContentSize(),
            horizontalArrangement = Arrangement.spacedBy(space = space),
        ) {
            LwaTextField(
                modifier = Modifier.weight(1f),
                field = action.default,
            )
            if (canBeCritical.value) {
                action.special?.let { field ->
                    LwaTextField(
                        modifier = Modifier.weight(1f),
                        field = field,
                    )
                }
                action.critical?.let { field ->
                    LwaTextField(
                        modifier = Modifier.weight(1f),
                        field = field,
                    )
                }
            }
        }
        LwaTextField(
            modifier = Modifier
                .fillMaxWidth()
                .moveFocusOnTab(focusManager = LocalFocusManager.current),
            field = action.description,
            singleLine = false,
        )
    }
}