local JointDrillLevelSelectCtrl = class("JointDrillLevelSelectCtrl", BaseCtrl)
local LocalData = require("GameCore.Data.LocalData")
local ClientManager = CS.ClientManager.Instance
JointDrillLevelSelectCtrl._mapNodeConfig = {
	TopBarPanel = {
		sCtrlName = "Game.UI.TopBarEx.TopBarCtrl"
	},
	animRoot = {
		sNodeName = "----SafeAreaRoot----",
		sComponentName = "Animator"
	},
	imgBg = {sComponentName = "Image"},
	goMain = {sNodeName = "---Main---"},
	btnAvg = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_Avg"
	},
	txtBtnAvg = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Btn_Story"
	},
	btnQuest = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_Quest"
	},
	txtBtnQuest = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Btn_Reward"
	},
	redDotQuest = {},
	goActTime = {},
	txtActTimeCn = {sComponentName = "TMP_Text"},
	txtActTime = {sComponentName = "TMP_Text"},
	goLevelList = {},
	txtTitle = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Difficulty_Select"
	},
	levelListContent = {
		sComponentName = "RectTransform"
	},
	goLevelItem = {},
	goLevelBattleItem = {},
	goTickets = {},
	btnTicketTip = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_Tickets"
	},
	txtTicketCount = {sComponentName = "TMP_Text"},
	goTip = {sNodeName = "--Tips--"},
	btnTipsBg = {
		sComponentName = "Button",
		callback = "OnBtnClick_TipsBg"
	},
	txtTipsName = {sComponentName = "TMP_Text"},
	txtTipsContent = {sComponentName = "TMP_Text"},
	txtRefreshTime = {sComponentName = "TMP_Text"},
	goChallengeEnd = {},
	txtChallengeEnd = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Challenge_End"
	},
	goRank = {},
	goRankNormal = {},
	txtRankCn = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Rank_Cur_Rank"
	},
	txtRankValue = {sComponentName = "TMP_Text"},
	txtScoreCn = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Rank_Highest_Score"
	},
	txtScoreValue = {sComponentName = "TMP_Text"},
	goRankEmpty = {},
	txtRankEmpty = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Self_Rank_Empty_1"
	},
	btnRankDetail = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_RankDetail"
	},
	txtBtnRankDetail = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Rank_Title_Detail"
	},
	goLevelInfo = {
		sNodeName = "---LevelInfo---"
	},
	goInBattle = {},
	goNormal = {},
	btnStart = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_Start"
	},
	txtBtnStart = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Btn_Start_Challenge"
	},
	animatorStart = {
		sNodeName = "AnimRootStart",
		sComponentName = "Animator"
	},
	btnContinue = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_Continue"
	},
	txtBtnContinue = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Btn_Continue_Challenge"
	},
	animatorContinue = {
		sNodeName = "AnimRootContinue",
		sComponentName = "Animator"
	},
	btnSimulation = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_Simulation"
	},
	txtBtnSimulation = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Btn_Start_Challenge_Simulate"
	},
	animatorSimulation = {
		sNodeName = "AnimRootSimulation",
		sComponentName = "Animator"
	},
	btnCancel = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_Cancel"
	},
	txtBtnCancel = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Btn_Stop_Challenge"
	},
	animatorCancel = {
		sNodeName = "AnimRootCancel",
		sComponentName = "Animator"
	},
	btnFastBattle = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_FastBattle"
	},
	animatorFastBattle = {
		sNodeName = "AnimRootFastBattle",
		sComponentName = "Animator"
	},
	txtBtnFastBattle = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Battle_Fast_Battle"
	},
	imgFastBattleLock = {},
	txtChallengeCount = {sComponentName = "TMP_Text"},
	txtRecLevel = {sComponentName = "TMP_Text"},
	txtRecommend = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Build_Recommend"
	},
	txtRecommendLevel = {
		sComponentName = "TMP_Text",
		sLanguageId = "InfinityTower_Recommend_Lv"
	},
	imgRecommendBuild = {sComponentName = "Image"},
	txtRecommendBuild = {
		sComponentName = "TMP_Text",
		sLanguageId = "InfinityTower_Recommend_Construct"
	},
	imgProperty = {nCount = 6, sComponentName = "Image"},
	txtBossSkill = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Boss_Info"
	},
	btnBossSkill = {
		nCount = 6,
		sComponentName = "UIButton",
		callback = "OnBtnClick_Skill"
	},
	imgBossSkill = {nCount = 6, sComponentName = "Image"},
	txtRewardTitle = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Challenge_Reward"
	},
	btnReward = {
		nCount = 3,
		sComponentName = "UIButton",
		callback = "OnBtnClick_Reward"
	},
	goRewardItem = {
		nCount = 3,
		sCtrlName = "Game.UI.TemplateEx.TemplateItemCtrl"
	},
	imgTickets = {sComponentName = "Image"},
	txtTicketsCount = {sComponentName = "TMP_Text"},
	goChallengeTime = {},
	txtChallengeTime = {sComponentName = "TMP_Text"},
	goBossInfo = {},
	imgBossHpBarBg = {
		sComponentName = "RectTransform"
	},
	imgBossHpBar = {sComponentName = "Image"},
	txtBossHp = {sComponentName = "TMP_Text"},
	imgSimulation = {},
	txtSimulation = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Btn_Start_Challenge_Simulate"
	},
	txtBossName = {sComponentName = "TMP_Text"},
	goAvgWindow = {
		sNodeName = "---AVGWindow---"
	},
	goBlur = {
		sNodeName = "t_fullscreen_blur_01"
	},
	goAvgRoot = {
		sNodeName = "goMainLineAvgRoot"
	},
	animWindow = {sNodeName = "rtWindow", sComponentName = "Animator"},
	txtAvgTitle = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_AVG_Title"
	},
	btnCloseWindow = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_CloseAvgWindow"
	},
	mainLineAvgLvName = {sComponentName = "TMP_Text"},
	mainLineAvgLvDes = {sComponentName = "TMP_Text"},
	btnCancelWindow = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_CancelAvgWindow"
	},
	txtBtnCancelWindow = {
		sComponentName = "TMP_Text",
		sLanguageId = "MessageBox_Cancel"
	},
	btnStartAvg = {
		sComponentName = "UIButton",
		callback = "OnBtnClick_StartAvg"
	},
	txtBtnStartAvg = {
		sComponentName = "TMP_Text",
		sLanguageId = "JointDrill_Enter_AVG"
	},
	goSmoke = {}
}
JointDrillLevelSelectCtrl._mapEventConfig = {
	RefreshJointDrillLevel = "OnEvent_RefreshJointDrillLevel",
	SelectJointDrillLevel = "OnEvent_SelectJointDrillLevel",
	GetJointDrillRankSuc = "OnEvent_GetJointDrillRankSuc",
	JointDrillRaidSuccess = "OnEvent_JointDrillRaidSuccess",
	RefreshChallengeTime = "OnEvent_RefreshChallengeTime",
	RefreshJointDrillActTime = "OnEvent_RefreshJointDrillActTime",
	[EventId.UIBackConfirm] = "OnEvent_Back",
	[EventId.UIHomeConfirm] = "OnEvent_BackHome"
}
JointDrillLevelSelectCtrl._mapRedDotConfig = {
	[RedDotDefine.JointDrillQuest] = {
		sNodeName = "redDotQuest"
	}
}
local panelType_main = 1
local panelType_levelInfo = 2
function JointDrillLevelSelectCtrl:PlayAvg(callback)
	local function avgEndCallback()
		EventManager.Remove("StoryDialog_DialogEnd", self, avgEndCallback)
		LocalData.SetPlayerLocalData("JointDrillAvgId", self.nActId)
		if callback ~= nil then
			callback()
		end
	end
	EventManager.Add("StoryDialog_DialogEnd", self, avgEndCallback)
	local mapCfg = ConfigTable.GetData("JointDrillControl", self.nActId)
	if mapCfg ~= nil then
		EventManager.Hit("StoryDialog_DialogStart", mapCfg.DrillPlotId)
	end
