local MainComp = class("MainComp", LuaComponent)

local UISpineObject = require "app/bf/unity/ui_spine_object"

-- local CHAPTER_PATH = "assets/arts/textures/background/chapter/%s.png"
local HERO_SPINE_ASSET_PATH = "assets/arts/spines/characters/%s/%s_skeletondata.asset"
local CHAPTER_PATH = "assets/prefabs/ui/chapter/%s.prefab"
local CHAPTER_PAGE = 5

function MainComp:onOpen()
	self.startMonsterAction = true
	if self.bossNode then
		self.bossNode:setAnchoredPositionX(0)
	end
	-- if self.bossShadow then
	-- 	self.bossShadow:setAnchoredPositionX(0)
	-- end
	if self.dialogueNode then
		self.dialogueNode:setAnchoredPositionX(0)
	end
end

function MainComp:onClose()
	if self.chapterMoveSeq then
		self.chapterMoveSeq:Kill()
		self.chapterMoveSeq = nil
	end
	if self.chapterMonsterGenerateSeq then
		self.chapterMonsterGenerateSeq:Kill()
		self.chapterMonsterGenerateSeq = nil
	end
	self.currChapterId = nil
	self.isTopTurn = nil
	self.startMonsterAction = false
	GFunc.killDOTween(GConst.DOTWEEN_IDS.CHAPTER_MONSTER)
	self.monsterSpineTopPool = {}
	self.monsterSpineDownPool = {}
	self.monsterSpineTopList = {}
	self.monsterSpineDownList = {}
	-- self.monsterShadowPool = {}
	self.monsterSmokePool = {}
	self.monsterNodeTop:removeAllChildren()
	self.monsterNodeDown:removeAllChildren()
	-- self.shadowNode:removeAllChildren()
	self.smokeNodeTop:removeAllChildren()
	self.smokeNodeDown:removeAllChildren()
end

