local battleGeneral = require("scripts/battleGeneral")
local battleMenu = require("scripts/battleMenu")

local onlinePlay = require("scripts/onlinePlay")

local textFiles = require("scripts/textFiles")

local maskCollection,shop

local menus = {}


menus.startMenu = battleMenu.createMenu{
    format = {
        hasBackground = true,hasBox = true,offsetY = 600,elementGapY = 6,
        boxImage = Graphics.loadImageResolved("maskCollection/resources/menu/box.png"),
    },
    optionFormat = {hasBox = false,textScale = 2},
    textFormat = {hasBox = false,textScale = 2,textMaxWidth = 400},
}

menus.startMenu.openFunc = function(menu)
    local text = textFiles.maskCollection.menus.start

    if onlinePlay.currentMode == onlinePlay.MODE_CLIENT then
        text = text.client

        menu:addText{text = text.description}

        menu:addOption{text = text.accept,runFunction = function(option)
            menu:close()
        end}
    else
        text = (maskCollection.saveData.saveFile ~= nil and text.continue) or text.begin

        menu:addText{text = text.description}

        menu:addOption{text = text.accept,runFunction = function(option)
            maskCollection.begin()

            if menu.isOpen then
                menu:close()
            end
        end}

        menu:addOption{text = text.reject,runFunction = function(option)
            menu:close()
        end}
    end
end


menus.upgradeMenu = battleMenu.createMenu{
    format = {
        hasBackground = true,hasBox = true,offsetY = 600,elementGapY = 6,
        boxImage = Graphics.loadImageResolved("maskCollection/resources/menu/box.png"),
    },
    optionFormat = {hasBox = false,textScale = 2},
    textFormat = {hasBox = false,textScale = 2,textMaxWidth = 512},
}

menus.upgradeMenu.openFunc = function(menu)
    local text = textFiles.maskCollection.menus.upgrade
    local upgradeText = text[menu.args.upgrade]
    
    local playerIdx = (battleMenu.controllingPlayer ~= nil and battleMenu.controllingPlayer.idx) or 0

    menu:addText{text = string.format("<image 'maskCollection/resources/menu/upgradeThumbnails/%s.png'>",menu.args.upgrade)}
    menu:addText{text = textFiles.funcs.replace(upgradeText.description,{PRICE = menu.args.price})}

    if playerIdx == 0 then
        return
    end

    if shop.ownsUpgrade(playerIdx,menu.args.upgrade) then
        if shop.upgradeEquipped(playerIdx,menu.args.upgrade) then
            menu:addOption{text = text.unequip,runFunction = function(option)
                shop.toggleUpgrade(playerIdx,menu.args.upgrade, false)

                SFX.play(35)
                menu:close()
            end}
        else
            menu:addOption{text = text.equip,runFunction = function(option)
                shop.toggleUpgrade(playerIdx,menu.args.upgrade, true)

                SFX.play(14)
                menu:close()
            end}
        end
    else
        menu:addOption{text = text.purchase,runFunction = function(option)
            if shop.tryPurchaseUpgrade(playerIdx,menu.args.upgrade) then
                SFX.play(14)
                menu:close()
            else
                SFX.play(shop.invalidSound)
            end
        end}
    end

    menu:addOption{text = text.cancel,runFunction = function(option)
        menu:close()
    end}
end


function menus.onInitAPI()
    maskCollection = require("maskCollection/scripts/maskCollection")
    shop = require("maskCollection/scripts/shop")


    function maskCollection.onGameStateChange()
        if maskCollection.gameState ~= maskCollection.GAME_STATE.WAITING_ROOM and (menus.startMenu.isOpen or menus.upgradeMenu.isOpen) then
            battleMenu.closeAll()
        end
    end

    function maskCollection.onClientsideActivation()
        battleMenu.menuFormatDefaults.boxImage = Graphics.loadImageResolved("maskCollection/resources/menu/box.png")
        battleMenu.optionFormatDefaults.boxImage = battleMenu.menuFormatDefaults.boxImage
        battleMenu.descriptionSettings.boxImage = battleMenu.menuFormatDefaults.boxImage

        battleMenu.backgroundSettings.image = Graphics.loadImageResolved("maskCollection/resources/menu/background.png")

        battleMenu.optionFormatDefaults.textColor[1][2] = Color.fromHexRGB(0xFF3869)
        battleMenu.optionFormatDefaults.textColor[2][2] = Color.fromHexRGB(0x7F1C34)

        battleGeneral.useReducedHubPauseMenu = true

        battleMenu.closeAll()
    end

    function maskCollection.onClientsideDeactivation()
        battleMenu.menuFormatDefaults.boxImage = Graphics.loadImageResolved("resources/menu/box.png")
        battleMenu.optionFormatDefaults.boxImage = battleMenu.menuFormatDefaults.boxImage
        battleMenu.descriptionSettings.boxImage = battleMenu.menuFormatDefaults.boxImage

        battleMenu.backgroundSettings.image = Graphics.loadImageResolved("resources/menu/background.png")

        battleMenu.optionFormatDefaults.textColor[1][2] = Color(1,1,0.25)
        battleMenu.optionFormatDefaults.textColor[2][2] = Color(0.5,0.5,0)

        battleGeneral.useReducedHubPauseMenu = false

        battleMenu.closeAll()
    end
end


return menus