end
function JointDrillLevelSelectCtrl:RefreshAvgInfo()
	local mapCfg = ConfigTable.GetData("JointDrillControl", self.nActId)
	if mapCfg ~= nil then
		NovaAPI.SetTMPText(self._mapNode.mainLineAvgLvName, mapCfg.PlotTitle)
		NovaAPI.SetTMPText(self._mapNode.mainLineAvgLvDes, mapCfg.PlotDesc)
	end
end
function JointDrillLevelSelectCtrl:Refresh()
	self.bInBattle = PlayerData.JointDrill:CheckJointDrillInBattle()
	self.nCurLevelId = PlayerData.JointDrill:GetJointDrillLevelId()
	if self.nPanelType == panelType_main then
		self:RefreshLevelList()
	elseif self.nPanelType == panelType_levelInfo then
		self:RefreshLevelInfo()
	end
	self:StartRankTimer()
	self:RefreshRankData()
	local nAvgId = LocalData.GetPlayerLocalData("JointDrillAvgId")
	self._mapNode.btnAvg.gameObject:SetActive(false)
end
function JointDrillLevelSelectCtrl:RefreshLevelList()
	self._mapNode.goMain.gameObject:SetActive(true)
	self._mapNode.goLevelInfo.gameObject:SetActive(false)
	self._mapNode.animRoot:Play("JointDrill_Main_in", 0, 0)
	if self.bInBattle or self.nActStatus == AllEnum.JointDrillActStatus.Start then
		self._mapNode.goLevelList.gameObject:SetActive(true)
		self._mapNode.goChallengeEnd.gameObject:SetActive(false)
		local mapLevelList = CacheTable.GetData("_JointDrillLevel", self.nGroupId)
		if self.nCurLevelId == 0 and self.battleItemCtrl ~= nil then
			destroy(self.battleItemCtrl.gameObject)
			self:UnbindCtrlByNode(self.battleItemCtrl)
			self.battleItemCtrl = nil
		end
		if mapLevelList ~= nil then
			for _, v in ipairs(self.tbLevelItemCtrl) do
				v.gameObject:SetActive(false)
			end
			local nIndex = 1
			for k, v in ipairs(mapLevelList) do
				local itemCtrl
				local nLevelId = v.Id
				if self.nCurLevelId == nLevelId then
					if self.battleItemCtrl == nil then
						local itemObj = instantiate(self._mapNode.goLevelBattleItem, self._mapNode.levelListContent)
						self.battleItemCtrl = self:BindCtrlByNode(itemObj, "Game.UI.JointDrill.JointDrillLevelItemCtrl")
					end
					self.battleItemCtrl.gameObject:SetActive(true)
					self.battleItemCtrl:SetItem(v, self.nCurLevelId)
				else
					if self.tbLevelItemCtrl[nIndex] == nil then
						local itemObj = instantiate(self._mapNode.goLevelItem, self._mapNode.levelListContent)
						itemCtrl = self:BindCtrlByNode(itemObj, "Game.UI.JointDrill.JointDrillLevelItemCtrl")
						table.insert(self.tbLevelItemCtrl, itemCtrl)
					else
						itemCtrl = self.tbLevelItemCtrl[nIndex]
					end
					if itemCtrl ~= nil then
						itemCtrl.gameObject:SetActive(true)
						itemCtrl:SetItem(v)
					end
					nIndex = nIndex + 1
				end
			end
		end
	else
		self._mapNode.goLevelList.gameObject:SetActive(false)
		self._mapNode.goChallengeEnd.gameObject:SetActive(true)
	end
	local nCurCount = PlayerData.Item:GetItemCountByID(self.nTicketId)
	NovaAPI.SetTMPText(self._mapNode.txtTicketCount, orderedFormat(ConfigTable.GetUIText("JointDrill_Challenge_Ticket"), nCurCount, self.nMaxTicketCount))
