local MallSkinCtrl = class("MallSkinCtrl", BaseCtrl)
MallSkinCtrl._mapNodeConfig = {
	sv = {
		sComponentName = "LoopScrollView"
	},
	cgSv = {
		sNodeName = "sv",
		sComponentName = "CanvasGroup"
	},
	tab = {},
	trTab = {sComponentName = "Transform"},
	svTab = {sComponentName = "ScrollRect"},
	goEmpty = {},
	txtSkinEmpty = {
		sComponentName = "TMP_Text",
		sLanguageId = "Mall_Skin_Empty"
	}
}
MallSkinCtrl._mapEventConfig = {MallOrderClear = "Refresh"}
function MallSkinCtrl:Refresh(bResetPos)
	if self._panel.nCurTog ~= AllEnum.MallToggle.Skin then
		return
	end
	NovaAPI.SetCanvasGroupAlpha(self._mapNode.cgSv, 0)
	local callback = function(tbList, nTime)
		self:RefreshData(tbList)
		self:RefreshTab()
		self:RefreshList(bResetPos)
		self:SetTimer(nTime)
		NovaAPI.SetCanvasGroupAlpha(self._mapNode.cgSv, 1)
	end
	PlayerData.Mall:SendMallPackageListReq(callback)
end
function MallSkinCtrl:RefreshData(tbList)
	local tbPage = PlayerData.Mall:GetPackagePage(GameEnum.MallItemType.Skin)
	local tbAllList = {}
	local tbPackageToPage = {}
	for _, v in pairs(tbList) do
		local mapCfg = ConfigTable.GetData("MallPackage", v.sId)
		if mapCfg.Tag == GameEnum.MallItemType.Skin then
			local nGroupId = mapCfg.GroupId
			if not tbPackageToPage[nGroupId] then
				tbPackageToPage[nGroupId] = {}
			end
			table.insert(tbPackageToPage[nGroupId], v)
			table.insert(tbAllList, v)
		end
	end
	self.tbPackagePage, self.tbPages = {}, {}
	table.insert(self.tbPackagePage, tbAllList)
	self.tbPages[1] = {
		Id = 0,
		Name = ConfigTable.GetUIText("Mall_Package_TabAll")
	}
	for _, v in pairs(tbPage) do
		if tbPackageToPage[v.Id] then
			table.insert(self.tbPackagePage, tbPackageToPage[v.Id])
			table.insert(self.tbPages, v)
		end
	end
	self.nPageCount = #self.tbPages
	if self._panel.nDefaultId then
		for k, v in ipairs(self.tbPages) do
			if v.Id == self._panel.nDefaultId then
				self.nCurTab = k
				self._panel.nDefaultId = nil
				break
			end
		end
	end
	if not self.tbPages[self.nCurTab] or self.nSelectPage and self.tbPages[self.nCurTab].Id ~= self.nSelectPage then
		self.nCurTab = 1
	end
	self.nSelectPage = self.tbPages[self.nCurTab].Id
