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

local SUMMON_SPINE_NAME = {
    [1] = "ui_core_box_1",
    [2] = "ui_core_box_2",
}

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

function BoxOpenUI:showCommonBG()
	return false
end

function BoxOpenUI:isFullScreen()
    return true
end

function BoxOpenUI:getOpenSoundId()
	-- return AudioManager.CFG_ID.SHOP_BOX_SUMMON
end

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

function BoxOpenUI:getPreLoadList()
	return {
		[GConst.TYPEOF_UNITY_CLASS.TEXTURE_2D] = {
			"assets/arts/textures/background/core_box/core_box_bg_1.png",
			"assets/arts/textures/background/core_box/core_box_bg_1.png"
		}
	}
end

function BoxOpenUI:onClose()
	if self.spineObj then
		self.spineObj:killAniCompleteSeq()
	end
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)

	self.vfx = self.uiMap["box_open_ui.vfx_b13_ui_hunxin_b01"]
	self:hideVfx()

	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]
	end
	if not spineName then
		spineName = DEFAULT_SPINE_NAME
	end
	self.root:addClickListener(nil)
	self.spineObj:loadAssetAsync(spineName, function()
		self:showNextReward(self.rewardIndex)
	end, false)

	self.uiMap["box_open_ui.continue"]:setText(I18N:getGlobalText(I18N.GlobalConst.CLICK_CONTINUE_DESC))
	self.uiMap["box_open_ui.skip"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_SKIP))
	self.uiMap["box_open_ui.skip"]:addClickListener(function()
		AudioManager:stopEffect(AudioManager.EFFECT_ID.SUMMON)
		self.spineObj:killAniCompleteSeq()
		ModuleManager.ShopManager:showBoxRewardUI(self.params)
	end)
end

function BoxOpenUI:showNextReward(targetIndex)
	if not targetIndex then
		self.rewardIndex = self.rewardIndex + 1
	else
		self.rewardIndex = targetIndex
	end

	if self.rewardIndex > self.maxRewardIndex then -- 容错
		self.spineObj:killAniCompleteSeq()
		ModuleManager.ShopManager:showBoxRewardUI(self.params)
		return
	end

	self.uiMap["box_open_ui.continue"]:setVisible(false)
	local reward = self.params.rewards[self.rewardIndex]
	if not self.rewardCell then
		self.rewardCell = self.uiMap["box_open_ui.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	end
	self.rewardCell:refresh(reward)
	local nameObj = self.uiMap["box_open_ui.name"]
	local qltObj = self.uiMap["box_open_ui.qlt"]
	nameObj:setText(GFunc.getServerRewardName(reward))
	local qlt = GFunc.getServerRewardQlt(reward)
	local str = GFunc.getQltDesc(qlt, true)
	local config = ConfigManager:getConfig("core_soul")[reward.soul.cfg_id]
	qltObj:setText(str)

	self.rewardCell:getBaseObject():setVisible(false)
	nameObj:setVisible(false)
	qltObj:setVisible(false)
	self:hideVfx()

	if qlt ~= self.lastQlt then
		self.lastQlt = qlt
		-- if self.lastQlt >= GConst.SoulConst.QLT_TYPE.PURPLE then
		-- 	-- self.bg:setTexture("assets/arts/textures/background/shop/shop_bg_2.png")
		-- else
		-- 	-- self.bg:setTexture("assets/arts/textures/background/shop/shop_bg_1.png")
		-- end
	end

	AudioManager:playEffect(AudioManager.EFFECT_ID.SUMMON)
	-- 播放spine表现 播放完毕后打开奖励UI

	-- local lightObj = self.uiMap["box_open_ui.ui_spine_obj_light"]
	-- lightObj:setVisible(false)
	-- lightObj:setVisible(false)
	-- lightObj:playAnim("born", false, true, false)
	self.spineObj:getSkeletonGraphic().enabled = true

	self.aniName = "born02"
	if self.type == GConst.ShopConst.BOX_REWARD_TYPE.SUMMON then
		self.aniName = "born0" .. (qlt+1)
    end

	self.root:addClickListener(function()
		AudioManager:stopEffect(AudioManager.EFFECT_ID.SUMMON)
		self.root:addClickListener(nil)
		self.spineObj:playAnimCompleteOnPer(self.aniName, false, true, function()
			self.uiMap["box_open_ui.continue"]:setVisible(true)

			if self.rewardIndex >= self.maxRewardIndex then
				self.uiMap["box_open_ui.skip"]:setText(GConst.EMPTY_STRING)
			end

			self.root:addClickListener(function()
				if self.rewardIndex >= self.maxRewardIndex then
					self.spineObj:killAniCompleteSeq()
					ModuleManager.ShopManager:showBoxRewardUI(self.params)
				else
					self:showNextReward()
				end
			end)
		end, true, 0.6, function()
			self.rewardCell:getBaseObject():setVisible(true)
			nameObj:setVisible(true)
			qltObj:setVisible(true)

			if self.lastQlt >= GConst.SoulConst.QLT_TYPE.PURPLE then
				self:showVfx()
			else
				self:hideVfx()
			end
		end, 0.70)
	end)

	self.spineObj:playAnimComplete(self.aniName, false, true, nil, true, 1.8, function()
		self.rewardCell:getBaseObject():setVisible(true)
		nameObj:setVisible(true)
		qltObj:setVisible(true)

		if self.lastQlt >= GConst.SoulConst.QLT_TYPE.PURPLE then
			self:showVfx()
		else
			self:hideVfx()
		end
		self.uiMap["box_open_ui.continue"]:setVisible(true)

		if self.rewardIndex >= self.maxRewardIndex then
			self.uiMap["box_open_ui.skip"]:setText(GConst.EMPTY_STRING)
		end

		self.root:addClickListener(function()
			if self.rewardIndex >= self.maxRewardIndex then
				self.spineObj:killAniCompleteSeq()
				ModuleManager.ShopManager:showBoxRewardUI(self.params)
			else
				self:showNextReward()
			end
		end)
	end)
end

function BoxOpenUI:showVfx()
	self.vfx:setActive(true)
	self.vfx:play()
end

function BoxOpenUI:hideVfx()
	self.vfx:setActive(false)
end

return BoxOpenUI