end
function JointDrillLevelSelectCtrl:RefreshLevelInfo()
	self._mapNode.goMain.gameObject:SetActive(false)
	self._mapNode.goLevelInfo.gameObject:SetActive(true)
	self._mapNode.goInBattle.gameObject:SetActive(self.bInBattle)
	self._mapNode.goNormal.gameObject:SetActive(not self.bInBattle)
	self._mapNode.animRoot:Play("JointDrill_LevelInfo_in", 0, 0)
	if self.bInBattle then
		self._mapNode.animatorContinue:Play("JointDrill_Btn_Continue", 0, 0)
		self._mapNode.animatorCancel:Play("JointDrill_Btn_Cancel", 0, 0)
	else
		self._mapNode.animatorStart:Play("JointDrill_Btn_Start", 0, 0)
		self._mapNode.animatorSimulation:Play("JointDrill_Btn_Small", 0, 0)
		self._mapNode.animatorFastBattle:Play("JointDrill_Btn_Small", 0, 0)
	end
	local mapLevelCfg = ConfigTable.GetData("JointDrillLevel", self.nSelectLevelId)
	if mapLevelCfg == nil then
		return
	end
	local nBossHp, nBossHpMax = 0, 0
	local bSimulate = PlayerData.JointDrill:GetBattleSimulate()
	if self.bInBattle then
		local mapBossInfo = PlayerData.JointDrill:GetJointDrillBossInfo()
		nBossHp = mapBossInfo.nHp
		nBossHpMax = mapBossInfo.nHpMax
		local tbTeams = PlayerData.JointDrill:GetJointDrillBuildList()
		local nChallengeCount = #tbTeams
		self.nAllChallengeCount = PlayerData.JointDrill:GetMaxChallengeCount(self.nSelectLevelId)
		NovaAPI.SetTMPText(self._mapNode.txtChallengeCount, orderedFormat(ConfigTable.GetUIText("JointDrill_Challenge_Count"), self.nAllChallengeCount - nChallengeCount))
		self:RefreshChallengeTime()
	else
		local nCurCount = PlayerData.Item:GetItemCountByID(self.nTicketId)
		NovaAPI.SetTMPText(self._mapNode.txtTicketsCount, "-1")
		local bPassed = PlayerData.JointDrill:CheckPassedId(self.nSelectLevelId)
		self._mapNode.imgFastBattleLock.gameObject:SetActive(not bPassed)
		nBossHpMax = PlayerData.JointDrill:GetMonsterMaxHp(mapLevelCfg.BossId, mapLevelCfg.Difficulty)
		nBossHp = nBossHpMax
	end
	local nMonsterId = mapLevelCfg.BossId
	local nDiff = mapLevelCfg.Difficulty
	local sMonsterName = PlayerData.JointDrill:GetMonsterName(nMonsterId)
	NovaAPI.SetTMPText(self._mapNode.txtBossName, string.format("%s/%s", sMonsterName, ConfigTable.GetUIText("JointDrill_Difficulty_Name_" .. nDiff)))
	NovaAPI.SetTMPText(self._mapNode.txtBossHp, string.format("%s/%s", nBossHp, nBossHpMax))
	NovaAPI.SetImageFillAmount(self._mapNode.imgBossHpBar, nBossHp / nBossHpMax)
	self._mapNode.imgSimulation.gameObject:SetActive(bSimulate)
	NovaAPI.SetTMPText(self._mapNode.txtRecLevel, mapLevelCfg.RecommendLv)
	local sScoreIcon = "Icon/BuildRank/BuildRank_" .. mapLevelCfg.RecommendBuildRank
	self:SetPngSprite(self._mapNode.imgRecommendBuild, sScoreIcon)
	local tbEET = {}
	local nMonsterId = mapLevelCfg.BossId
	local mapMonsterCfgData = ConfigTable.GetData("Monster", nMonsterId)
	if mapMonsterCfgData ~= nil then
		local nTempleteId = mapMonsterCfgData.Templete
		local mapTemplete = ConfigTable.GetData("MonsterValueTempleteAdjust", nTempleteId)
		if mapTemplete ~= nil then
			tbEET = clone(mapTemplete.WeakEET)
		end
	end
	for k, v in ipairs(self._mapNode.imgProperty) do
		v.gameObject:SetActive(tbEET[k] ~= nil)
		if tbEET[k] ~= nil then
			local sName = AllEnum.ElementIconType.Icon .. tbEET[k]
			self:SetAtlasSprite(v, "12_rare", sName)
		end
	end
	self.tbBossSkill = mapLevelCfg.BossAffix
	for k, v in ipairs(self._mapNode.imgBossSkill) do
		v.gameObject:SetActive(mapLevelCfg.BossAffix[k] ~= nil)
		if mapLevelCfg.BossAffix[k] ~= nil then
			local mapSkillCfg = ConfigTable.GetData("JointDrillAffix", mapLevelCfg.BossAffix[k])
			if mapSkillCfg ~= nil then
				self:SetPngSprite(v, mapSkillCfg.Icon)
			end
		end
	end
	local rewardData = mapLevelCfg.RewardPreview
	self.tbReward = decodeJson(rewardData)
	for i = 1, 3 do
		local bReceived = false
		if self.tbReward[i] ~= nil then
			if self.tbReward[i][3] == 1 then
				bReceived = PlayerData.JointDrill:CheckPassedId(self.nSelectLevelId)
			end
			self._mapNode.goRewardItem[i]:SetItem(self.tbReward[i][1], nil, self.tbReward[i][2], nil, bReceived, self.tbReward[i][3] == 1)
		else
			self._mapNode.goRewardItem[i]:SetItem(0)
		end
	end
