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

import com.pixelized.shared.lwa.model.characterSheet.CharacterSheet
import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetJson
import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetJsonV1
import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetPreview
import com.pixelized.shared.lwa.protocol.rest.CharacterPreviewJson


class CharacterSheetJsonFactory(
    private val v1: CharacterSheetJsonV1Factory,
) {
    fun convertFromJson(
        json: CharacterSheetJson,
    ): CharacterSheet {
        return when (json) {
            is CharacterSheetJsonV1 -> v1.convertFromJson(json = json)
        }
    }

    // Json conversion.

    fun convertToJson(
        sheet: CharacterSheet,
    ): CharacterSheetJson {
        val json = CharacterSheetJsonV1(
            id = sheet.id,
            name = sheet.name,
            job = sheet.job,
            portrait = sheet.portrait?.takeIf { it.isNotBlank() },
            thumbnail = sheet.thumbnail?.takeIf { it.isNotBlank() },
            externalLink = sheet.externalLink,
            level = sheet.level,
            shouldLevelUp = sheet.shouldLevelUp,
            strength = sheet.strength,
            dexterity = sheet.dexterity,
            constitution = sheet.constitution,
            height = sheet.height,
            intelligence = sheet.intelligence,
            power = sheet.power,
            charisma = sheet.charisma,
            damage = sheet.damage,
            fatigue = sheet.fatigue,
            diminished = sheet.diminished,
            alterations = sheet.alterations,
            skills = sheet.commonSkills.map {
                CharacterSheetJsonV1.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,
                )
            },
            occupations = sheet.specialSkills.map {
                CharacterSheetJsonV1.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,
                )
            },
            magics = sheet.magicSkills.map {
                CharacterSheetJsonV1.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,
                )
            },
            rolls = sheet.actions.map {
                CharacterSheetJsonV1.Roll(
                    id = it.id,
                    label = it.label,
                    description = it.description,
                    canBeCritical = it.canBeCritical,
                    roll = it.default,
                    special = it.special,
                    critical = it.critical
                )
            },
            tag = sheet.tags,
        )
        return json
    }

    fun convertToPreview(
        sheet: CharacterSheet,
    ): CharacterSheetPreview {
        return CharacterSheetPreview(
            characterSheetId = sheet.id,
            name = sheet.name,
            portrait = sheet.portrait,
            thumbnail = sheet.thumbnail,
            job = sheet.job,
            level = sheet.level,
            externalLink = sheet.externalLink,
            tags = sheet.tags,
        )
    }

    fun convertFromJson(
        json: CharacterPreviewJson,
    ): CharacterSheetPreview {
        return CharacterSheetPreview(
            characterSheetId = json.id,
            name = json.name,
            portrait = json.portrait,
            thumbnail = json.thumbnail,
            job = json.job ?: "",
            level = json.level,
            externalLink = json.externalLink,
            tags = json.tags,
        )
    }

    fun convertToPreviewJson(
        sheet: CharacterSheet,
    ): CharacterPreviewJson {
        return CharacterPreviewJson(
            id = sheet.id,
            portrait = sheet.portrait,
            thumbnail = sheet.thumbnail,
            name = sheet.name,
            job = sheet.job,
            level = sheet.level,
            externalLink = sheet.externalLink,
            tags = sheet.tags,
        )
    }
}