package com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.sheet

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
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.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.composable.decoratedBox.DecoratedBox
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.sheet.item.CharacterDetailSheetAction
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.sheet.item.CharacterDetailSheetActionUio
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.sheet.item.CharacterDetailSheetCharacteristic
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.sheet.item.CharacterDetailSheetCharacteristicUio
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.sheet.item.CharacterDetailSheetSkill
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.sheet.item.CharacterDetailSheetSkillUio
import com.pixelized.desktop.lwa.ui.theme.lwa
import lwacharactersheet.composeapp.generated.resources.Res
import lwacharactersheet.composeapp.generated.resources.character_sheet__skills__common_title
import lwacharactersheet.composeapp.generated.resources.character_sheet__skills__magic_title
import lwacharactersheet.composeapp.generated.resources.character_sheet__skills__special_title
import org.jetbrains.compose.resources.stringResource

@Stable
data class CharacterDetailSheetUio(
    val characterSheetId: String,
    val characteristics: List<CharacterDetailSheetCharacteristicUio>,
    val commonSkills: List<CharacterDetailSheetSkillUio>,
    val specialSkills: List<CharacterDetailSheetSkillUio>,
    val magicSkills: List<CharacterDetailSheetSkillUio>,
    val actions: List<CharacterDetailSheetActionUio>,
)

@Stable
object CharacterDetailSheetDefault {
    @Stable
    val paddings = PaddingValues(horizontal = 16.dp, vertical = 8.dp)
}

@Composable
fun CharacterDetailSheet(
    modifier: Modifier = Modifier,
    paddings : PaddingValues = CharacterDetailSheetDefault.paddings,
    sheet: State<CharacterDetailSheetUio?>,
    onCharacteristic: (CharacterDetailSheetCharacteristicUio) -> Unit,
    onSkill: (CharacterDetailSheetSkillUio) -> Unit,
    onUseSkill: (CharacterDetailSheetSkillUio) -> Unit,
    onAction: (CharacterDetailSheetActionUio) -> Unit,
) {
    Row(
        modifier = modifier
            .verticalScroll(state = rememberScrollState())
            .padding(paddingValues = paddings),
        horizontalArrangement = Arrangement.spacedBy(space = 8.dp),
    ) {
        Column(
            verticalArrangement = Arrangement.spacedBy(space = 8.dp),
        ) {
            sheet.value?.characteristics?.forEach {
                CharacterDetailSheetCharacteristic(
                    modifier = Modifier.size(size = MaterialTheme.lwa.dimen.sheet.characteristic),
                    characteristic = it,
                    onClick = { onCharacteristic(it) },
                )
            }
        }
        Column(
            modifier = Modifier.fillMaxWidth(),
            verticalArrangement = Arrangement.spacedBy(space = 8.dp)
        ) {
            DecoratedBox(
                modifier = Modifier.fillMaxWidth(),
                padding = remember { PaddingValues(vertical = 8.dp) },
            ) {
                Column {
                    Text(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(bottom = 8.dp, top = 4.dp),
                        style = MaterialTheme.typography.caption,
                        textAlign = TextAlign.Center,
                        text = stringResource(Res.string.character_sheet__skills__common_title),
                    )
                    sheet.value?.commonSkills?.forEach { skill ->
                        CharacterDetailSheetSkill(
                            modifier = Modifier.fillMaxWidth(),
                            skill = skill,
                            onSkill = onSkill,
                            onUse = onUseSkill,
                        )
                    }
                }
            }
            AnimatedVisibility(
                visible = sheet.value?.specialSkills?.isNotEmpty() ?: false,
            ) {
                DecoratedBox(
                    modifier = Modifier.fillMaxWidth(),
                    padding = remember { PaddingValues(vertical = 8.dp) },
                ) {
                    Column {
                        Text(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 8.dp, top = 4.dp),
                            style = MaterialTheme.typography.caption,
                            textAlign = TextAlign.Center,
                            text = stringResource(Res.string.character_sheet__skills__special_title),
                        )
                        sheet.value?.specialSkills?.forEach { skill ->
                            CharacterDetailSheetSkill(
                                modifier = Modifier.fillMaxWidth(),
                                skill = skill,
                                onSkill = onSkill,
                                onUse = onUseSkill,
                            )
                        }
                    }
                }
            }
            AnimatedVisibility(
                visible = sheet.value?.magicSkills?.isNotEmpty() ?: false,
            ) {
                DecoratedBox(
                    modifier = Modifier.fillMaxWidth(),
                    padding = remember { PaddingValues(vertical = 8.dp) },
                ) {
                    Column {
                        Text(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 8.dp, top = 4.dp),
                            style = MaterialTheme.typography.caption,
                            textAlign = TextAlign.Center,
                            text = stringResource(Res.string.character_sheet__skills__magic_title),
                        )
                        sheet.value?.magicSkills?.forEach { skill ->
                            CharacterDetailSheetSkill(
                                modifier = Modifier.fillMaxWidth(),
                                skill = skill,
                                onSkill = onSkill,
                                onUse = onUseSkill,
                            )
                        }
                    }
                }
            }
            AnimatedVisibility(
                visible = sheet.value?.actions?.isNotEmpty() ?: false,
            ) {
                Column {
                    sheet.value?.actions?.forEach { action ->
                        CharacterDetailSheetAction(
                            modifier = Modifier.fillMaxWidth(),
                            action = action,
                            onClick = onAction,
                        )
                    }
                }
            }
        }
    }
}

