local MainCompBaseCell = require "app/ui/main_city/component/main_comp_base_cell"
local ChapterComp = class("ChapterComp", MainCompBaseCell)
local CHAPTER_PATH = "assets/arts/textures/background/chapter/%s.png"

local BOX_ICON = {
	"common_chest_1",
	"common_chest_4"
}

function ChapterComp:getIsOpen()
    return true
end

function ChapterComp:getEntranceName()
    return I18N:getGlobalText(I18N.GlobalConst.MAIN_CHAPTER)
end

function ChapterComp:isShowTopNode()
    return true
end

function ChapterComp:isShowSideBar()
    return true
end

function ChapterComp:isShowBounty()
    return true
end

function ChapterComp:isShowHeroFormation()
    return true
end

function ChapterComp:getFightHpCost()
    return DataManager.ChapterData:getFightCost()
end

function ChapterComp:onClickFight()
	ModuleManager.ChapterManager:startFight()
end

function ChapterComp:init()
	self.uiMap = self:getBaseObject():genAllChildren()
	self:initChapter()
end

function ChapterComp:initChapter()
	self.chapterImg = self.uiMap["chapter.img"]
	self.chapterNameTx = self.uiMap["chapter.name_tx"]
	self.chapterWavetx = self.uiMap["chapter.record_tx"]
	self.leftArrow = self.uiMap["chapter.left_arrow"]
	local leftArrowBtn = self.uiMap["chapter.left_arrow.btn"]
	leftArrowBtn:addClickListener(function()
		if DataManager.ChapterData:goLastChapter() then
			self:refresh()
		end
	end)
	self.rightArrow = self.uiMap["chapter.right_arrow"]
	local rightArrowBtn = self.uiMap["chapter.right_arrow.btn"]
	rightArrowBtn:addClickListener(function()
		if DataManager.ChapterData:goNextChapter() then
			self:refresh()
		end
	end)

	self.uiMap["chapter.effect_node.ui_spine_obj"]:playAnim("idle", true, false)
end

function ChapterComp:refresh()
	self:refreshChapter()
end

function ChapterComp:refreshChapter(force)
	local chapterId = DataManager.ChapterData:getChapterId()
	if self.currChapterId ~= chapterId or force then
		if DataManager.ChapterData:getIsFirstChapter(chapterId) then -- 第一章不需要左箭头
			self.leftArrow:setVisible(false)
			self.rightArrow:setVisible(DataManager.ChapterData:getMaxChapterId() >= 1)
		elseif chapterId == DataManager.ChapterData:getMaxChapterId() + 1 then -- 只能看打的最远的关卡
			self.leftArrow:setVisible(true)
			self.rightArrow:setVisible(false)
		else
			self.leftArrow:setVisible(true)
			self.rightArrow:setVisible(not DataManager.ChapterData:isFinalChapter(chapterId))
		end

		self.currChapterId = chapterId
		local chapterInfo = ConfigManager:getConfig("chapter")[chapterId]
		local chapterI18NInfo = I18N:getConfig("chapter")[chapterId]
		if chapterInfo then
			self.chapterImg:setTexture(string.format(CHAPTER_PATH, chapterInfo.icon))
		end
		if chapterI18NInfo then
			self.chapterNameTx:setText(chapterI18NInfo.name)
		end
		self.chapterWavetx:setText(I18N:getGlobalText(I18N.GlobalConst.CHAPTER_DESC_1, DataManager.ChapterData:getChapterMaxWave()))

		local slider = self.uiMap["chapter.progress_bg.slider"]
		if not self.boxObjs then
			self.boxObjs = {}
			for i = 1, 3 do
				self.boxObjs[i] = {
					box = self.uiMap["chapter.progress_bg.box_" .. i],
					desc = self.uiMap["chapter.progress_bg.box_desc_" .. i],
					spineObj = self.uiMap["chapter.progress_bg.spine_node.ui_spine_obj_" .. i],
					boxIcon = self.uiMap["chapter.progress_bg.box_" .. i .. ".box_icon"]
				}
			end
		end

		local mysteryBoxCount = DataManager.ChapterData:getChapterMysteryBoxRewardCount(chapterId)
		local mysteryBoxBg = self.uiMap["chapter_comp.img.bg"]
		mysteryBoxBg:setVisible(mysteryBoxCount > 0)
		local mysteryBoxIcon = self.uiMap["chapter.img.mystery_box_icon"]
		mysteryBoxIcon:setVisible(mysteryBoxCount > 0)
		if mysteryBoxCount > 0 then
			local gotCount = DataManager.ChapterData:getChapterMysteryBoxGotCount(chapterId)
			local desc = I18N:getGlobalText(I18N.GlobalConst.CHAPTER_DESC_2, gotCount, mysteryBoxCount)
			self.uiMap["chapter.img.mystery_box_icon.desc"]:setText(desc)
		end

		local rewardChapterId = DataManager.ChapterData:getIsHaveRewardsMinId()
		if rewardChapterId > chapterId then
			rewardChapterId = chapterId
		end

		local curMaxWave = DataManager.ChapterData:getChapterMaxWave(rewardChapterId)
		local boxCount = DataManager.ChapterData:getChapterBoxCount(rewardChapterId)
		local unitValue = 1 / boxCount
		local sliderValue = 0
		local lastValue = 0
		for i = 1, boxCount do
			if curMaxWave < lastValue then
				break
			end
			local maxWave = DataManager.ChapterData:getChapterBoxNum(rewardChapterId, i)
			local wave = math.min(curMaxWave, maxWave)
			sliderValue = sliderValue + unitValue * (wave - lastValue) / (maxWave - lastValue)
			lastValue = maxWave
		end
		slider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = sliderValue

		for index, objs in ipairs(self.boxObjs) do
			local show = boxCount >= index
			objs.box:setActive(show)
			objs.desc:setActive(show)
			objs.spineObj:setVisible(false)
			if show then
				local needWave = DataManager.ChapterData:getChapterBoxNum(rewardChapterId, index)
				local x = 370 * (index / boxCount)
				local rewards = DataManager.ChapterData:getChapterBoxRewards(rewardChapterId, index)
				local num = DataManager.ChapterData:getChapterBoxNum(rewardChapterId, index)
				local rewardGot = DataManager.ChapterData:getChapterBoxRewardGot(rewardChapterId, index)
				local icon = BOX_ICON[1]
				if rewardGot then
					icon = BOX_ICON[2]
				end
				objs.box:addClickListener(function()
					if needWave <= curMaxWave and not rewardGot then
						objs.spineObj:setVisible(true)
						objs.spineObj:playAnimComplete("open", false, false, function()
							ModuleManager.ChapterManager:openBox(rewardChapterId, index)
						end)
					else
						ModuleManager.TipsManager:showRewardsTips(rewards, nil, objs.box)
					end
				end)
				objs.boxIcon:setSprite(GConst.ATLAS_PATH.COMMON, icon)
				objs.box:setAnchoredPositionX(x)
				if needWave <= curMaxWave and not rewardGot then
					objs.spineObj:setVisible(true)
					objs.spineObj:playAnim("idle", true, false)
					objs.spineObj:setAnchoredPositionX(x)
					objs.boxIcon:setVisible(false)
				else
					objs.spineObj:setVisible(false)
					objs.boxIcon:setVisible(true)
				end
				objs.desc:setAnchoredPositionX(x)
				if boxCount == index then
					objs.desc:setText(I18N:getGlobalText(I18N.GlobalConst.MAIN_DESC_1, rewardChapterId))
				else
					objs.desc:setText(num)
				end
			end
		end

        self:refreshFightBtn()
	end
end

return ChapterComp