function MainComp:init()
	local uiMap = self:getBaseObject():genAllChildren()

	uiMap["main_comp.arena_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_1))
	uiMap["main_comp.daily_challenge_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.DAILY_CHALLENGE))
	uiMap["main_comp.left_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.IDLE_DESC_1))
	uiMap["main_comp.right_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESC_2))
	self.backGroundNode = uiMap["main_comp.back_ground"]
	self.arenaBtn = uiMap["main_comp.arena_btn"]
	self.dailyChallengeBtn = uiMap["main_comp.daily_challenge_btn"]

	self.chapterBg = uiMap["main_comp.bg"]
	-- self.chapterBg:setAnchoredPositionX(-720)
	self.smokeNodeTop = uiMap["main_comp.smoke_node_1"]
	self.monsterNodeTop = uiMap["main_comp.monster_node_1"]
	self.bossNode = uiMap["main_comp.boss_node"]
	self.bossNode:setActive(false)
	self.bossSpine = uiMap["main_comp.boss_node.boss"]
	self.smokeNodeDown = uiMap["main_comp.smoke_node_2"]
	self.monsterNodeDown = uiMap["main_comp.monster_node_2"]
	self.monsterSpineTopPool = {}
	self.monsterSpineDownPool = {}
	self.monsterSpineTopList = {}
	self.monsterSpineDownList = {}
	-- self.monsterShadowPool = {}
	self.monsterSmokePool = {}
	self.startMonsterAction = true

	self.fightBtn = uiMap["main_comp.fight_btn"]
	-- 体力消耗
	self.fightCost = uiMap["main_comp.fight_btn.cost"]
	self.costTxCost = uiMap["main_comp.fight_btn.cost.tx_cost"]
	-- 按钮文本
	self.txFight = uiMap["main_comp.fight_btn.tx_desc"]
	self.fightBtn:addClickListener(function ()
		ModuleManager.ChapterManager:startFight()
	end)

	self.leftArrowBtn = uiMap["main_comp.middle_bg.left_arrow.btn"]
	self.rightArrowBtn = uiMap["main_comp.middle_bg.right_arrow.btn"]

	self.leftBtn = uiMap["main_comp.left_btn"]
	self.rightBtn = uiMap["main_comp.right_btn"]

	self.chapterMiddleBg = uiMap["main_comp.middle_bg"]
	self.chapterTx = uiMap["main_comp.middle_bg.chapter_tx"]
	self.chapterMaxTx = uiMap["main_comp.middle_bg.max_tx"]
	self.chapterInfoBtn = uiMap["main_comp.middle_bg.info_btn"]
	self.chapterBoxBtn = uiMap["main_comp.middle_bg.box_btn"]
	self.chapterPageNode = uiMap["main_comp.middle_bg.page_node"]
	self.chapterPageBtns = {}
	self.chapterPageBtnCheckImgs = {}
	self.chapterPageBtnDescTxs = {}
	self.chapterPageBtnMaskImgs = {}
	self.chapterPageBtnSelectImgs = {}
	for i = 1, CHAPTER_PAGE do
		self.chapterPageBtns[i] = uiMap["main_comp.middle_bg.page_node.page_btn_" .. i]
		self.chapterPageBtnCheckImgs[i] = uiMap["main_comp.middle_bg.page_node.page_btn_" .. i .. ".check_img"]
		self.chapterPageBtnDescTxs[i] = uiMap["main_comp.middle_bg.page_node.page_btn_" .. i .. ".desc_tx"]
		self.chapterPageBtnMaskImgs[i] = uiMap["main_comp.middle_bg.page_node.page_btn_" .. i .. ".mask_img"]
		self.chapterPageBtnSelectImgs[i] = uiMap["main_comp.middle_bg.page_node.page_btn_" .. i .. ".select_img"]
		self.chapterPageBtns[i]:addClickListener(function()
			self.chapterStage = i
			local chapterId = DataManager.ChapterData:getChapterCfgByPageAndStage(self.chapterPage, self.chapterStage)
			DataManager.ChapterData:setChapterId(chapterId)
			self:refresh()
		end)
	end
	-- MaincityManager:showChapterBoxUI(chapterId)

	uiMap["main_comp.middle_bg.box_btn"]:addClickListener(function()
		local chapterId = DataManager.ChapterData:getChapterId()
		ModuleManager.MaincityManager:showChapterBoxUI(chapterId)
	end)
	uiMap["main_comp.middle_bg.info_btn"]:addClickListener(function()
		local chapterId = DataManager.ChapterData:getChapterId()
		ModuleManager.MaincityManager:showChapterBoxUI(chapterId)
	end)
	uiMap["main_comp.arena_btn"]:addClickListener(function()
		if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.ARENA) then
			return
		end
		ModuleManager.ArenaManager:reqArenaInfo(true)
	end)
	uiMap["main_comp.daily_challenge_btn"]:addClickListener(function()
		if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.DAILY_CHALLENGE) then
			return
		end
		ModuleManager.DailyChallengeManager:showDailyChallengeUI()
	end)
	self.leftArrowBtn:addClickListener(function()
		self.chapterPage  = self.chapterPage - 1
		self.chapterList = DataManager.ChapterData:getChapterList(self.chapterPage)
		if self.chapterStage > #self.chapterList then
			self.chapterStage = #self.chapterList
		end
		local chapterId = DataManager.ChapterData:getChapterCfgByPageAndStage(self.chapterPage, self.chapterStage)
		DataManager.ChapterData:setChapterId(chapterId)
		self:refresh()
	end)
	self.rightArrowBtn:addClickListener(function()
		self.chapterPage = self.chapterPage + 1
			self.chapterList = DataManager.ChapterData:getChapterList(self.chapterPage)
		if self.chapterStage > #self.chapterList then
			self.chapterStage = #self.chapterList
		end
		local chapterId = DataManager.ChapterData:getChapterCfgByPageAndStage(self.chapterPage, self.chapterStage)
		DataManager.ChapterData:setChapterId(chapterId)
		self:refresh()
	end)
	self.leftBtn:addClickListener(function()
		ModuleManager.IdleManager:showIdleDropUI()
	end)
	self.rightBtn:addClickListener(function()
		ModuleManager.SummonManager:showSummonMainUI()
	end)
	local chapterId = DataManager.ChapterData:getChapterId()
	self.chapterPage = DataManager.ChapterData:getChapterPage(chapterId)
	self.chapterStage = DataManager.ChapterData:getChapterStage(chapterId)

	self.chapterScenes = {}
end

function MainComp:refreshTime()
	self:refreshRedPoint()
end

function MainComp:refresh()
	self:refreshChapter()
	self:refreshRedPoint()
end

function MainComp:refreshChapter(force)
	local chapterId = DataManager.ChapterData:getChapterId()
	self.chapterList = DataManager.ChapterData:getChapterList(self.chapterPage)
	if self.currChapterId ~= chapterId or force then
		self.currChapterId = chapterId
		self.chapterPage = DataManager.ChapterData:getChapterPage(chapterId)
		self.chapterStage = DataManager.ChapterData:getChapterStage(chapterId)
		self:refreshChapterBg()
        self:refreshFightBtn()
		self:doBossAction()
	end
	self.currChapterId = chapterId
	self:refreshChapterInfo()
	-- self:doChapterMove()
	-- self:doMonsterAction()
	self:refreshChapterBtn()

	self.leftBtn:setActive(ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.IDLE_DROP, true))
	self.rightBtn:setActive(ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.SUMMON_OPEN, true))
