local BoxSellCell = class("BoxSellCell", BaseCell)

local BASE_CELL_HEIGHT = 522
local FULL_CELL_HEIGHT = 786
local HEIGHT_ADD = 30

function BoxSellCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.bg = uiMap["box_sell_cell.bg"]
	local boxNameTx1 = uiMap["box_sell_cell.bg.box_1.name_tx"]
	boxNameTx1:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_1)) -- 普通宝箱
	local boxDescTx1 = uiMap["box_sell_cell.bg.box_1.desc_tx"]
	boxDescTx1:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_24)) -- 必出<color=#5fff57>普通</color>英雄
	self.boxBuyBtn1 = uiMap["box_sell_cell.bg.box_1"]
	self.boxBuyBtnCoin1 = uiMap["box_sell_cell.bg.box_1.buy_btn.coin"]
	self.boxBuyBtnTx1 = uiMap["box_sell_cell.bg.box_1.buy_btn.buy_tx"]
	local boxNameTx2 = uiMap["box_sell_cell.bg.box_2.name_tx"]
	boxNameTx2:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_2)) -- 精致宝箱
	local boxDescTx2 = uiMap["box_sell_cell.bg.box_2.desc_tx"]
	boxDescTx2:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_25)) -- 必出<color=#8aedff>稀有</color>英雄
	self.boxBuyBtn2 = uiMap["box_sell_cell.bg.box_2"]
	self.boxBuyBtnCoin2 = uiMap["box_sell_cell.bg.box_2.buy_btn.coin"]
	self.boxBuyBtnTx2 = uiMap["box_sell_cell.bg.box_2.buy_btn.buy_tx"]
	self.box3 = uiMap["box_sell_cell.bg.box_3"]
	local boxNameTx3 = uiMap["box_sell_cell.bg.box_3.name_tx"]
	boxNameTx3:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_3)) -- 珍贵宝箱
	local boxDescTx3 = uiMap["box_sell_cell.bg.box_3.desc_tx"]
	boxDescTx3:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_26)) -- 必出<color=#ff8af0>史诗</color>英雄
	self.boxBuyBtn3 = uiMap["box_sell_cell.bg.box_3"]
	self.boxBuyBtnCoin3 = uiMap["box_sell_cell.bg.box_3.buy_btn.coin"]
	self.boxBuyBtnTx3 = uiMap["box_sell_cell.bg.box_3.buy_btn.buy_tx"]
	self.progressSlider = uiMap["box_sell_cell.bg.progress_slider"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
	self.progressLv = uiMap["box_sell_cell.bg.progress_lv"]
	self.progressTx = uiMap["box_sell_cell.bg.progress_tx"]
	self.helpBtn = uiMap["box_sell_cell.bg.help_btn"]
	local titleTx = uiMap["box_sell_cell.title_bg.text"]
	titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_27)) -- 宝箱

	self.boxBuyBtn1:addClickListener(function()
		self:onClickBox(GConst.SummonConst.SUMMON_TYPE.LV_1)
	end)
	self.boxBuyBtn2:addClickListener(function()
		self:onClickBox(GConst.SummonConst.SUMMON_TYPE.LV_2)
	end)
	self.boxBuyBtn3:addClickListener(function()
		self:onClickBox(GConst.SummonConst.SUMMON_TYPE.LV_3)
	end)
	self.helpBtn:addClickListener(function()
		self:showHelpTips()
	end)

	self.cellHeight = BASE_CELL_HEIGHT
end

function BoxSellCell:refresh()
	if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.STORE_BOX_3_OPEN, true) then -- 没解锁的话第三个宝箱不显示
		self.cellHeight = BASE_CELL_HEIGHT + 70
		self.baseObject:setSizeDeltaY(BASE_CELL_HEIGHT)
		self.bg:setTexture("assets/arts/textures/background/shop/shop_bg_10.png")
		self.bg:setSizeDeltaY(BASE_CELL_HEIGHT - HEIGHT_ADD)
		self.box3:setVisible(false)
	else
		self.cellHeight = FULL_CELL_HEIGHT + 70
		self.baseObject:setSizeDeltaY(FULL_CELL_HEIGHT)
		self.bg:setTexture("assets/arts/textures/background/shop/shop_bg_9.png")
		self.bg:setSizeDeltaY(FULL_CELL_HEIGHT - HEIGHT_ADD)
		self.box3:setVisible(true)
	end
	self:refreshLv()
	self:refreshCost()
