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

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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Button
import androidx.compose.material.Icon
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import com.pixelized.desktop.lwa.ui.composable.error.ErrorSnackHandler
import com.pixelized.desktop.lwa.ui.composable.textfield.LwaTextFieldUio
import com.pixelized.desktop.lwa.ui.navigation.screen.LocalScreenController
import com.pixelized.desktop.lwa.ui.navigation.screen.destination.gamemaster.navigateToGameMasterAlterationEditPage
import com.pixelized.desktop.lwa.ui.screen.admin.common.GMFilterHeader
import com.pixelized.desktop.lwa.ui.screen.admin.common.tag.GMTagUio
import com.pixelized.desktop.lwa.ui.theme.color.component.LwaButtonColors
import kotlinx.coroutines.launch
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.game_master__alteration__create
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel

@Composable
fun GMAlterationPage(
    viewModel: GMAlterationViewModel = koinViewModel(),
) {
    val screen = LocalScreenController.current
    val scope = rememberCoroutineScope()

    val alterations = viewModel.alterations.collectAsState()
    val tags = viewModel.tags.collectAsState()

    Box {
        GMAlterationContent(
            modifier = Modifier.fillMaxSize(),
            filter = viewModel.filter,
            tags = tags,
            alterations = alterations,
            onTag = viewModel::onTag,
            onAlterationEdit = {
                screen.navigateToGameMasterAlterationEditPage(alterationId = it)
            },
            onAlterationDelete = {
                scope.launch {
                    viewModel.deleteAlteration(alterationId = it)
                }
            },
            onAlterationCreate = {
                screen.navigateToGameMasterAlterationEditPage(alterationId = null)
            },
        )

        ErrorSnackHandler(
            error = viewModel.error,
        )
    }
}

@Composable
private fun GMAlterationContent(
    modifier: Modifier = Modifier,
    padding: Dp = 8.dp,
    spacing: Dp = 8.dp,
    filter: LwaTextFieldUio,
    tags: State<List<GMTagUio>>,
    alterations: State<List<GMAlterationUio>>,
    onTag: (String) -> Unit,
    onAlterationEdit: (String) -> Unit,
    onAlterationDelete: (String) -> Unit,
    onAlterationCreate: () -> Unit,
) {
    Column(
        modifier = modifier,
    ) {
        Surface(
            modifier = Modifier.zIndex(1f), // avoid display issue with LazyColumnScope.animateItem()
            elevation = 1.dp,
        ) {
            GMFilterHeader(
                padding = padding,
                spacing = spacing,
                filter = filter,
                tags = tags,
                onTag = onTag,
            )
        }
        Box(
            modifier = Modifier.fillMaxWidth().weight(1f),
        ) {
            LazyColumn(
                modifier = Modifier.matchParentSize(),
                contentPadding = remember {
                    PaddingValues(
                        start = padding,
                        top = padding,
                        end = padding,
                        bottom = padding + 48.dp + padding,
                    )
                },
                verticalArrangement = Arrangement.spacedBy(space = spacing),
            ) {
                items(
                    items = alterations.value,
                    key = { it.alterationId },
                ) { alteration ->
                    GMAlteration(
                        modifier = Modifier
                            .fillMaxWidth()
                            .animateItem(),
                        alteration = alteration,
                        onAlteration = {
                            onAlterationEdit(alteration.alterationId)
                        },
                        onDelete = {
                            onAlterationDelete(alteration.alterationId)
                        },
                        onTag = onTag,
                    )
                }
            }
            Row(
                modifier = Modifier
                    .align(alignment = Alignment.BottomEnd)
                    .padding(all = padding),
                horizontalArrangement = Arrangement.SpaceBetween,
            ) {
                Button(
                    colors = LwaButtonColors(),
                    shape = CircleShape,
                    onClick = onAlterationCreate,
                ) {
                    Text(
                        modifier = Modifier.padding(end = 4.dp),
                        text = stringResource(Res.string.game_master__alteration__create),
                    )
                    Icon(
                        imageVector = Icons.Default.Add,
                        contentDescription = null,
                    )
                }
            }
        }
    }
}