package com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon

import com.pixelized.desktop.lwa.ui.composable.tooltip.BasicTooltipUio
import com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.common.CharacterRibbonAlterationUio
import com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.common.CharacterRibbonPortraitUio
import com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.common.CharacterRibbonUio
import com.pixelized.desktop.lwa.utils.extention.foldInto
import com.pixelized.shared.lwa.model.AlteredCharacterSheetFactory
import com.pixelized.shared.lwa.model.alteration.Alteration
import com.pixelized.shared.lwa.model.alteration.FieldAlteration
import com.pixelized.shared.lwa.model.characterSheet.CharacterSheet

private const val DEFAULT_ICON = "https://bg3.wiki/w/images/a/af/Arcana_Icon.png"

class CharacterRibbonFactory(
    private val alteredCharacterSheetFactory: AlteredCharacterSheetFactory,
) {
    fun convertToPlayerPortraitUio(
        characterSheet: CharacterSheet?,
        alterations: List<Alteration>,
        fieldAlterations: Map<String, List<FieldAlteration>>,
        hideOverruled: Boolean,
        enableCharacterSheet: Boolean,
        enableCharacterStats: Boolean,
        enableCharacterStatus: Boolean,
    ): CharacterRibbonUio? {
        if (characterSheet == null) return null

        val alteredCharacterSheet = alteredCharacterSheetFactory.sheet(
            characterSheet = characterSheet,
            alterations = fieldAlterations,
        )

        return CharacterRibbonUio(
            characterSheetId = characterSheet.id,
            hideOverruled = hideOverruled,
            enableDetail = enableCharacterSheet,
            portrait = CharacterRibbonPortraitUio(
                portrait = alteredCharacterSheet.thumbnail,
                name = alteredCharacterSheet.name,
                levelUp = alteredCharacterSheet.shouldLevelUp,
                stats = takeIf { enableCharacterStats }?.let {
                    CharacterRibbonPortraitUio.StatsDetail(
                        hp = alteredCharacterSheet.maxHp - alteredCharacterSheet.damage,
                        maxHp = alteredCharacterSheet.maxHp,
                        pp = alteredCharacterSheet.maxPp - alteredCharacterSheet.fatigue,
                        maxPp = alteredCharacterSheet.maxPp,
                    )
                },
            ),
            status = takeIf { enableCharacterStatus }?.let {
                alterations.map { alteration ->
                    CharacterRibbonAlterationUio(
                        icon = alteration.metadata.icon ?: DEFAULT_ICON,
                        tooltips = BasicTooltipUio(
                            title = alteration.metadata.name,
                            description = alteration.metadata.description,
                        ),
                    )
                }.foldInto(
                    size = 4,
                )
            } ?: emptyList(),
        )
    }
}