local DungeonGoldCell = class("DungeonGoldCell", BaseCell)

function DungeonGoldCell:init()
	local uiMap = self:getUIMap()

	self.challengeBtn = uiMap["gold_cell.challenge_btn"]
	self.challengeBtn:addClickListener(function()
		if self.chapterId then
			-- local remainTimes = DataManager.DungeonDailyData.GoldData:getTodayRemainTimes()
			-- local ticketCount = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_DUNGEON_TICKET_1)
			-- if remainTimes <= 0 and ticketCount <= 0 then
			-- 	GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE_DESC_1))
			-- 	return
			-- end
			-- ModuleManager.ChapterManager:showMainBattleEntryUI(ModuleManager.BattleManager.BATTLE_TYPE.DUNGEON,function()
			-- 	self:OnClickStrtGame()
			-- end, self.chapterId, false, GConst.DungeonConst.DUNGEON_DAILY_TYPE.GOLD)
			self:OnClickStrtGame()
		end
	end)
	self.challengeBtnTx = uiMap["gold_cell.challenge_btn.text"]
	self.challengeBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_18))
	-- 门票
	self.challengeBtnItem = uiMap["gold_cell.challenge_btn.item"]
	self.challengeBtnItemTx = uiMap["gold_cell.challenge_btn.item.text"]
	self.challengeBtnItemIcon = uiMap["gold_cell.challenge_btn.item.icon"]
	self.challengeBtnItemNumTx = uiMap["gold_cell.challenge_btn.item.num_tx"]
	self.challengeBtnItemTx:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_18))
	self.challengeBtnItemIcon:setSprite(GFunc.getIconRes(GConst.ItemConst.ITEM_ID_DUNGEON_TICKET_1))

	self.sweepBtn = uiMap["gold_cell.sweep_btn"]
	self.sweepBtn:addClickListener(function()
		if self.chapterId then
			ModuleManager.DungeonManager:sweepDungeonDaily(self.chapterId)
		end
	end)
	self.sweepBtnTx = uiMap["gold_cell.sweep_btn.text"]
	self.sweepBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_DESC_3))
	-- 门票
	self.sweepBtnItem = uiMap["gold_cell.sweep_btn.item"]
	self.sweepBtnItemTx = uiMap["gold_cell.sweep_btn.item.text"]
	self.sweepBtnItemIcon = uiMap["gold_cell.sweep_btn.item.icon"]
	self.sweepBtnItemNumTx = uiMap["gold_cell.sweep_btn.item.num_tx"]
	self.sweepBtnItemTx:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_DESC_3))
	self.sweepBtnItemIcon:setSprite(GFunc.getIconRes(GConst.ItemConst.ITEM_ID_DUNGEON_TICKET_1))

	self.unlockBtn = uiMap["gold_cell.unlock_btn"]
	self.unlockBtn:addClickListener(function()
		GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.CHALLENGE_TEAM_10))
	end)
	self.unlockTx = uiMap["gold_cell.unlock_btn.text"]
	self.unlockTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_7))

	self.stageTx = uiMap["gold_cell.stage_tx"]
	self.powerTx = uiMap["gold_cell.power_tx"]

	self.scrollrect = uiMap["gold_cell.scrollrect"]
	self.rewardList = {}
end
function DungeonGoldCell:OnClickStrtGame()
	if self.chapterId then
		ModuleManager.DungeonManager:onDungeonStartReq(self.chapterId)
		-- ModuleManager.DungeonManager:startDungeonDailyBattle(self.chapterId)
	end
end

