autoImport('UIListItemCtrlLuckyBag')
autoImport('UIListItemViewControllerZenyShopItem')
autoImport('UIModelZenyShop')
autoImport('UIModelMonthlyVIP')
autoImport('UISubViewControllerMonthlyVIP')
autoImport('UISubViewControllerZenyList')
autoImport('UISubViewControllerGachaCoin')
autoImport('UISubViewControllerBaihuoShopItem')
autoImport('FuncZenyShop')

UIViewControllerBaihuoShop = class('UIViewControllerBaihuoShop', ContainerView)

UIViewControllerBaihuoShop.ViewType = UIViewType.NormalLayer

local reusableArray = {}

UIViewControllerBaihuoShop.instance = nil

function UIViewControllerBaihuoShop:Init()
	UIViewControllerBaihuoShop.instance = self

	-- UIModelZenyShop.Ins():RequestQueryShopItem()

	self:GetGameObjects()
	self:RegisterButtonClickEvent()
	self:BGAdapt()
	self:LoadView()

	self.zenyController = nil --self:AddSubView('UISubViewControllerZenyList', UISubViewControllerZenyList)
	self.monthlyVIPController = nil -- self:AddSubView('UISubViewControllerMonthlyVIP', UISubViewControllerMonthlyVIP)
	self.gachaCoinController =  nil --self:AddSubView('UISubViewControllerGachaCoin', UISubViewControllerGachaCoin)
	self.itemController = self:AddSubView('UISubViewControllerBaihuoShopItem', UISubViewControllerBaihuoShopItem)

	--local tabZeny = PanelConfig.ZenyShop.tab
	--local tabMonthlyVIP = PanelConfig.ZenyShopMonthlyVIP.tab
	--local tabGachaCoin = PanelConfig.ZenyShopGachaCoin.tab
	local tabItem = PanelConfig.BaihuoShop.tab
	self.subViewControllers = {}
	--self.subViewControllers[tabZeny] = self.zenyController
	--self.subViewControllers[tabMonthlyVIP] = self.monthlyVIPController
	--self.subViewControllers[tabGachaCoin] = self.gachaCoinController
	self.subViewControllers[tabItem] = self.itemController
	--self:AddTabChangeEvent(self.sGOSelection[tabZeny], nil, tabZeny)
	--self:AddTabChangeEvent(self.sGOSelection[tabMonthlyVIP], nil, tabMonthlyVIP)
	--self:AddTabChangeEvent(self.sGOSelection[tabGachaCoin], nil, tabGachaCoin)
	self:AddTabChangeEvent(self.sGOSelection[tabItem], nil, tabItem)


	-- 新增
	--local coinTabItem = PanelConfig.BaihuoShopZeny.tab
	--self.itemController = self:AddSubView('UISubViewControllerZenyShopItem', UISubViewControllerZenyShopItem)
	--self.subViewControllers[coinTabItem] = self.itemController
	--self:AddTabChangeEvent(self.sGOSelection[coinTabItem], nil, coinTabItem)


	self:TabChangeHandler(self.viewdata.view.tab)
	-- self:RegisterZenyIconTargetPosition()
	-- self:SetZenyIconRoot()
	-- self:SetZenyIconCount()
	self:ListenServerResponse()
end

function UIViewControllerBaihuoShop:OnExit()
	UIViewControllerBaihuoShop.super.OnExit(self)

	UIViewControllerBaihuoShop.instance = nil
	FuncZenyShop.Instance():ClearProductPurchase()
end