end
function JointDrillLevelSelectCtrl:ActEnd()
	local confirmCallback = function()
		EventManager.Hit(EventId.CloesCurPanel)
	end
	local msg = {
		nType = AllEnum.MessageBox.Alert,
		sContent = ConfigTable.GetUIText("JointDrill_Act_End_Tip"),
		callbackConfirm = confirmCallback
	}
	EventManager.Hit(EventId.OpenMessageBox, msg)
end
function JointDrillLevelSelectCtrl:SetChallengeTime(nRemainTime)
	local tbTime = timeFormat_Table(nRemainTime)
	local sTime = ""
	if tbTime.min > 0 then
		sTime = orderedFormat(ConfigTable.GetUIText("JointDrill_Challenge_Time_3"), tbTime.min, tbTime.sec)
	else
		sTime = orderedFormat(ConfigTable.GetUIText("JointDrill_Challenge_Time_4"), tbTime.sec)
	end
	NovaAPI.SetTMPText(self._mapNode.txtChallengeTime, orderedFormat(ConfigTable.GetUIText("JointDrill_Challenge_Time_Left"), sTime))
end
function JointDrillLevelSelectCtrl:RefreshChallengeTime()
	local nStartTime = PlayerData.JointDrill:GetJointDrillStartTime()
	local nCloseTime = math.floor(nStartTime + ConfigTable.GetConfigNumber("JointDrill_Challenge_Time_Max"))
	local nCurTime = ClientManager.serverTimeStamp
	local nRemainTime = nCloseTime - nCurTime
	self:SetChallengeTime(nRemainTime)