end

function MainComp:refreshChapterBg()
	if self.isLoading then
		return
	end
	local chapterId = DataManager.ChapterData:getChapterId()
	local icon = DataManager.ChapterData:getChapterCfg()[chapterId].icon
	local scene = DataManager.ChapterData:getChapterCfg()[chapterId].scene
	local bgPath = string.format(CHAPTER_PATH, icon)
	if self.currScenePath == bgPath then
		return
	end
	if self.chapterScenes[bgPath] then
		self.chapterScenes[bgPath]:setActive(true)
		if self.currScenePath and self.chapterScenes[self.currScenePath] then
			self.chapterScenes[self.currScenePath]:setActive(false)
		end
		self.currScenePath = bgPath
		self.chapterBg:setSprite(GConst.ATLAS_PATH.UI_CHAPTER, scene)
	else
		self.isLoading = true
		local oldPath = self.currScenePath
		UIPrefabManager:loadUIWidgetAsync(bgPath, self.backGroundNode, function(prefab)
			self.isLoading = false
			if oldPath and self.chapterScenes[oldPath] then
				self.chapterScenes[oldPath]:setActive(false)
			end
			self.currScenePath = bgPath
			self.chapterScenes[self.currScenePath] = prefab
			self.chapterBg:setSprite(GConst.ATLAS_PATH.UI_CHAPTER, scene)
		end)
	end
end

function MainComp:refreshChapterBtn()
	local chapterNewId = DataManager.ChapterData:getNewChapterId()
	local maxPage = DataManager.ChapterData:getChapterPage(chapterNewId)
	self.leftArrowBtn:setActive(self.chapterPage > 1)
	self.rightArrowBtn:setActive(self.chapterPage < maxPage)
end

function MainComp:refreshFightBtn()
	-- 体力消耗
	local cost = DataManager.ChapterData:getFightCost()
	self.fightCost:setActive(true)
	self.txFight:setText(I18N:getGlobalText(I18N.GlobalConst.START_DESC))
	self.costTxCost:setText(GFunc.getRewardNum(cost))
end

function MainComp:refreshChapterInfo()
	local chapterId = DataManager.ChapterData:getChapterId()
	local chapterMaxId = DataManager.ChapterData:getMaxChapterId()
	for i = 1, CHAPTER_PAGE do
		if i <= #self.chapterList then
			self.chapterPageBtns[i]:setActive(true)
			self.chapterPageBtnSelectImgs[i]:setActive(i == self.chapterStage)
			self.chapterPageBtnDescTxs[i]:setText(i)
			local selChapterId = DataManager.ChapterData:getChapterCfgByPageAndStage(self.chapterPage, i)
			self.chapterPageBtnCheckImgs[i]:setActive(selChapterId <= chapterMaxId)
			self.chapterPageBtnMaskImgs[i]:setActive(selChapterId > (chapterMaxId + 1))
		else
			self.chapterPageBtns[i]:setActive(false)
		end
	end
	self.chapterPageNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()

	self.fightBtn:setActive(chapterId <= chapterMaxId + 1)

	local chapterI18NInfo = I18N:getConfig("chapter")[chapterId]
	if chapterI18NInfo then	
		self.chapterTx:setText(chapterI18NInfo.name)
	end
	if chapterId <= chapterMaxId then
		self.chapterMaxTx:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_7))
	elseif chapterId == chapterMaxId + 1 then
		self.chapterMaxTx:setText(I18N:getGlobalText(I18N.GlobalConst.CHAPTER_DESC_1, DataManager.ChapterData:getChapterMaxWave()))
	else
		self.chapterMaxTx:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_22))
	end