end

function BoxSellCell:refreshLv()
	local lv = DataManager.SummonData:getSummonLevel()
	local exp = DataManager.SummonData:getSummonExp()
	local maxExp = DataManager.SummonData:getSummonMaxExp()
	self.progressLv:setText("Lv." .. tostring(lv))
	if maxExp and maxExp > 0 then
		self.progressTx:setText(tostring(exp) .. "/" .. tostring(maxExp))
		self.progressSlider.value = exp / maxExp
	else
		self.progressTx:setText(I18N:getGlobalText(I18N.GlobalConst.STR_MAX))
		self.progressSlider.value = 1
	end
end

function BoxSellCell:refreshCost()
	local costItem1, cost1 = DataManager.SummonData:getSummonCost(GConst.SummonConst.SUMMON_TYPE.LV_1)
	local costItem2, cost2 = DataManager.SummonData:getSummonCost(GConst.SummonConst.SUMMON_TYPE.LV_2)
	local costItem3, cost3 = DataManager.SummonData:getSummonCost(GConst.SummonConst.SUMMON_TYPE.LV_3)

	if GFunc.checkCost(costItem1.id, costItem1.num, false) then
		self.boxBuyBtnCoin1:setSprite(GFunc.getIconRes(costItem1.id))
		local totalCount = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_BOX_KEY_LV_1)
		local totalCountStr = totalCount <= 99 and tostring(totalCount) or "99"
		self.boxBuyBtnTx1:setText(totalCountStr .. "/" .. tostring(costItem1.num))
	else
		self.boxBuyBtnCoin1:setSprite(GFunc.getIconRes(cost1.id))
		self.boxBuyBtnTx1:setText(cost1.num)
	end
	GFunc.centerImgAndTx(self.boxBuyBtnCoin1, self.boxBuyBtnTx1, 5)

	if GFunc.checkCost(costItem2.id, costItem2.num, false) then
		self.boxBuyBtnCoin2:setSprite(GFunc.getIconRes(costItem2.id))
		local totalCount = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_BOX_KEY_LV_2)
		local totalCountStr = totalCount <= 99 and tostring(totalCount) or "99"
		self.boxBuyBtnTx2:setText(totalCountStr .. "/" .. tostring(costItem2.num))
	else
		self.boxBuyBtnCoin2:setSprite(GFunc.getIconRes(cost2.id))
		self.boxBuyBtnTx2:setText(cost2.num)
	end
	GFunc.centerImgAndTx(self.boxBuyBtnCoin2, self.boxBuyBtnTx2, 5)

	if GFunc.checkCost(costItem3.id, costItem3.num, false) then
		self.boxBuyBtnCoin3:setSprite(GFunc.getIconRes(costItem3.id))
		local totalCount = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_BOX_KEY_LV_3)
		local totalCountStr = totalCount <= 99 and tostring(totalCount) or "99"
		self.boxBuyBtnTx3:setText(totalCountStr .. "/" .. tostring(costItem3.num))
	else
		self.boxBuyBtnCoin3:setSprite(GFunc.getIconRes(cost3.id))
		self.boxBuyBtnTx3:setText(cost3.num)
	end
	GFunc.centerImgAndTx(self.boxBuyBtnCoin3, self.boxBuyBtnTx3, 5)
end

function BoxSellCell:onClickBox(summonType)
	ModuleManager.ShopManager:showBoxHeroUI(summonType)
end

function BoxSellCell:showHelpTips()
	ModuleManager.ShopManager:showBoxLevelUI()
end

function BoxSellCell:getCellHeight()
	return self.cellHeight
end

function BoxSellCell:getIsOpen()
	return ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.STORE_BOX_OPEN, true)
end

function BoxSellCell:setVisible(visible)
	self.baseObject:setVisible(visible)
end

return BoxSellCell