end
function JointDrillLevelSelectCtrl:RefreshActTime()
	local actData = PlayerData.Activity:GetActivityDataById(self.nActId)
	if actData == nil then
		return
	end
	local nRemainTime = 0
	local nCurTime = ClientManager.serverTimeStamp
	if self.nActStatus == AllEnum.JointDrillActStatus.Start then
		local nChallengeEndTime = actData:GetChallengeEndTime()
		nRemainTime = nChallengeEndTime - nCurTime
	elseif self.nActStatus == AllEnum.JointDrillActStatus.WaitClose then
		local nActEndTime = actData:GetActCloseTime()
		nRemainTime = nActEndTime - nCurTime
	end
	self:SetActTime(nRemainTime)
end
function JointDrillLevelSelectCtrl:SetActTime(nRemainTime)
	if self.nActStatus == AllEnum.JointDrillActStatus.Start then
		NovaAPI.SetTMPText(self._mapNode.txtActTimeCn, ConfigTable.GetUIText("JointDrill_Challenge_Time_Text"))
	elseif self.nActStatus == AllEnum.JointDrillActStatus.WaitClose then
		NovaAPI.SetTMPText(self._mapNode.txtActTimeCn, ConfigTable.GetUIText("JointDrill_Act_Time_Text"))
	end
	if nRemainTime <= 0 then
		if self.nPanelType == panelType_main then
			self:RefreshLevelList()
		end
		if self.nActStatus == AllEnum.JointDrillActStatus.Closed then
			self:ActEnd()
		end
	end
	local tbTime = timeFormat_Table(nRemainTime)
	local sTime = ""
	if 0 < tbTime.day then
		sTime = orderedFormat(ConfigTable.GetUIText("JointDrill_Challenge_Time_1"), tbTime.day, tbTime.hour)
	elseif 0 < tbTime.hour then
		sTime = orderedFormat(ConfigTable.GetUIText("JointDrill_Challenge_Time_2"), tbTime.hour, tbTime.min)
	elseif 0 < tbTime.min then
		sTime = orderedFormat(ConfigTable.GetUIText("JointDrill_Challenge_Time_3"), tbTime.min, tbTime.sec)
	else
		sTime = orderedFormat(ConfigTable.GetUIText("JointDrill_Challenge_Time_4"), tbTime.sec)
	end
	NovaAPI.SetTMPText(self._mapNode.txtActTime, sTime)
end
function JointDrillLevelSelectCtrl:RefreshRankData()
	local mapSelfRank = PlayerData.JointDrill:GetSelfRankData()
	local bRank = mapSelfRank ~= nil and mapSelfRank.Rank > 0
	self._mapNode.goRankNormal:SetActive(bRank)
	self._mapNode.goRankEmpty:SetActive(not bRank)
	if bRank then
		NovaAPI.SetTMPText(self._mapNode.txtRankValue, orderedFormat(ConfigTable.GetUIText("JointDrill_Self_Rank"), mapSelfRank.Rank))
		NovaAPI.SetTMPText(self._mapNode.txtScoreValue, mapSelfRank.Score)
	end
end
function JointDrillLevelSelectCtrl:StartRankTimer()
	local nLastRankTime, nTime = PlayerData.JointDrill:GetLastRankRefreshTime()
	local nNextTime = nLastRankTime + nTime
	local checkSend = function()
		local nCurTime = ClientManager.serverTimeStamp
		if nLastRankTime == 0 or nCurTime >= nNextTime and self.nActStatus ~= AllEnum.JointDrillActStatus.Closed then
			PlayerData.JointDrill:SendJointDrillRankMsg()
		end
	end
	checkSend()
	if self.rankTimer ~= nil then
		self.rankTimer:Cancel()
		self.rankTimer = nil
	end
	self.rankTimer = self:AddTimer(0, 10, checkSend, true, true, true)
end
function JointDrillLevelSelectCtrl:StartTicketsRefreshTimer()
	if self.ticketRefreshTimer ~= nil then
		self.ticketRefreshTimer:Cancel()
		self.ticketRefreshTimer = nil
	end
	local nCurTime = ClientManager.serverTimeStamp
	local nNextRefreshTime = CS.ClientManager.Instance:GetNextRefreshTime(nCurTime)
	local refreshTime = function()
		nCurTime = ClientManager.serverTimeStamp
		local nRemainTime = nNextRefreshTime - nCurTime
		local sTime = timeFormat_HMS(nRemainTime)
		NovaAPI.SetTMPText(self._mapNode.txtRefreshTime, orderedFormat(ConfigTable.GetUIText("JointDrill_Tickets_Refresh_Time"), sTime))
	end
	refreshTime()
	self.ticketRefreshTimer = self:AddTimer(0, 1, function()
		refreshTime()
	end, true, true, true)
end
function JointDrillLevelSelectCtrl:Awake()
	self.nPanelType = panelType_main
	self.nSelectLevelId = 0
	self.nTicketId = ConfigTable.GetConfigNumber("JointDrill_Challenge_Tickets_Id")
	self.nMaxTicketCount = ConfigTable.GetConfigNumber("JointDrill_Challenge_Tickets_Max")
	local mapItemCfg = ConfigTable.GetData_Item(self.nTicketId)
	if mapItemCfg ~= nil then
		self:SetPngSprite(self._mapNode.imgTickets, mapItemCfg.Icon)
		NovaAPI.SetTMPText(self._mapNode.txtTipsName, mapItemCfg.Title)
		NovaAPI.SetTMPText(self._mapNode.txtTipsContent, ConfigTable.GetUIText("JointDrill_Tickets_Desc"))
	end
