package com.pixelized.shared.lwa.model.characterSheet.factory

import com.pixelized.shared.lwa.model.characterSheet.CharacterSheet
import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetJsonV1
import com.pixelized.shared.lwa.usecase.CharacterSheetUseCase

class CharacterSheetJsonV1Factory(
    private val characterSheetUseCase: CharacterSheetUseCase,
) {
    fun convertFromJson(
        json: CharacterSheetJsonV1,
    ): CharacterSheet = characterSheetUseCase.run {
        CharacterSheet(
            id = json.id,
            name = json.name,
            job = json.job ?: "",
            portrait = json.portrait,
            thumbnail = json.thumbnail,
            externalLink = json.externalLink,
            level = json.level,
            shouldLevelUp = json.shouldLevelUp ?: false,
            strength = json.strength,
            dexterity = json.dexterity,
            constitution = json.constitution,
            height = json.height,
            intelligence = json.intelligence,
            power = json.power,
            charisma = json.charisma,
            damage = json.damage ?: 0,
            fatigue = json.fatigue ?: 0,
            diminished = json.diminished ?: 0,
            alterations = json.alterations ?: emptyList(),
            commonSkills = json.skills.map {
                CharacterSheet.Skill(
                    id = it.id,
                    label = it.label,
                    description = it.description,
                    base = it.base,
                    bonus = it.bonus,
                    level = it.level,
                    occupation = it.occupation,
                    used = it.used,
                )
            },
            specialSkills = json.occupations.map {
                CharacterSheet.Skill(
                    id = it.id,
                    label = it.label,
                    description = it.description,
                    base = it.base,
                    bonus = it.bonus,
                    level = it.level,
                    occupation = it.occupation,
                    used = it.used,
                )
            },
            magicSkills = json.magics.map {
                CharacterSheet.Skill(
                    id = it.id,
                    label = it.label,
                    description = it.description,
                    base = it.base,
                    bonus = it.bonus,
                    level = it.level,
                    occupation = it.occupation,
                    used = it.used,
                )
            },
            actions = json.rolls.map {
                CharacterSheet.Roll(
                    id = it.id,
                    label = it.label,
                    description = it.description,
                    canBeCritical = it.canBeCritical,
                    default = it.roll,
                    special = it.special,
                    critical = it.critical,
                )
            },
            tags = json.tag ?: emptyList(),
        )
    }
}