end

function MainComp:refreshRedPoint()
	local time = Time:getServerTime() - DataManager.IdleData:getLastDropTime()
    local idleMaxTime = DataManager.IdleData:getIdleMaxTime()
    if time >= idleMaxTime or DataManager.IdleData:getQuickIdleRemainTimes() > 0 then
        self.leftBtn:addRedPoint(70, 20, 1)
	else
		self.leftBtn:removeRedPoint()
    end

	if DataManager.SummonData:hasSummonCostRedPoint() then
        self.rightBtn:addRedPoint(-70, 20, 1)
	else
		self.rightBtn:removeRedPoint()
	end

	if DataManager.ArenaData:hasEntranceRedDot() then
		self.arenaBtn:addRedPoint(70, 26, 1)
	else
		self.arenaBtn:removeRedPoint()
	end

	if DataManager.DailyChallengeData:showRedPoint() then
		self.dailyChallengeBtn:addRedPoint(-70, 26, 1)
	else
		self.dailyChallengeBtn:removeRedPoint()
	end
end

function MainComp:getArenaBtnPosition()
	return self.arenaBtn:getPosition()
end

function MainComp:getDailyChallengeBtnPosition()
	return self.dailyChallengeBtn:getPosition()
end

function MainComp:getIdleBtnPosition()
	return self.leftBtn:getPosition()
end

function MainComp:getSummonBtnPosition()
	return self.rightBtn:getPosition()
end
-- region 动画
-- function MainComp:doChapterMove()
-- 	if self.chapterMoveSeq then
-- 		return
-- 	end
-- 	self.chapterBg:setAnchoredPositionX(-720)
-- 	self.chapterMoveSeq = self.chapterBg:createBindTweenSequence()
-- 	local moveTween = self.chapterBg:getTransform():DOAnchorPosX(720, 20):SetEase(CS.DG.Tweening.Ease.Linear)
-- 	self.chapterMoveSeq:Append(moveTween)
-- 	self.chapterMoveSeq:AppendCallback(function()
-- 		self.chapterBg:setAnchoredPositionX(-720)
-- 	end)
-- 	self.chapterMoveSeq:SetLoops(-1)
-- end

