package com.pixelized.desktop.lwa.repository.settings

import com.pixelized.desktop.lwa.repository.settings.model.Settings
import com.pixelized.desktop.lwa.repository.settings.model.SettingsJson
import com.pixelized.desktop.lwa.repository.settings.model.SettingsJsonV1
import com.pixelized.desktop.lwa.repository.settings.model.SettingsJsonV2
import com.pixelized.desktop.lwa.usecase.SettingsUseCase


class SettingsFactory(
    private val useCase: SettingsUseCase,
) {
    fun convertToJson(
        settings: Settings,
    ): SettingsJson {
        return SettingsJsonV2(
            host = settings.network.host,
            port = settings.network.port,
            playerName = settings.playerName,
            dynamicDice = settings.portrait.dynamicDice,
            dynamicDiceDelay = settings.portrait.dynamicDiceDelay,
            showChat = settings.chat.showChat,
            autoScrollChat = settings.chat.autoScrollChat,
            maxLineCount = settings.chat.maxLineCount,
            isAdmin = settings.isAdmin,
            isGameMaster = settings.isGameMaster,
        )
    }

    fun convertFromJson(
        json: SettingsJson,
    ): Settings {
        return when (json) {
            is SettingsJsonV1 -> convertFromJsonV1(json)
            is SettingsJsonV2 -> convertFromJsonV2(json)
        }
    }

    private fun convertFromJsonV1(
        json: SettingsJsonV1,
    ): Settings {
        val default = useCase.defaultSettings()
        return Settings(
            playerName = json.playerName ?: default.playerName,
            network = Settings.Network(
                host = json.host ?: default.network.host,
                port = json.port ?: default.network.port,
            ),
            portrait = Settings.Portrait(
                dynamicDice = json.dynamicDice ?: default.portrait.dynamicDice,
                dynamicDiceDelay = json.dynamicDiceDelay ?: default.portrait.dynamicDiceDelay,
            ),
            chat = Settings.Chat(
                showChat = default.chat.showChat,
                autoScrollChat = json.autoScrollChat ?: default.chat.autoScrollChat,
                maxLineCount = json.maxLineCount ?: default.chat.maxLineCount,
            ),
            isAdmin = json.isAdmin ?: default.isAdmin,
            isGameMaster = json.isGameMaster ?: default.isGameMaster,
        )
    }

    private fun convertFromJsonV2(
        json: SettingsJsonV2,
    ): Settings {
        val default = useCase.defaultSettings()
        return Settings(
            playerName = json.playerName ?: default.playerName,
            network = Settings.Network(
                host = json.host ?: default.network.host,
                port = json.port ?: default.network.port,
            ),
            portrait = Settings.Portrait(
                dynamicDice = json.dynamicDice ?: default.portrait.dynamicDice,
                dynamicDiceDelay = json.dynamicDiceDelay ?: default.portrait.dynamicDiceDelay,
            ),
            chat = Settings.Chat(
                showChat = json.showChat ?: default.chat.showChat,
                autoScrollChat = json.autoScrollChat ?: default.chat.autoScrollChat,
                maxLineCount = json.maxLineCount ?: default.chat.maxLineCount,
            ),
            isAdmin = json.isAdmin ?: default.isAdmin,
            isGameMaster = json.isGameMaster ?: default.isGameMaster,
        )
    }
}