package com.pixelized.desktop.lwa.ui.screen.admin.action

import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.pixelized.desktop.lwa.ui.composable.confirmation.ConfirmationDialog
import com.pixelized.desktop.lwa.ui.composable.error.ErrorSnackHandler
import com.pixelized.desktop.lwa.ui.theme.lwa
import kotlinx.coroutines.launch
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.ic_camping_24dp
import lwacharactersheet.composeapp.generated.resources.ic_edit_note_24dp
import lwacharactersheet.composeapp.generated.resources.ic_sync_24dp
import lwacharactersheet.composeapp.generated.resources.ic_visibility_24dp
import lwacharactersheet.composeapp.generated.resources.ic_visibility_off_24dp
import org.koin.compose.viewmodel.koinViewModel

@Stable
data class ActionPageUio(
    val party: Boolean,
    val npc: Boolean,
)

@Composable
fun GMActionPage(
    viewModel: GMActionViewModel = koinViewModel(),
) {
    val scope = rememberCoroutineScope()
    val scroll = rememberScrollState()
    val actions = viewModel.actions.collectAsStateWithLifecycle()
    val editDialog = viewModel.editDialog.collectAsStateWithLifecycle()
    val validationDialog = viewModel.validationDialog.collectAsStateWithLifecycle()

    GMActionContent(
        actions = actions,
        scroll = scroll,
        onServerSync = {
            scope.launch {
                viewModel.onServerSync()
            }
        },
        onEditSession = {
            scope.launch {
                viewModel.onEditSession()
            }
        },
        onPartyHeal = {
            scope.launch {
                viewModel.onPartyHeal()
            }
        },
        onPartyVisibility = {
            scope.launch {
                viewModel.onPlayerVisibility()
            }
        },
        onNpcVisibility = {
            scope.launch {
                viewModel.onNpcVisibility()
            }
        },
    )

    ErrorSnackHandler(
        error = viewModel.error,
    )

    ConfirmationDialog(
        dialog = validationDialog,
    )

    GMEditDialog(
        dialog = editDialog
    )
}

@Composable
fun GMActionContent(
    modifier: Modifier = Modifier,
    scroll: ScrollState,
    spacing: Dp = MaterialTheme.lwa.dimen.paddingValue,
    actions: State<ActionPageUio?>,
    onServerSync: () -> Unit,
    onEditSession: () -> Unit,
    onPartyHeal: () -> Unit,
    onPartyVisibility: () -> Unit,
    onNpcVisibility: () -> Unit,
) {
    Column(
        modifier = modifier
            .verticalScroll(state = scroll)
            .padding(vertical = spacing, horizontal = spacing),
        verticalArrangement = Arrangement.spacedBy(space = spacing),
    ) {
        GMAction(
            modifier = Modifier.fillMaxWidth(),
            icon = Res.drawable.ic_sync_24dp,
            label = "Synchronization du serveur",
            onAction = onServerSync,
        )
        GMAction(
            modifier = Modifier.fillMaxWidth(),
            icon = Res.drawable.ic_edit_note_24dp,
            label = "Edition du titre de session",
            onAction = onEditSession,
        )
        GMAction(
            modifier = Modifier.fillMaxWidth(),
            icon = Res.drawable.ic_camping_24dp,
            label = "Soigner le groupe de joueur",
            onAction = onPartyHeal,
        )
        actions.value?.party?.let { party ->
            GMAction(
                modifier = Modifier.fillMaxWidth(),
                icon = when (party) {
                    true -> Res.drawable.ic_visibility_off_24dp
                    else -> Res.drawable.ic_visibility_24dp
                },
                label = when (party) {
                    true -> "Cacher les Joueurs"
                    else -> "Afficher les Joueurs"
                },
                onAction = onPartyVisibility,
            )
        }
        actions.value?.npc?.let { npc ->
            GMAction(
                modifier = Modifier.fillMaxWidth(),
                icon = when (npc) {
                    true -> Res.drawable.ic_visibility_off_24dp
                    else -> Res.drawable.ic_visibility_24dp
                },
                label = when (npc) {
                    true -> "Cacher les NPCs"
                    else -> "Afficher les NPCs"
                },
                onAction = onNpcVisibility,
            )
        }
    }
}