function MainComp:doBossAction()
	if self.currChapterId == nil then
		return
	end
	local chapterInfo = ConfigManager:getConfig("chapter")[self.currChapterId]
	if chapterInfo == nil then
		return
	end
	local monsterIdList = chapterInfo.monster
	-- monsterIdList = {20001, 10001, 10002}
	if monsterIdList == nil then
		return
	end
	self.chapterMonsterShowList = monsterIdList
	if self.chapterBossId == nil then -- 还没显示boss模型
		self.chapterBossId = self.chapterMonsterShowList[#self.chapterMonsterShowList]
		local chapterBossId = self.chapterBossId
		local monsterCfg = ConfigManager:getConfig("monster")
		local monsterInfo = monsterCfg[self.chapterBossId]
		if monsterInfo then
			local modelId = monsterInfo.model_id
			local path = string.format(HERO_SPINE_ASSET_PATH, modelId, modelId)
			self.bossNode:setActive(false)
			local scale = monsterInfo.ui or 1
			self.bossSpine:loadAssetAsync(modelId, function()
				if chapterBossId == self.chapterBossId then
					self.bossNode:setActive(true)
					self.bossSpine:setLocalScale(scale, scale, scale)
					self.bossSpine:playAnim("move", true, true, true)
				else -- 加载完成后，已经切换到其他boss了
					self:doChangeBossAction()
				end
			end, path)
		end
	else
		local chapterBossId = self.chapterMonsterShowList[#self.chapterMonsterShowList]
		if self.chapterBossId ~= chapterBossId then
			self.chapterBossId = chapterBossId
			self:doChangeBossAction()
		end
	end
end

-- 切换boss
function MainComp:doChangeBossAction()
	local chapterBossId = self.chapterBossId
	local monsterCfg = ConfigManager:getConfig("monster")
	local monsterInfo = monsterCfg[self.chapterBossId]
	if monsterInfo then
		local modelId = monsterInfo.model_id
		local path = string.format(HERO_SPINE_ASSET_PATH, modelId, modelId)
		self.bossNode:setActive(false)
		local scale = monsterInfo.ui or 1
		self.bossSpine:loadAssetAsync(modelId, function()
			if chapterBossId == self.chapterBossId then
				self.bossNode:setActive(true)
				self.bossSpine:setLocalScale(scale, scale, scale)
				self.bossSpine:playAnim("move", true, true, true)
			else -- 加载完成后，已经切换到其他boss了
				self:doChangeBossAction()
			end
		end, path)
	end
end

-- function MainComp:doMonsterAction()
-- 	if self.chapterMonsterGenerateSeq then
-- 		return
-- 	end
-- 	self.chapterMonsterGenerateSeq = DOTweenManager:createSeqWithIntId()
-- 	local interval = GFunc.getConstValue("chapter_idel_monster_x") / 1000
-- 	interval = 2
-- 	self.chapterMonsterGenerateSeq:AppendCallback(function()
-- 		self:generateChapterMonsters()
-- 	end)
-- 	self.chapterMonsterGenerateSeq:AppendInterval(interval)
-- 	self.chapterMonsterGenerateSeq:SetLoops(-1)
-- end

-- function MainComp:generateChapterMonsters()
-- 	local monsterId
-- 	if #self.chapterMonsterShowList <= 2 then
-- 		monsterId = self.chapterMonsterShowList[2]
-- 	else
-- 		monsterId = self.chapterMonsterShowList[math.random(2, #self.chapterMonsterShowList)]
-- 	end
-- 	if monsterId == nil then
-- 		return
-- 	end
-- 	local monsterCfg = ConfigManager:getConfig("monster_base")
-- 	local monsterInfo = monsterCfg[monsterId]
-- 	if monsterInfo == nil then
-- 		return
-- 	end
-- 	if self.isTopTurn == nil then
-- 		self.isTopTurn = math.random(1, 100) > 50
-- 	end
-- 	local modelId = monsterInfo.model_id
-- 	local pool
-- 	local list
-- 	local parent
-- 	local posY
-- 	if self.isTopTurn then -- 刷上面
-- 		pool = self.monsterSpineTopPool[modelId]
-- 		list = self.monsterSpineTopList
-- 		parent = self.monsterNodeTop
-- 		posY = math.random(1, 300)
-- 	else -- 刷下面
-- 		pool = self.monsterSpineDownPool[modelId]
-- 		list = self.monsterSpineDownList
-- 		parent = self.monsterNodeDown
-- 		posY = -math.random(1, 240)
-- 	end
-- 	self.isTopTurn = not self.isTopTurn
-- 	if pool and #pool > 0 then
-- 		local spine = table.remove(pool)
-- 		table.insert(list, spine)
-- 		spine:setActive(true)
-- 		self:doMonsterMove(modelId, spine, posY, monsterInfo, not self.isTopTurn)
-- 	else
-- 		SpineManager:loadUIHeroSpineWidgetAsync(modelId, parent, function(spine)
-- 			if not self.startMonsterAction then
-- 				spine:destroy()
-- 				return
-- 			end
-- 			table.insert(list, spine)
-- 			local scale = monsterInfo.ui or 1
-- 			spine:setLocalScale(-scale, scale, scale)
-- 			self:doMonsterMove(modelId, spine, posY, monsterInfo, not self.isTopTurn)
-- 		end)
-- 	end
-- end

-- function MainComp:doMonsterMove(modelId, spine, posY, monsterInfo, isTopTurn)
-- 	spine:playAnim("move", true, true, true)
-- 	local posX = GConst.UI_SCREEN_WIDTH / 2 + 100
-- 	local targetPosX = -GConst.UI_SCREEN_WIDTH / 2 - 300
-- 	spine:setAnchoredPosition(posX, posY)
-- 	local seq = DOTweenManager:createSeqWithIntId(GConst.DOTWEEN_IDS.CHAPTER_MONSTER)
-- 	if self.chapterMonsterSpeed == nil then
-- 		-- self.chapterMonsterSpeed = GFunc.getConstValue("chapter_idel_monstermove")
-- 		self.chapterMonsterSpeed = 150
-- 	end
-- 	local distance = posX - targetPosX
-- 	local moveTime = distance / self.chapterMonsterSpeed
-- 	local moveTween = spine:getTransform():DOAnchorPosX(targetPosX, moveTime):SetEase(CS.DG.Tweening.Ease.Linear)
-- 	seq:Append(moveTween)
-- end

-- function MainComp:enterChapterBattle()
-- 	-- boss等一会再跑
-- 	local bossWaitTime = GFunc.getConstValue("chapter_fight_bosswait") / 1000
-- 	local bossSeq = DOTweenManager:createSeqWithIntId(GConst.DOTWEEN_IDS.CHAPTER_MONSTER)
-- 	bossSeq:AppendInterval(bossWaitTime)
-- 	local bossSpeed = GFunc.getConstValue("chapter_fight_bossmove")
-- 	local distance = GConst.UI_SCREEN_WIDTH/2 + 100
-- 	local moveTime = distance / bossSpeed
-- 	local targetPosX = -GConst.UI_SCREEN_WIDTH / 2 - 300
-- 	local moveTween = self.bossNode:getTransform():DOAnchorPosX(targetPosX, moveTime):SetEase(CS.DG.Tweening.Ease.Linear)
-- 	bossSeq:Append(moveTween)
-- 	local moveTween2 = self.dialogueNode:getTransform():DOAnchorPosX(targetPosX, moveTime):SetEase(CS.DG.Tweening.Ease.Linear)
-- 	bossSeq:Join(moveTween2)
-- 	-- local moveTween3 = self.bossShadow:getTransform():DOAnchorPosX(targetPosX, moveTime):SetEase(CS.DG.Tweening.Ease.Linear)
-- 	-- bossSeq:Join(moveTween3)


-- 	-- 出来一批小怪从右边跑向左边
-- 	local monsterCount = GFunc.getConstIntValue("chapter_fight_monster_num")
-- 	self.chapterFightMonsterCount = monsterCount
-- 	local wave = 5
-- 	local monsterCountPerWave = math.ceil(monsterCount / wave)
-- 	-- 分批处理
-- 	local monsterSeq = DOTweenManager:createSeqWithIntId(GConst.DOTWEEN_IDS.CHAPTER_MONSTER)
-- 	for i = 1, wave do
-- 		monsterSeq:AppendCallback(function ()
-- 			if monsterCountPerWave > monsterCount then
-- 				monsterCountPerWave = monsterCount
-- 			end
-- 			monsterCount = monsterCount - monsterCountPerWave
-- 			for j = 1, monsterCountPerWave do
-- 				self:generateChapterFightMonsters()
-- 			end
-- 		end)
-- 		monsterSeq:AppendInterval(0.03)
-- 	end
-- 	return GFunc.getConstValue("chapter_fight_wait") / 1000
-- end

-- function MainComp:generateChapterFightMonsters()
-- 	local monsterId
-- 	if #self.chapterMonsterShowList <= 2 then
-- 		monsterId = self.chapterMonsterShowList[2]
-- 	else
-- 		monsterId = self.chapterMonsterShowList[math.random(2, #self.chapterMonsterShowList)]
-- 	end
-- 	if monsterId == nil then
-- 		return
-- 	end
-- 	local monsterCfg = ConfigManager:getConfig("monster_base")
-- 	local monsterInfo = monsterCfg[monsterId]
-- 	if monsterInfo == nil then
-- 		return
-- 	end
-- 	local modelId = monsterInfo.model_id
-- 	local pool
-- 	local list
-- 	local parent
-- 	local posY
-- 	if self.isTopTurn then -- 刷上面
-- 		pool = self.monsterSpineTopPool[modelId]
-- 		list = self.monsterSpineTopList
-- 		parent = self.monsterNodeTop
-- 		posY = math.random(1, 300)
-- 	else -- 刷下面
-- 		pool = self.monsterSpineDownPool[modelId]
-- 		list = self.monsterSpineDownList
-- 		parent = self.monsterNodeDown
-- 		posY = -math.random(1, 240)
-- 	end
-- 	self.isTopTurn = not self.isTopTurn
-- 	if pool and #pool > 0 then
-- 		local spine = table.remove(pool)
-- 		table.insert(list, spine)
-- 		spine:setActive(true)
-- 		self:doMonsterFightMove(modelId, spine, posY, monsterInfo, not self.isTopTurn)
-- 	else
-- 		SpineManager:loadUIHeroSpineWidgetAsync(modelId, parent, function(spine)
-- 			if not self.startMonsterAction then
-- 				spine:destroy()
-- 				return
-- 			end
-- 			table.insert(list, spine)
-- 			local scale = monsterInfo.ui or 1
-- 			spine:setLocalScale(-scale, scale, scale)
-- 			self:doMonsterFightMove(modelId, spine, posY, monsterInfo, not self.isTopTurn)
-- 		end)
-- 	end
-- end

-- function MainComp:doMonsterFightMove(modelId, spine, posY, monsterInfo, isTopTurn)
-- 	spine:playAnim("move", true, true, true)
-- 	local posX = GConst.UI_SCREEN_WIDTH / 2 + math.random(100, math.floor(GConst.UI_SCREEN_WIDTH*2/3))
-- 	spine:setAnchoredPosition(posX, posY)
-- 	local seq = DOTweenManager:createSeqWithIntId(GConst.DOTWEEN_IDS.CHAPTER_MONSTER)
-- 	if self.chapterMonsterFightSpeed == nil then
-- 		-- self.chapterMonsterFightSpeed = GFunc.getConstValue("chapter_fight_monstermove")
-- 		self.chapterMonsterFightSpeed = 880
-- 	end
-- 	local distance = posX + GConst.UI_SCREEN_WIDTH/2 + 100
-- 	local moveTime = distance / self.chapterMonsterFightSpeed
-- 	local targetPosX = -GConst.UI_SCREEN_WIDTH / 2 - 100
-- 	local moveTween = spine:getTransform():DOAnchorPosX(targetPosX, moveTime):SetEase(CS.DG.Tweening.Ease.Linear)
-- 	seq:Append(moveTween)

-- 	if self.chapterFightMonsterCount then
-- 		self.chapterFightMonsterCount = self.chapterFightMonsterCount - 1
-- 		if self.chapterFightMonsterCount == 0 then -- 最后一个怪物生成完毕后，重新排一下序
-- 			self:sortChapterFightMonstersOrder()
-- 			self:sortChapterFightMonstersOrder()
-- 			self:sortChapterFightMonstersOrder()
-- 			self:sortChapterFightMonstersOrder()
-- 		end
-- 	end
-- end

-- function MainComp:sortChapterFightMonstersOrder()
-- 	self:sortChildrenOrder(self.monsterNodeTop)
-- 	self:sortChildrenOrder(self.monsterNodeDown)
-- end

-- function MainComp:sortChildrenOrder(parent)
-- 	local children = parent:getChildList()
-- 	if children and #children > 0 then
-- 		table.sort(children, function(a, b)
-- 			return a:getAnchoredPositionY() > b:getAnchoredPositionY()
-- 		end)

-- 		for i = #children, 1, -1 do
-- 			children[i]:getTransform():SetSiblingIndex(i - 1)
-- 		end
-- 	end
-- end

-- endregion

return MainComp