end
function MallSkinCtrl:RefreshList(bResetPos)
	for nInstanceId, objCtrl in pairs(self.tbGridCtrl) do
		self:UnbindCtrlByNode(objCtrl)
		self.tbGridCtrl[nInstanceId] = nil
	end
	self.tbCurList = self.tbPackagePage[self.nCurTab] or {}
	if #self.tbCurList == 0 then
		self._mapNode.goEmpty.gameObject:SetActive(true)
		self._mapNode.sv.gameObject:SetActive(false)
		self._mapNode.svTab.gameObject:SetActive(false)
	else
		self._mapNode.goEmpty.gameObject:SetActive(false)
		self._mapNode.sv.gameObject:SetActive(true)
		self._mapNode.svTab.gameObject:SetActive(true)
		self._mapNode.sv:SetAnim(0.04)
		self._mapNode.sv:Init(#self.tbCurList, self, self.OnGridRefresh, self.OnGridBtnClick, not bResetPos)
	end
end
function MallSkinCtrl:RefreshTab()
	NovaAPI.SetScrollHorizontal(self._mapNode.svTab, self.nPageCount > 4)
	self.ctrlTab = {}
	delChildren(self._mapNode.trTab)
	for i = 1, self.nPageCount do
		local goTab = instantiate(self._mapNode.tab, self._mapNode.trTab)
		goTab:SetActive(true)
		local btn = goTab.transform:Find("btnTab"):GetComponent("UIButton")
		local func_Handler = ui_handler(self, self.OnBtnClick_ChangeSheet, goTab, i)
		btn.onClick:AddListener(func_Handler)
		self.ctrlTab[i] = self:BindCtrlByNode(goTab, "Game.UI.TemplateEx.TemplateTabCtrl")
		self.ctrlTab[i]:SetText(self.tbPages[i].Name)
	end
	for i = 1, self.nPageCount do
		local nState = self:GetTabState(i)
		self.ctrlTab[i]:SetSelect(i == self.nCurTab, nState)
		if i == self.nCurTab and 1 < i then
			self.ctrlTab[i - 1]:SetLine(false)
		end
	end
end
function MallSkinCtrl:SetTimer(nTime)
	if self.timer ~= nil then
		self.timer:Cancel(false)
		self.timer = nil
	end
	if 0 < nTime then
		self.timer = self:AddTimer(1, nTime, function()
			self:Refresh(true)
			EventManager.Hit("MallCloseDetail")
		end, true, true, false)
	end
end
function MallSkinCtrl:GetTabState(nCurTab)
	local nState = 2
	if nCurTab == 1 then
		nState = 1
	elseif nCurTab == self.nPageCount then
		nState = 3
	end
	return nState
end
function MallSkinCtrl:OnGridRefresh(goGrid, gridIndex)
	local nIndex = gridIndex + 1
	local mapData = self.tbPackagePage[self.nCurTab][nIndex]
	local nInstanceID = goGrid:GetInstanceID()
	if not self.tbGridCtrl[nInstanceID] then
		self.tbGridCtrl[nInstanceID] = self:BindCtrlByNode(goGrid, "Game.UI.Mall.MallSkinItemCtrl")
	end
	self.tbGridCtrl[nInstanceID]:Refresh(mapData)
end
function MallSkinCtrl:OnGridBtnClick(goGrid, gridIndex)
	local nIndex = gridIndex + 1
	local mapData = self.tbPackagePage[self.nCurTab][nIndex]
	local mapCfg = ConfigTable.GetData("MallPackage", mapData.sId)
	if not mapCfg then
		return
	end
	local tbParam = decodeJson(mapCfg.Items)
	local nSkinId = 0
	for nId, v in pairs(tbParam) do
		nSkinId = tonumber(nId)
		break
	end
	local func = function()
		EventManager.Hit(EventId.OpenPanel, PanelId.MallSkinPreview, self.tbCurList, nSkinId, nIndex)
	end
	EventManager.Hit(EventId.SetTransition, 5, func)
end
function MallSkinCtrl:ResetTab()
	if not self.ctrlTab then
		return
	end
	if self.nCurTab and self.nCurTab > 1 then
		local nState = self:GetTabState(self.nCurTab)
		self.ctrlTab[self.nCurTab]:SetSelect(false, nState)
		self.ctrlTab[self.nCurTab - 1]:SetLine(true)
	end
	self.nCurTab = 1
	self.ctrlTab[self.nCurTab]:SetSelect(true, 1)
end
function MallSkinCtrl:Awake()
	self.tbGridCtrl = {}
	self._mapNode.goEmpty.gameObject:SetActive(false)
	self._mapNode.sv.gameObject:SetActive(false)
	self._mapNode.svTab.gameObject:SetActive(false)
end
function MallSkinCtrl:OnEnable()
end
function MallSkinCtrl:OnDisable()
	for nInstanceId, objCtrl in pairs(self.tbGridCtrl) do
		self:UnbindCtrlByNode(objCtrl)
		self.tbGridCtrl[nInstanceId] = nil
	end
	self.tbGridCtrl = {}
	if self.ctrlTab then
		for nTab, objCtrl in pairs(self.ctrlTab) do
			self:UnbindCtrlByNode(objCtrl)
			self.ctrlTab[nTab] = nil
		end
		self.ctrlTab = nil
	end
end
function MallSkinCtrl:OnDestroy()
end
function MallSkinCtrl:OnBtnClick_ChangeSheet(btn, nIndex)
	if nIndex == self.nCurTab then
		return
	end
	local nState = self:GetTabState(self.nCurTab)
	self.ctrlTab[self.nCurTab]:SetSelect(false, nState)
	if self.nCurTab > 1 then
		self.ctrlTab[self.nCurTab - 1]:SetLine(true)
	end
	nState = self:GetTabState(nIndex)
	self.ctrlTab[nIndex]:SetSelect(true, nState)
	if 1 < nIndex then
		self.ctrlTab[nIndex - 1]:SetLine(false)
	end
	self.nCurTab = nIndex
	self.nSelectPage = self.tbPages[self.nCurTab].Id
	self:RefreshList(true)
end
return MallSkinCtrl
