autoImport('UIModelBaihuoShop')
autoImport('UIListItemControllerBaihuoItemSale')
autoImport('HappyShopBuyItemCell')

UISubViewControllerBaihuoShopItem = class('UISubViewControllerBaihuoShopItem', SubView)

UISubViewControllerBaihuoShopItem.instance = nil

function UISubViewControllerBaihuoShopItem:Init()
	
end

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

	UISubViewControllerBaihuoShopItem.instance = nil
	if self.puchaseDetailCtrl ~= nil then
		self.puchaseDetailCtrl:Exit()
	end
	self:CancelListenServerResponse()
end

function UISubViewControllerBaihuoShopItem:MyInit()
	UISubViewControllerBaihuoShopItem.instance = self

	self.gameObject = self:LoadPreferb('view/UISubViewZenyShopItem', nil, true)

	self:GetGameObjects()
	self:SetActivePurchaseDetail(false)
	self:GetModelSet()
	self:LoadView()
	self:ListenServerResponse()

	UIModelBaihuoShop.Ins():RequestQueryShopItem(UIModelBaihuoShop.ItemShopType, UIModelBaihuoShop.ItemShopID)

	HappyShopProxy.Instance:InitShop(nil, UIModelBaihuoShop.ItemShopID, UIModelBaihuoShop.ItemShopType)
	self.isInit = true
end

function UISubViewControllerBaihuoShopItem:ReInit()
	self:SetActivePurchaseDetail(false)
end

function UISubViewControllerBaihuoShopItem:GetGameObjects()
	self.goItemsList = self:FindGO('ItemsList')
	self.uiGridItem = self:FindGO('ItemsRoot', self.goItemsList):GetComponent(UIGrid)
	self.widgetTipRelative = self:FindGO('TipRelative'):GetComponent(UIWidget)
	self.goPurchaseDetail = self:LoadPreferb('cell/HappyShopBuyItemCell', self.gameObject, true)
end

function UISubViewControllerBaihuoShopItem:GetModelSet()
	self.shopItemDatas = UIModelBaihuoShop.Ins():GetItemShopConf()
	if self.arrShopItemData == nil then
		self.arrShopItemData = {}
	end
	TableUtility.ArrayClear(self.arrShopItemData)
	for k, v in pairs(self.shopItemDatas) do
		table.insert(self.arrShopItemData, v)
	end
	table.sort(self.arrShopItemData, function (x, y)
		return x.ShopOrder < y.ShopOrder
	end)
end

function UISubViewControllerBaihuoShopItem:LoadView()
	if self.itemListCtrl == nil then
		self.itemListCtrl = UIGridListCtrl.new(self.uiGridItem, UIListItemControllerBaihuoItemSale, 'UIListItemItemSale')
	end
	self.itemListCtrl:ResetDatas(self.arrShopItemData)
end

function UISubViewControllerBaihuoShopItem:LoadPurchaseDetailView(shop_item_data)
	self:SetActivePurchaseDetail(true)
	if self.puchaseDetailCtrl == nil then
		self.puchaseDetailCtrl = HappyShopBuyItemCell.new(self.goPurchaseDetail)
	end
	self.puchaseDetailCtrl:SetData(shop_item_data)
end

function UISubViewControllerBaihuoShopItem:ListenServerResponse()
	EventManager.Me():AddEventListener(ServiceEvent.SessionShopQueryShopConfigCmd, self.OnReceiveQueryShopConfigCmd, self)
	EventManager.Me():AddEventListener(ServiceEvent.ItemGetCountItemCmd, self.OnReceiveItemGetCount, self)
	EventManager.Me():AddEventListener(ServiceEvent.NUserUpdateShopGotItem, self.OnReceiveUpdateShopGotItem, self)
end

function UISubViewControllerBaihuoShopItem:CancelListenServerResponse()
	EventManager.Me():RemoveEventListener(ServiceEvent.SessionShopQueryShopConfigCmd, self.OnReceiveQueryShopConfigCmd, self)
	EventManager.Me():RemoveEventListener(ServiceEvent.ItemGetCountItemCmd, self.OnReceiveItemGetCount, self)
	EventManager.Me():RemoveEventListener(ServiceEvent.NUserUpdateShopGotItem, self.OnReceiveUpdateShopGotItem, self)
end

function UISubViewControllerBaihuoShopItem:OnReceiveQueryShopConfigCmd(message)
	self:GetModelSet()
	self:LoadView()
end

function UISubViewControllerBaihuoShopItem:OnReceiveItemGetCount(data)
	if data then
		if self.puchaseDetailCtrl ~= nil then
			self.puchaseDetailCtrl:SetItemGetCount(data)
		end
	end
end


function UISubViewControllerBaihuoShopItem:OnReceiveUpdateShopGotItem(data)
	self:GetModelSet()
	self:LoadView()
end

function UISubViewControllerBaihuoShopItem:SetActivePurchaseDetail(b)
	self.goPurchaseDetail:SetActive(b)
end