function UIViewControllerBaihuoShop:GetGameObjects()
	self.goBTNBack = self:FindGO('BTN_Back', self.gameObject)
	self.goZenyBalance = self:FindGO('ZenyBalance', self.gameObject)
	self.goLabZenyBalance = self:FindGO('Lab', self.goZenyBalance)
	self.labZenyBalance = self.goLabZenyBalance:GetComponent(UILabel)
	self.spZeny = self:FindGO('Icon', self.goZenyBalance):GetComponent(UISprite)
	IconManager:SetItemIcon('item_100', self.spZeny)

	--Gold币
	self.goGoldBalance = self:FindGO('GoldBalance', self.gameObject)
	self.goLabGoldBalance = self:FindGO('Lab', self.goGoldBalance)
	self.labGoldBalance = self.goLabGoldBalance:GetComponent(UILabel)
	self.spGold = self:FindGO('Icon', self.goGoldBalance):GetComponent(UISprite)
	IconManager:SetItemIcon('item_6000006', self.spGold)	--设置图标


	self.goGachaCoinBalance = self:FindGO('GachaCoinBalance', self.gameObject)
	self.goLabGachaCoinBalance = self:FindGO('Lab', self.goGachaCoinBalance)
	self.labGachaCoinBalance = self.goLabGachaCoinBalance:GetComponent(UILabel)
	self.spGachaCoin = self:FindGO('Icon', self.goGachaCoinBalance):GetComponent(UISprite)
	IconManager:SetItemIcon('item_151', self.spGachaCoin)
	self.goZenyIconRoots = self:FindGO('ZenyIconRoots', self.gameObject)
	self.goRootTemplate = self:FindGO('RootTemplate', self.gameObject)
	self.goPanelDynamicNPC = self:FindGO('PanelDynamicNPC', self.gameObject)
	self.goDynamicNPC = self:FindGO('DynamicNPC', self.goPanelDynamicNPC)
	self.texDynamicNPC = self.goDynamicNPC:GetComponent(UITexture)
	self.widgetTipRelative = self:FindGO('TipRelativeWidget', self.gameObject):GetComponent(UIWidget)
	self.goBG = self:FindGO('BG', self.gameObject)
	self.texBG = self.goBG:GetComponent(UITexture)
	self.goTip = self:FindGO('Tip', self.gameObject)
	self.spFrame = self:FindGO('Frame', self.goTip):GetComponent(UISprite)
	self.goSelections = self:FindGO('Selections')
	self.goSelectionTemplate = self:FindGO('Selection', self.goSelections)
end

function UIViewControllerBaihuoShop:RegisterButtonClickEvent()
	self:AddClickEvent(self.goBTNBack, function ()
		self:OnClickForButtonBack()
	end)
end

function UIViewControllerBaihuoShop:GetMonthCardConfigure()
	local year = UIModelMonthlyVIP.YearOfServer(-18000)
	local month = UIModelMonthlyVIP.MonthOfServer(-18000)
	for _, v in pairs(Table_MonthCard) do
		if v.Year == year and v.Month == month then
			return v
		end
	end
	return nil
end

function UIViewControllerBaihuoShop:LoadView()
	self:LoadZenyBalanceView()
	if self.goSelections.transform.childCount <= 1 then
		self.sGOSelection = {}
		self.sSpBGSelection = {}
		self.sLabTitleSelection = {}
		local functionCount = 0
		local functions = GameConfig.BaihuoShop.Functions
		for i = 1, #functions do
			local tab = functions[i].Tab
			local isForbid = false
			if tab == PanelConfig.BaihuoShop.tab then
			--	isForbid = GameConfig.SystemForbid.ZenyPurchase
			--elseif tab == PanelConfig.ZenyShopMonthlyVIP.tab then
			--	isForbid = GameConfig.SystemForbid.CardPurchase
			--elseif tab == PanelConfig.ZenyShopGachaCoin.tab then
			--	isForbid = GameConfig.SystemForbid.GoldPurchase
			--elseif tab == PanelConfig.ZenyShopItem.tab then
				isForbid = GameConfig.SystemForbid.ItemPurchase
			end
			if not isForbid then
				functionCount = functionCount + 1

				local functionName = functions[i].Title
				local goSelection = GameObject.Instantiate(self.goSelectionTemplate)
				goSelection.transform:SetParent(self.goSelections.transform, false)
				goSelection.name = tab
				goSelection:SetActive(true)
				local labTitle = self:FindGO('Title', goSelection):GetComponent(UILabel)
				labTitle.text = functionName
				self.sGOSelection[tab] = goSelection
				self.sSpBGSelection[tab] = self:FindGO('BG', goSelection):GetComponent(UISprite)
				self.sLabTitleSelection[tab] = labTitle
			end
		end
		self.goSelections:GetComponent(UIGrid).repositionNow = true

		local widthOfTipFrame = self.spFrame.width
		widthOfTipFrame = widthOfTipFrame + functionCount * 180
		self.spFrame.width = widthOfTipFrame
	end
	local monthCardConfigure = self:GetMonthCardConfigure()
	if monthCardConfigure ~= nil then
		PictureManager.Instance:SetZenyShopNPC(monthCardConfigure.NpcPicture, self.texDynamicNPC)
	end