end
function JointDrillLevelSelectCtrl:OnEnable()
	self._mapNode.goAvgWindow.gameObject:SetActive(false)
	self._mapNode.goSmoke.gameObject:SetActive(false)
	self._mapNode.goLevelItem.gameObject:SetActive(false)
	self._mapNode.goLevelBattleItem.gameObject:SetActive(false)
	self._mapNode.goTip.gameObject:SetActive(false)
	self.tbLevelItemCtrl = {}
	self.nRankReqCount = 0
	self.nActStatus = 0
	local tbParam = self:GetPanelParam()
	if 0 < #tbParam then
		self.nActId = tbParam[1]
		local mapCfg = ConfigTable.GetData("JointDrillControl", self.nActId)
		if mapCfg == nil then
			return
		end
		self.nGroupId = mapCfg.DrillLevelGroupId
		self:SetPngSprite(self._mapNode.imgBg, "UI/Play_JointDrill/SpriteAtlas/Sprite/" .. mapCfg.BG)
	end
	local callback = function()
	end
	PlayerData.Build:GetAllBuildBriefData(callback)
	self.nActStatus = PlayerData.JointDrill:GetActStatus()
	self._mapNode.goSmoke.gameObject:SetActive(false)
	self._mapNode.goSmoke.gameObject:SetActive(true)
	self:RefreshActTime()
	self:Refresh()
	self:StartTicketsRefreshTimer()
	self:RefreshAvgInfo()
end
function JointDrillLevelSelectCtrl:OnDisable()
	if self.ticketRefreshTimer ~= nil then
		self.ticketRefreshTimer:Cancel()
		self.ticketRefreshTimer = nil
	end
	if self.rankTimer ~= nil then
		self.rankTimer:Cancel()
		self.rankTimer = nil
	end
	if self.battleItemCtrl ~= nil then
		destroy(self.battleItemCtrl.gameObject)
		self:UnbindCtrlByNode(self.battleItemCtrl)
	end
	self.battleItemCtrl = nil
	for _, v in ipairs(self.tbLevelItemCtrl) do
		destroy(v.gameObject)
		self:UnbindCtrlByNode(v)
	end
	self.tbLevelItemCtrl = {}
end
function JointDrillLevelSelectCtrl:OnDestroy()
end
function JointDrillLevelSelectCtrl:OnBtnClick_Avg()
end
function JointDrillLevelSelectCtrl:OnBtnClick_Quest()
	EventManager.Hit(EventId.OpenPanel, PanelId.JointDrillQuest)
end
function JointDrillLevelSelectCtrl:OnBtnClick_Tickets()
	if self.nTicketId ~= nil then
		self._mapNode.goTip.gameObject:SetActive(true)
		local mapItemCfg = ConfigTable.GetData_Item(self.nTicketId)
		if mapItemCfg ~= nil then
			NovaAPI.SetTMPText(self._mapNode.txtTipsContent, mapItemCfg.Desc)
		end
	end
end
function JointDrillLevelSelectCtrl:OnBtnClick_TipsBg()
	self._mapNode.goTip.gameObject:SetActive(false)
end
function JointDrillLevelSelectCtrl:OnBtnClick_RankDetail()
	EventManager.Hit(EventId.OpenPanel, PanelId.JointDrillRanking, self.nActId)
end
function JointDrillLevelSelectCtrl:OnBtnClick_LevelBack()
	self.nPanelType = panelType_main
	self:RefreshLevelList()
end
function JointDrillLevelSelectCtrl:OnBtnClick_LevelHome()
	PanelManager.Home()
end
function JointDrillLevelSelectCtrl:OnBtnClick_Start()
	local bInChallengeTime = PlayerData.JointDrill:CheckActChallengeTime()
	if not bInChallengeTime then
		EventManager.Hit(EventId.OpenMessageBox, ConfigTable.GetUIText("JointDrill_Challenge_End_Tip"))
		self.nPanelType = panelType_main
		self:RefreshLevelList()
		return
	end
	local nCurCount = PlayerData.Item:GetItemCountByID(self.nTicketId)
	if nCurCount <= 0 then
		EventManager.Hit(EventId.OpenMessageBox, ConfigTable.GetUIText("JointDrill_Tickets_NotEnough"))
		return
	end
	if self.nSelectLevelId == 0 then
		printError("LevelId 错误")
		return
	end
	local callback = function()
		EventManager.Hit(EventId.OpenPanel, PanelId.JointDrillBuildList, self.nSelectLevelId, false)
	end
	callback()
