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

import com.pixelized.shared.lwa.model.inventory.Inventory
import com.pixelized.shared.lwa.model.inventory.InventoryJsonV1

class InventoryJsonFactoryV1 {

    fun convertFromJson(json: InventoryJsonV1): Inventory {
        return Inventory(
            characterSheetId = json.characterSheetId,
            purse = Inventory.Purse(
                gold = json.purse.gold,
                silver = json.purse.silver,
                copper = json.purse.copper,
            ),
            items = json.items.map {
                Inventory.Item(
                    inventoryId = it.inventoryId,
                    itemId = it.itemId,
                    count = it.count,
                    equipped = it.equipped ?: false,
                )
            },
        )
    }

    fun convertToJson(inventory: Inventory): InventoryJsonV1 {
        return InventoryJsonV1(
            characterSheetId = inventory.characterSheetId,
            purse = InventoryJsonV1.PurseJson(
                gold = inventory.purse.gold,
                silver = inventory.purse.silver,
                copper = inventory.purse.copper,
            ),
            items = inventory.items.map {
                InventoryJsonV1.ItemJson(
                    inventoryId = it.inventoryId,
                    itemId = it.itemId,
                    count = it.count,
                    equipped = it.equipped,
                )
            },
        )
    }
}