end

function UIViewControllerBaihuoShop:LoadZenyBalanceView()
	local milCommaBalance = FuncZenyShop.FormatMilComma(MyselfProxy.Instance:GetROB())
	if milCommaBalance then
		self.labZenyBalance.text = milCommaBalance
	end
	milCommaBalance = FuncZenyShop.FormatMilComma(MyselfProxy.Instance:GetLottery())
	if milCommaBalance then
		self.labGachaCoinBalance.text = milCommaBalance
	end
	milCommaBalance = FuncZenyShop.FormatMilComma(MyselfProxy.Instance:GetDiamond())
	if milCommaBalance then
		self.labGoldBalance.text = milCommaBalance
	end
end

function UIViewControllerBaihuoShop:OnClickForButtonBack()
	-- local zenyBalance = MyselfProxy.Instance:GetROB() or 0
	-- local digitsValue = {}
	-- local isBreak = false
	-- local digit = 10
	-- while not isBreak do
	-- 	local quotient = math.CalculateQuotient(zenyBalance, digit)
	-- 	local remainder = math.CalculateRemainder(zenyBalance, digit)
	-- 	table.insert(digitsValue, remainder)
	-- 	if quotient == 0 then
	-- 		break
	-- 	else
	-- 		zenyBalance = quotient
	-- 	end
	-- end
	-- for k, v in pairs(digitsValue) do
	-- 	if k > 0 and k < 7 then
	-- 		if v == 1 then
	-- 			self:CachePurchasedItem(k)
	-- 		end
	-- 	end
	-- end
	if self.zenyController ~= nil then
		local purchasedShopItems = self.zenyController:GetCachedPurchaseItem()
		if purchasedShopItems ~= nil then
			self:ShowAnimationConfirmView()
		end
	end

	self:CloseSelf()
end

function UIViewControllerBaihuoShop:TabChangeHandler(tab)
	local subViewController = self.subViewControllers[tab]
	if not subViewController.isInit then
		subViewController:MyInit()
	end
	subViewController.gameObject:SetActive(true)
	if tab == PanelConfig.BaihuoShop.tab then
		subViewController:ReInit()
	end
	if self.currentSubViewCtrl ~= nil then
		if self.currentSubViewCtrl == subViewController then
			return
		else
			self.currentSubViewCtrl.gameObject:SetActive(false)
		end
	end
	self.currentSubViewCtrl = subViewController

	-- self.goZenyBalance:SetActive(false)
	-- self.goGachaCoinBalance:SetActive(tab == PanelConfig.ZenyShopGachaCoin.tab or tab == PanelConfig.BaihuoShop.tab)
	
	self.goZenyBalance:SetActive(true)
	self.goGachaCoinBalance:SetActive(true)
	
	self:ChangeSelection(self.sSpBGSelection[tab], self.sLabTitleSelection[tab])
end

function UIViewControllerBaihuoShop:ListenServerResponse()
	self:AddListenEvt(MyselfEvent.MyDataChange, self.OnReceiveEventMyDataChange)
	self:AddListenEvt(ServiceEvent.SessionShopQueryShopConfigCmd, nil)
	self:AddListenEvt(ServiceEvent.ItemGetCountItemCmd, nil)
	self:AddListenEvt(ServiceEvent.NUserUpdateShopGotItem, nil)
end

function UIViewControllerBaihuoShop:OnReceiveEventMyDataChange(data)
	self:LoadZenyBalanceView()
end