end
function JointDrillLevelSelectCtrl:OnBtnClick_Continue()
	local callback = function()
		self.bInBattle = PlayerData.JointDrill:CheckJointDrillInBattle()
		self.nPanelType = panelType_main
		self:RefreshLevelList()
	end
	local nStartTime = PlayerData.JointDrill:GetJointDrillStartTime()
	local nCloseTime = math.floor(nStartTime + ConfigTable.GetConfigNumber("JointDrill_Challenge_Time_Max"))
	if nCloseTime <= ClientManager.serverTimeStamp then
		PlayerData.JointDrill:JointDrillGameOver(callback, true)
		return
	end
	local bInChallengeTime = PlayerData.JointDrill:CheckActChallengeTime()
	if not bInChallengeTime then
		PlayerData.JointDrill:JointDrillGameOver(callback, true)
		return
	end
	local bSimulate = PlayerData.JointDrill:GetBattleSimulate()
	EventManager.Hit(EventId.OpenPanel, PanelId.JointDrillBuildList, self.nSelectLevelId, bSimulate)
end
function JointDrillLevelSelectCtrl:OnBtnClick_Simulation()
	local bInChallengeTime = PlayerData.JointDrill:CheckActChallengeTime()
	if not bInChallengeTime then
		EventManager.Hit(EventId.OpenMessageBox, ConfigTable.GetUIText("JointDrill_Challenge_End_Tip"))
		self.nPanelType = panelType_main
		self:RefreshLevelList()
		return
	end
	if self.nSelectLevelId == 0 then
		printError("LevelId 错误")
		return
	end
	EventManager.Hit(EventId.OpenPanel, PanelId.JointDrillBuildList, self.nSelectLevelId, true)
end
function JointDrillLevelSelectCtrl:OnBtnClick_Cancel()
	local sTip = ConfigTable.GetUIText("JointDrill_Give_Up_Tip")
	local callback = function()
		local gameOverCallback = function()
			self.bInBattle = PlayerData.JointDrill:CheckJointDrillInBattle()
			self.nPanelType = panelType_main
			self:RefreshLevelList()
		end
		PlayerData.JointDrill:JointDrillGameOver(gameOverCallback, true)
	end
	local msg = {
		nType = AllEnum.MessageBox.Confirm,
		sContent = sTip,
		callbackConfirm = callback
	}
	EventManager.Hit(EventId.OpenMessageBox, msg)
end
function JointDrillLevelSelectCtrl:OnBtnClick_FastBattle()
	local bInChallengeTime = PlayerData.JointDrill:CheckActChallengeTime()
	if not bInChallengeTime then
		EventManager.Hit(EventId.OpenMessageBox, ConfigTable.GetUIText("JointDrill_Challenge_End_Tip"))
		self.nPanelType = panelType_main
		self:RefreshLevelList()
		return
	end
	if not PlayerData.JointDrill:CheckPassedId(self.nSelectLevelId) then
		EventManager.Hit(EventId.OpenMessageBox, ConfigTable.GetUIText("JointDrill_Fast_Battle_Tip"))
		return
	end
	local nCurTicketCount = PlayerData.Item:GetItemCountByID(self.nTicketId)
	if nCurTicketCount <= 0 then
		EventManager.Hit(EventId.OpenMessageBox, ConfigTable.GetUIText("JointDrill_Tickets_NotEnough"))
		return
	end
	EventManager.Hit(EventId.OpenPanel, PanelId.JointDrillRaid, self.nSelectLevelId)
end
function JointDrillLevelSelectCtrl:OnEvent_RefreshJointDrillLevel()
	self:Refresh()
end
function JointDrillLevelSelectCtrl:OnEvent_SelectJointDrillLevel(nLevelId)
	if self.nPanelType == panelType_main then
		local bInChallengeTime = PlayerData.JointDrill:CheckActChallengeTime()
		if not bInChallengeTime then
			local gameOverCallback = function()
				EventManager.Hit(EventId.OpenMessageBox, ConfigTable.GetUIText("JointDrill_Challenge_End_Tip"))
				self.nPanelType = panelType_main
				self:RefreshLevelList()
			end
			if self.bInBattle then
				PlayerData.JointDrill:JointDrillGameOver(gameOverCallback, true)
			else
				gameOverCallback()
			end
			return
		end
		local nAnimTime = NovaAPI.GetAnimClipLength(self._mapNode.animRoot, {
			"JointDrill_Main_out"
		})
		self._mapNode.animRoot:Play("JointDrill_Main_out")
		self._mapNode.goSmoke.gameObject:SetActive(false)
		self._mapNode.goSmoke.gameObject:SetActive(true)
		self:AddTimer(1, nAnimTime, function()
			self.nPanelType = panelType_levelInfo
			self.nSelectLevelId = nLevelId
			self:RefreshLevelInfo()
		end, true, true, true)
		EventManager.Hit(EventId.TemporaryBlockInput, nAnimTime)
	end