function DungeonGoldCell:refresh(index)
	local chapterInfo = DataManager.DungeonDailyData.GoldData:getChapterInfo(index)
	self.chapterId = chapterInfo.id
	local maxPassedId = DataManager.DungeonDailyData.GoldData:getMaxPassedId()
	local challengeId = DataManager.DungeonDailyData.GoldData:getChallengeId()
	local chapterName = DataManager.DungeonDailyData.GoldData:getChapterName(chapterInfo.id)
	self.stageTx:setText(chapterName)
	local curPower = DataManager.HeroData:getShowPower()
	local chapterPower = chapterInfo.cfg.power
	if curPower >= chapterPower then
		self.powerTx:setText("<color=#049500>" .. I18N:getGlobalText(I18N.GlobalConst.RECOMMENDED_POWER, GFunc.num2Str(chapterPower, 2)) .. "</color>")
	else
		self.powerTx:setText("<color=#E10000>" .. I18N:getGlobalText(I18N.GlobalConst.RECOMMENDED_POWER, GFunc.num2Str(chapterPower, 2)) .. "</color>")
	end
	local rewardsCount = #self.rewardList
	for i = 1, rewardsCount do
		table.remove(self.rewardList)
	end
	self.firstCount = 0
	if chapterInfo.id > challengeId then -- 未解锁
		for k, v in ipairs(chapterInfo.cfg.first_reward) do
			table.insert(self.rewardList, v)
		end
		self.firstCount = #chapterInfo.cfg.first_reward
		for k, v in ipairs(chapterInfo.cfg.reward) do
			table.insert(self.rewardList, v)
		end
		self.challengeBtn:setActive(false)
		self.sweepBtn:setActive(false)
		self.unlockBtn:setActive(true)
	elseif chapterInfo.id == challengeId and challengeId ~= maxPassedId then -- 正在挑战中
		for k, v in ipairs(chapterInfo.cfg.first_reward) do
			table.insert(self.rewardList, v)
		end
		self.firstCount = #chapterInfo.cfg.first_reward
		for k, v in ipairs(chapterInfo.cfg.reward) do
			table.insert(self.rewardList, v)
		end
		self.challengeBtn:setActive(true)
		self.sweepBtn:setActive(false)
		self.unlockBtn:setActive(false)
	else -- 已通关，可以扫荡
		self.firstCount = 0
		for k, v in ipairs(chapterInfo.cfg.reward) do
			table.insert(self.rewardList, v)
		end
		self.challengeBtn:setActive(false)
		self.sweepBtn:setActive(true)
		self.unlockBtn:setActive(false)
	end

	if not self.scrollRectComp then
        self.scrollRectComp = self.scrollrect:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
        self.scrollRectComp:addInitCallback(function()
            return GConst.TYPEOF_LUA_CLASS.REWARD_CELL
        end)
        self.scrollRectComp:addRefreshCallback(function(cellIndex, cell)
			cell:refreshByConfig(self.rewardList[cellIndex], false, false)
			cell:setShowFirstTag(cellIndex <= self.firstCount)
        end)
    end

	local totalCount = #self.rewardList
    if self.scrollRectComp:getTotalCount() == nil or self.scrollRectComp:getTotalCount() <= 0 then
		self.scrollRectComp:refillCells(totalCount)
    elseif self.scrollRectComp:getTotalCount() ~= totalCount then
		self.scrollRectComp:clearCells()
		self.scrollRectComp:refillCells(totalCount)
	else
		self.scrollRectComp:updateAllCell()
	end

	-- 根据道具数量刷新入口按钮
	local remainTimes = DataManager.DungeonDailyData.GoldData:getTodayRemainTimes()
	local ticketCount = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_DUNGEON_TICKET_1)
	if remainTimes <= 0 and ticketCount > 0 then
		self.challengeBtnItem:setVisible(true)
		self.challengeBtnTx:setVisible(false)
		self.challengeBtnItemNumTx:setText(ticketCount .. "/1")

		self.sweepBtnItem:setVisible(true)
		self.sweepBtnTx:setVisible(false)
		self.sweepBtnItemNumTx:setText(ticketCount .. "/1")
	else
		self.challengeBtnItem:setVisible(false)
		self.challengeBtnTx:setVisible(true)

		self.sweepBtnItem:setVisible(false)
		self.sweepBtnTx:setVisible(true)
	end
end

return DungeonGoldCell