package com.pixelized.desktop.lwa.ui.screen.admin.item.list

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
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.MoreVert
import androidx.compose.material.minimumInteractiveComponentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.screen.admin.common.tag.GMTag
import com.pixelized.desktop.lwa.ui.screen.admin.common.tag.GMTagUio
import com.pixelized.desktop.lwa.ui.theme.lwa
import kotlinx.coroutines.launch
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.game_master__item__delete
import lwacharactersheet.composeapp.generated.resources.ic_delete_forever_24dp
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource

@Stable
data class GMItemUio(
    val itemId: String,
    val label: String,
    val tags: List<GMTagUio>,
)

@Stable
object GMItemDefault {
    @Stable
    val padding = PaddingValues(start = 16.dp)
    @Stable
    val spacing = 4.dp
}

@Composable
fun GMItem(
    modifier: Modifier = Modifier,
    padding: PaddingValues = GMItemDefault.padding,
    spacing: Dp = GMItemDefault.spacing,
    item: GMItemUio,
    onItem: () -> Unit,
    onDelete: () -> Unit,
    onTag: (String) -> Unit,
) {
    val state = rememberLazyListState()
    val scope = rememberCoroutineScope()
    Row(
        modifier = Modifier
            .clip(shape = MaterialTheme.lwa.shapes.gameMaster)
            .clickable(onClick = onItem)
            .background(color = MaterialTheme.lwa.colorScheme.elevated.base1dp)
            .minimumInteractiveComponentSize()
            .padding(paddingValues = padding)
            .then(other = modifier),
        horizontalArrangement = Arrangement.spacedBy(space = spacing),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Text(
            modifier = Modifier.weight(1f),
            style = MaterialTheme.lwa.typography.system.body1,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1,
            text = item.label,
        )
        LazyRow(
            modifier = Modifier
                .draggable(
                    orientation = Orientation.Horizontal,
                    state = rememberDraggableState { delta ->
                        scope.launch {
                            state.scrollBy(-delta)
                        }
                    },
                )
                .weight(1f),
            state = state,
            horizontalArrangement = Arrangement.spacedBy(space = 2.dp, alignment = Alignment.End),
        ) {
            items(
                items = item.tags,
                key = { it.id },
            ) { tag ->
                GMTag(
                    elevation = 4.dp,
                    tag = tag,
                    onTag = { onTag(tag.id) },
                )
            }
        }
        OverflowActionMenu(
            item = item,
            onDelete = onDelete,
        )
    }
}

@Composable
private fun OverflowActionMenu(
    modifier: Modifier = Modifier,
    item: GMItemUio,
    onDelete: () -> Unit,
) {
    val overflowMenu = remember(item) {
        mutableStateOf(false)
    }
    IconButton(
        modifier = modifier,
        onClick = { overflowMenu.value = true },
    ) {
        Icon(
            imageVector = Icons.Default.MoreVert,
            tint = MaterialTheme.colors.primary,
            contentDescription = null,
        )
        DropdownMenu(
            expanded = overflowMenu.value,
            onDismissRequest = {
                overflowMenu.value = false
            },
            content = {
                DropdownMenuItem(
                    onClick = {
                        overflowMenu.value = false
                        onDelete()
                    },
                ) {
                    Row(
                        horizontalArrangement = Arrangement.spacedBy(space = 8.dp),
                        verticalAlignment = Alignment.CenterVertically,
                    ) {
                        Icon(
                            painter = painterResource(Res.drawable.ic_delete_forever_24dp),
                            tint = MaterialTheme.lwa.colorScheme.base.primary,
                            contentDescription = null,
                        )
                        Text(
                            style = MaterialTheme.lwa.typography.system.body1,
                            color = MaterialTheme.lwa.colorScheme.base.primary,
                            text = stringResource(Res.string.game_master__item__delete),
                        )
                    }
                }
            },
        )
    }
}