end
function JointDrillLevelSelectCtrl:OnEvent_GetJointDrillRankSuc()
	local nLastRankTime, nTime = PlayerData.JointDrill:GetLastRankRefreshTime()
	local nNextTime = nLastRankTime + nTime
	local nCurTime = ClientManager.serverTimeStamp
	if nLastRankTime == 0 or nNextTime <= nCurTime then
		self:RefreshRankData()
		return
	end
	self:StartRankTimer()
	self:RefreshRankData()
end
function JointDrillLevelSelectCtrl:OnEvent_JointDrillRaidSuccess(netMsg)
	local mapSelfRank = PlayerData.JointDrill:GetSelfRankData()
	local nRank = 0
	local nScoreOld = 0
	if mapSelfRank ~= nil then
		nRank = mapSelfRank.Rank
		nScoreOld = mapSelfRank.Score
	end
	local nTotalScoreOld = PlayerData.JointDrill:GetTotalRankScore()
	local nScore = math.max(netMsg.Score - nTotalScoreOld, 0)
	local mapScore = {
		nScore = nScore,
		nTotalScore = netMsg.Score,
		nScoreOld = nScoreOld
	}
	local callback = function()
		if netMsg.Rewards ~= nil then
			local tabItem = {}
			for k, v in ipairs(netMsg.Rewards) do
				for _, item in ipairs(v.Items) do
					if tabItem[item.Tid] == nil then
						tabItem[item.Tid] = 0
					end
					tabItem[item.Tid] = tabItem[item.Tid] + item.Qty
				end
			end
			local tbShowItem = {}
			for nId, nCount in pairs(tabItem) do
				table.insert(tbShowItem, {Tid = nId, Qty = nCount})
			end
			UTILS.OpenReceiveByDisplayItem(tbShowItem, netMsg.Change)
		end
	end
	EventManager.Hit(EventId.OpenPanel, PanelId.JointDrillRankUp, nRank, nRank, mapScore, AllEnum.JointDrillResultType.ChallengeEnd, callback)
end
function JointDrillLevelSelectCtrl:OnEvent_RefreshChallengeTime(nRemainTime)
	self:SetChallengeTime(nRemainTime)
end
function JointDrillLevelSelectCtrl:OnEvent_RefreshJointDrillActTime(nStatus, nRemainTime)
	self.nActStatus = nStatus
	self:SetActTime(nRemainTime)
end
function JointDrillLevelSelectCtrl:OnBtnClick_Skill(btn, nIndex)
	if self.tbBossSkill[nIndex] ~= nil then
		local nSkillId = self.tbBossSkill[nIndex]
		if nSkillId ~= 0 then
			local mapData = {nSkillId = nSkillId, bJointDrill = true}
			EventManager.Hit(EventId.OpenPanel, PanelId.SkillTips, btn.transform, mapData)
		end
	end
end
function JointDrillLevelSelectCtrl:OnBtnClick_Reward(btn, nIndex)
	if self.tbReward[nIndex] ~= nil then
		local nTid = self.tbReward[nIndex][1]
		UTILS.ClickItemGridWithTips(nTid, btn.transform, true, true, false)
	end
end
function JointDrillLevelSelectCtrl:OnBtnClick_CloseAvgWindow()
	self._mapNode.animWindow:Play("t_window_04_t_out")
	local closeWindow = function()
		self._mapNode.goAvgWindow.gameObject:SetActive(false)
	end
	self:AddTimer(1, 0.3, closeWindow, true, true, true, false)
end
function JointDrillLevelSelectCtrl:OnBtnClick_CancelAvgWindow()
	self._mapNode.animWindow:Play("t_window_04_t_out")
	local closeWindow = function()
		self._mapNode.goAvgWindow.gameObject:SetActive(false)
	end
	self:AddTimer(1, 0.3, closeWindow, true, true, true, false)
end
function JointDrillLevelSelectCtrl:OnBtnClick_StartAvg()
	self:PlayAvg()
end
function JointDrillLevelSelectCtrl:OnEvent_Back()
	if self.nPanelType == panelType_levelInfo then
		local nAnimTime = NovaAPI.GetAnimClipLength(self._mapNode.animRoot, {
			"JointDrill_LevelInfo_out"
		})
		self._mapNode.animRoot:Play("JointDrill_LevelInfo_out")
		self._mapNode.goSmoke.gameObject:SetActive(false)
		self._mapNode.goSmoke.gameObject:SetActive(true)
		self:AddTimer(1, nAnimTime, function()
			self.nPanelType = panelType_main
			self:RefreshLevelList()
		end, true, true, true)
		EventManager.Hit(EventId.TemporaryBlockInput, nAnimTime)
	else
		EventManager.Hit(EventId.CloesCurPanel)
	end
end
function JointDrillLevelSelectCtrl:OnEvent_BackHome()
	PanelManager.Home()
end
return JointDrillLevelSelectCtrl