function UIViewControllerBaihuoShop:RegisterZenyIconTargetPosition()
	if self.itemsController then
		for _, v in pairs(self.itemsController) do
			local itemController = v
			itemController:RegisterZenyIconTargetPosition(self.goZenyBalance.transform.position)
		end
	end
end

function UIViewControllerBaihuoShop:SetZenyIconRoot(trans)
	if self.itemsController then
		for _, v in pairs(self.itemsController) do
			local itemController = v

			local goCopyRootTemplate = GameObject.Instantiate(self.goRootTemplate)
			local transCopyRootTemplate = goCopyRootTemplate.transform
			transCopyRootTemplate:SetParent(self.goZenyIconRoots.transform)
			local pos = transCopyRootTemplate.localPosition
			pos.x = 0; pos.y = 0; pos.z = 0
			transCopyRootTemplate.localPosition = pos
			local rotation = transCopyRootTemplate.localRotation
			local eulerAngles = rotation.eulerAngles
			eulerAngles.x = 0; eulerAngles.y = 0; eulerAngles.z = 0
			rotation.eulerAngles = eulerAngles
			transCopyRootTemplate.localRotation = rotation
			local scale = transCopyRootTemplate.localScale
			scale.x = 1; scale.y = 1; scale.z = 1
			transCopyRootTemplate.localScale = scale

			itemController:SetZenyIconRoot(transCopyRootTemplate)
		end
	end
end

function UIViewControllerBaihuoShop:SetZenyIconCount()
	if self.itemsController then
		for _, v in pairs(self.itemsController) do
			local itemController = v
			itemController:SetZenyIconCount()
		end
	end
end

local gColorNormal = Color32(62, 89, 167, 255)
local gColorSelected = Color32(182, 113, 42, 255)
function UIViewControllerBaihuoShop:ChangeSelection(sp_frame, lab_title)
	sp_frame.spriteName = 'recharge_btn_1'
	if self.spBGCurrentSelection ~= nil and self.spBGCurrentSelection ~= sp_frame then
		self.spBGCurrentSelection.spriteName = 'recharge_btn_2'
	end
	self.spBGCurrentSelection = sp_frame
	lab_title.color = gColorSelected
	if self.labTitleCurrentSelection ~= nil and self.labTitleCurrentSelection ~= lab_title then
		self.labTitleCurrentSelection.color = gColorNormal
	end
	self.labTitleCurrentSelection = lab_title
end

function UIViewControllerBaihuoShop:ShowAnimationConfirmView()
	MsgManager.ConfirmMsgByID(201, function ()
		self:OnAnimationConfirm()
	end, function ()
		self:OnAnimationCancel()
	end)
end

function UIViewControllerBaihuoShop:OnAnimationConfirm()
	local purchasedShopItems = self.zenyController:GetCachedPurchaseItem()
	self:RequestStartAnimation(purchasedShopItems)
end

function UIViewControllerBaihuoShop:OnAnimationCancel()
	
end

function UIViewControllerBaihuoShop:RequestStartAnimation(purchaseShopItems)
	ServiceNUserProxy.Instance:CallChargePlayUserCmd(purchaseShopItems)
end

function UIViewControllerBaihuoShop:BGAdapt()
	if not ApplicationInfo.IsIphoneX() then
		self.texBG.leftAnchor.target = nil
		self.texBG.rightAnchor.target = nil
		self.texBG.topAnchor.target = nil
		self.texBG.bottomAnchor.target = nil
		self.texBG.width = 1288
	end
end

function UIViewControllerBaihuoShop:OnEnter()
	UIViewControllerBaihuoShop.super.OnEnter(self);
	helplog("UIViewControllerBaihuoShop OnEnter");
	ServiceUserEventProxy.Instance:CallQueryChargeCnt();

	local gOManager_Camera = Game.GameObjectManagers[Game.GameObjectType.Camera];
	gOManager_Camera:ActiveMainCamera(false);
end

function UIViewControllerBaihuoShop:OnExit()
	UIViewControllerBaihuoShop.super.OnExit(self);

	local gOManager_Camera = Game.GameObjectManagers[Game.GameObjectType.Camera];
	gOManager_Camera:ActiveMainCamera(true);
end