local BoxOpenUI = class("BoxOpenUI", BaseUI)
local AUDIO_DELAY_TIME = 0.4
local SPINE_OPEN_TIME = 1.4
local DEFAULT_SPINE_NAME = "ui_shop_box_1" -- 默认的展示动画

local SUMMON_SPINE_NAME = {
    [1] = "ui_shop_box_1",
    [2] = "ui_shop_box_2",
    [3] = "ui_shop_box_3",
}
local BOUNTY_SPINE_NAME = {
    [8] = "ui_bounty_chestopen_1",
    [9] = "ui_bounty_chestopen_2",
    [11] = "ui_bounty_chestopen_3",
    [18] = "ui_bounty_chestopen_4",
    [10] = "ui_bounty_chestopen_5",
}
local ARENA_BOUNTY_SPINE_NAME = {
    [8] = "ui_bounty_chestopen_1",
    [9] = "ui_bounty_chestopen_2",
    [11] = "ui_bounty_chestopen_3",
    [18] = "ui_bounty_chestopen_4",
    [10] = "ui_bounty_chestopen_5",
}

local ARENA_AD_BOX_SPINE_NAME = {
    [1] = "ui_arena_adchest_1",
    [2] = "ui_arena_adchest_2",
}

function BoxOpenUI:ctor(params)
    self.params = params or {} -- 将信息传递给下一个界面
    self.type = self.params and self.params.type 
    self.index = self.params.params
end

function BoxOpenUI:isFullScreen()
    return true
end

function BoxOpenUI:getPrefabPath()
    return "assets/prefabs/ui/shop/box_open_ui.prefab"
end

function BoxOpenUI:onLoadRootComplete()
    self.uiMap = self.root:genAllChildren()
    self.bg = self.uiMap["box_open_ui.bg"]
    -- 奖励界面打开时 关闭此界面
    self:addEventListener(EventManager.CUSTOM_EVENT.CLOSE_BOX_OPEN_UI, function()
        self:closeUI()
    end)
    EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.CLOSE_BOX_HERO_UI)
    -- 播放spine表现 播放完毕后打开奖励UI
    self.spineObj = self.uiMap["box_open_ui.ui_spine_obj"]
    local spineName
    if self.type == GConst.ShopConst.BOX_REWARD_TYPE.SUMMON then
        spineName = SUMMON_SPINE_NAME[self.index]
    elseif self.type == GConst.ShopConst.BOX_REWARD_TYPE.BOUNTY then
        spineName = BOUNTY_SPINE_NAME[self.index]
    elseif self.type == GConst.ShopConst.BOX_REWARD_TYPE.ARENA_BOUNTY then
        spineName = ARENA_BOUNTY_SPINE_NAME[self.index]
    elseif self.type == GConst.ShopConst.BOX_REWARD_TYPE.ARENA_AD_BOX then
        spineName = ARENA_AD_BOX_SPINE_NAME[self.index]
    end
    if not spineName then
        spineName = DEFAULT_SPINE_NAME
    end
    self.spineObj:loadAssetAsync(spineName, function()
        self.spineObj:playAnim("born", false, true, true)
        self.spineObj:getSkeletonGraphic().enabled = true
        self:performWithDelayGlobal(function()
            ModuleManager.ShopManager:showBoxRewardUI(self.params)
        end, SPINE_OPEN_TIME)
    end, false)
    -- 音频
    self:performWithDelayGlobal(function()
        AudioManager:playEffect(AudioManager.EFFECT_ID.SUMMON_START)
    end, AUDIO_DELAY_TIME)
end

return BoxOpenUI