local BoxHeroUI = class("BoxHeroUI", BaseUI)

local TITLE_TEXT = {
	[GConst.SummonConst.SUMMON_TYPE.LV_1] = I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_1), -- 普通宝箱
	[GConst.SummonConst.SUMMON_TYPE.LV_2] = I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_2), -- 精致宝箱
	[GConst.SummonConst.SUMMON_TYPE.LV_3] = I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_3), -- 珍贵宝箱
}
local ICON_NAME = {
	[GConst.SummonConst.SUMMON_TYPE.LV_1] = "shop_chest_1",
	[GConst.SummonConst.SUMMON_TYPE.LV_2] = "shop_chest_2",
	[GConst.SummonConst.SUMMON_TYPE.LV_3] = "shop_chest_3"
}
local MAX_ITEM_NUM = 4

function BoxHeroUI:ctor(params)
	params = params or {}

	self.summonType = params.type
	self.arenaBoxInfo = params.arena
end

function BoxHeroUI:isFullScreen()
    return false
end

function BoxHeroUI:onPressBackspace()
	self:closeUI()
end

function BoxHeroUI:getPrefabPath()
    return "assets/prefabs/ui/shop/box_hero_ui.prefab"
end

function BoxHeroUI:onLoadRootComplete()
    self.uiMap = self.root:genAllChildren()
	self.uiMap["box_hero_ui.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

	self.titleTx = self.uiMap["box_hero_ui.bg.title"]
	self.boxImg = self.uiMap["box_hero_ui.bg.box"]

	-- 购买按钮
	self.buyBtn = self.uiMap["box_hero_ui.bg.buy_btn"]
	self.buyBtnTx = self.uiMap["box_hero_ui.bg.buy_btn.text"]
	self.buyBtnIcon = self.uiMap["box_hero_ui.bg.buy_btn.icon"]
	-- 视频按钮
	self.adBtn = self.uiMap["box_hero_ui.bg.ad_btn"]
	self.adBtnTx = self.uiMap["box_hero_ui.bg.ad_btn.tx_ad"]
	self.adBtnIcon = self.uiMap["box_hero_ui.bg.ad_btn.img_ad"]
	-- tag提示
	self.tag = self.uiMap["box_hero_ui.bg.item_node.item_4.tag"]
	self.txTag = self.uiMap["box_hero_ui.bg.item_node.item_4.tag.tx_tag"]

	self.itemNodeLayout = self.uiMap["box_hero_ui.bg.item_node"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)
	self.itemNodeList = {}
	self.itemCellList = {}
	self.itemTextList = {}
	self.itemHelpList = {}
	for i = 1, MAX_ITEM_NUM do
		table.insert(self.itemNodeList, self.uiMap["box_hero_ui.bg.item_" .. i])
		table.insert(self.itemCellList, CellManager:addCellComp(self.uiMap["box_hero_ui.bg.item_".. i .. ".item_cell"], GConst.TYPEOF_LUA_CLASS.ITEM_CELL))
		table.insert(self.itemTextList, self.uiMap["box_hero_ui.bg.item_".. i .. ".num"])
		table.insert(self.itemHelpList, self.uiMap["box_hero_ui.bg.item_" .. i .. ".help"])
	end

	self.adBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_36))
	GFunc.centerImgAndTx(self.adBtnIcon, self.adBtnTx, 5)

	-- 奖励界面打开时 关闭此界面
    self:addEventListener(EventManager.CUSTOM_EVENT.CLOSE_BOX_HERO_UI, function()
		if self.arenaBoxInfo then
			self:closeUI()
		else
			self:onRefresh()
		end
    end)
end

function BoxHeroUI:onRefresh()
	self.buyBtn:setActive(false)
	self.adBtn:setActive(false)
	self.tag:setActive(false)

	if self.summonType then
		self:refreshCummonBox()
	elseif self.arenaBoxInfo then
		self:refreshArenaAdBox()
	end
end

-- 刷新商城宝箱
function BoxHeroUI:refreshCummonBox()
	self.buyBtn:setActive(true)
	self.buyBtn:addClickListener(function()
		self:onClickSummon()
	end)

	self.titleTx:setText(TITLE_TEXT[self.summonType])
	self.boxImg:setSprite(GConst.ATLAS_PATH.SHOP, ICON_NAME[self.summonType])

	self:showRewards(DataManager.SummonData:getSummonTotalRewardByLv(self.summonType))-- 本次实际奖励

	local costItem, cost = DataManager.SummonData:getSummonCost(self.summonType)
	if GFunc.checkCost(costItem.id, costItem.num, false) then
		self.buyBtnIcon:setSprite(GFunc.getIconRes(costItem.id))
		self.buyBtnTx:setText(costItem.num)
	else
		self.buyBtnIcon:setSprite(GFunc.getIconRes(cost.id))
		self.buyBtnTx:setText(cost.num)
	end
	GFunc.centerImgAndTx(self.buyBtnIcon, self.buyBtnTx, 5)
end

-- 刷新竞技场ad宝箱
function BoxHeroUI:refreshArenaAdBox()
	self.adBtn:setActive(true)
	self.adBtn:addClickListener(function()
		SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.ARENA_AD_BOX, function ()
			ModuleManager.ArenaManager:reqAdBoxReward(self.arenaBoxInfo.isWin)
		end)
	end)

	self.tag:setActive(true)
	self.txTag:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_35))

	if self.arenaBoxInfo.isWin then
		self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_33))
		self.boxImg:setSprite(GConst.ATLAS_PATH.SHOP, "shop_chest_5")
	else
		self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_34))
		self.boxImg:setSprite(GConst.ATLAS_PATH.SHOP, "shop_chest_4")
	end
	self:showRewards(DataManager.ArenaData:getAdBoxRewards(self.arenaBoxInfo.isWin))
end

function BoxHeroUI:showRewards(rewards)
	for i = 1, MAX_ITEM_NUM do
		if i <= #rewards then
			self.itemNodeList[i]:setVisible(true)
			self.itemCellList[i]:refreshByCfg(rewards[i].id, 0)
			self.itemCellList[i]:setNum("")
			self.itemTextList[i]:setText(rewards[i].num)
			self.itemCellList[i]:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(rewards[i].id, GConst.REWARD_TYPE.ITEM, self.itemCellList[i]:getBaseObject())
			end)

			local type = ConfigManager:getConfig("item")[rewards[i].id].type
			if type == 3 or type == 6 then
				self.itemHelpList[i]:setVisible(true)
				self.itemHelpList[i]:addClickListener(function()
					ModuleManager.TipsManager:showHeroFragmentTips(rewards[i].id)
				end)
			else
				self.itemHelpList[i]:setVisible(false)
			end
		else
			self.itemNodeList[i]:setVisible(false)
		end
	end
	self.itemNodeLayout:RefreshLayout()
end

function BoxHeroUI:onClickSummon()
	local costItem, cost = DataManager.SummonData:getSummonCost(self.summonType)
	if GFunc.checkCost(costItem.id, costItem.num, false) then
		ModuleManager.SummonManager:summon(self.summonType, 1)
	elseif GFunc.checkCost(cost.id, cost.num, true) then
		ModuleManager.SummonManager:summon(self.summonType, 2)
	end
end

return BoxHeroUI