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

import com.pixelized.shared.lwa.model.campaign.Campaign
import com.pixelized.shared.lwa.model.campaign.CampaignJson
import com.pixelized.shared.lwa.model.campaign.CampaignJsonV1
import com.pixelized.shared.lwa.model.campaign.CampaignJsonV2

class CampaignJsonFactory(
    private val v1: CampaignJsonV1Factory,
    private val v2: CampaignJsonV2Factory,
) {
    fun convertFromJson(
        json: CampaignJson,
    ): Campaign {
        return when (json) {
            is CampaignJsonV1 -> v1.convertFromJson(campaignJson = json)
            is CampaignJsonV2 -> v2.convertFromJson(campaignJson = json)
        }
    }

    fun convertFromJson(
        json: CampaignJson.SceneJson,
    ): Campaign.Scene {
        return when (json) {
            is CampaignJsonV1.SceneJsonV1 -> v1.convertFromJson(sceneJson = json)
            is CampaignJsonV2.SceneJsonV2 -> v2.convertFromJson(sceneJson = json)
        }
    }

    fun convertFromJson(
        json: CampaignJson.MiniMap
    ): Campaign.MiniMap {
        return when (json) {
            is CampaignJsonV2.MiniMapJson -> v2.convertFromJson(mapJson = json)
        }
    }

    // Json conversion.

    fun createScene(
        title: String,
    ): CampaignJsonV2.SceneJsonV2 {
        return CampaignJsonV2.SceneJsonV2(
            name = title,
        )
    }

    fun convertToJson(
        campaign: Campaign,
    ): CampaignJson {
        return CampaignJsonV2(
            characters = campaign.characters,
            npcs = campaign.npcs,
            map = campaign.map?.let { campaign ->
                CampaignJsonV2.MiniMapJson(
                    id = campaign.id,
                    camera = campaign.camera?.let { camera ->
                        CampaignJsonV2.MiniMapJson.CameraJson(
                            zoom = camera.zoom,
                            offsetX = camera.offsetX,
                            offsetY = camera.offsetY,
                        )
                    }
                )
            },
            scene = CampaignJsonV2.SceneJsonV2(
                name = campaign.scene.name,
            ),
            options = CampaignJsonV2.OptionsJsonV2(
                showPlayer = campaign.options.showParty,
                showNpcs = campaign.options.showNpcs,
            )
        )
    }
}