autoImport('IconManager')
autoImport('UIPriceDiscount')
autoImport('FuncZenyShop')
autoImport("ItemTipBaseCell")
ShopItemInfoCell = class("ShopItemInfoCell", ItemTipBaseCell)


UIListItemControllerBaihuoItemSale = class('UIListItemControllerBaihuoItemSale', BaseCell)

function UIListItemControllerBaihuoItemSale:Init()
	self:GetGameObjects()
end

function UIListItemControllerBaihuoItemSale:SetData(data)
	self.shopItemData = data

	self:GetModelSet()
	self:LoadView()
	self:RegisterClickEvent()
end

function UIListItemControllerBaihuoItemSale:GetGameObjects()
	self.spIcon = self:FindGO('Icon', self.gameObject):GetComponent(UISprite)
	self.labCount = self:FindGO('Count'):GetComponent(UILabel)
	self.labName = self:FindGO('Name', self.gameObject):GetComponent(UILabel)
	self.goPrice = self:FindGO('Price')
	self.labPrice = self:FindGO('Lab', self.goPrice):GetComponent(UILabel)
	self.spGoldIcon = self:FindGO('Icon', self.goPrice):GetComponent(UISprite)
	IconManager:SetItemIcon('item_6000006', self.spGoldIcon)
	self.goOriginPrice = self:FindGO('OriginPrice')
	self.labOriginPrice = self:FindGO('Lab', self.goOriginPrice):GetComponent(UILabel)
	self.goPriceBG = self:FindGO('BG', self.goPrice)
	self.goDiscount = self:FindGO('Discount', self.gameObject)
	self.goPercent = self:FindGO('Percent', self.goDiscount)
	self.labPercent = self:FindGO('Value1', self.goPercent):GetComponent(UILabel)
	self.labPercentSymbol = self:FindGO('Value2', self.goPercent):GetComponent(UILabel)
	self.spPercentBG = self:FindGO('BG', self.goPercent):GetComponent(UIMultiSprite)
	self.goSoldOut = self:FindGO('SoldOut')
end

function UIListItemControllerBaihuoItemSale:RegisterClickEvent()
	self:AddClickEvent(self.gameObject, function ()
		self:OnClickForSelf()
	end)
	self:AddClickEvent(self.goPriceBG, function ()
		self:OnClickForPriceView()
	end)
	self:AddClickEvent(self.spIcon.gameObject, function ()
		self:OnClickForIconView()
	end)
end

function UIListItemControllerBaihuoItemSale:GetModelSet()
	self.itemID = self.shopItemData.goodsID
	self.itemConf = Table_Item[self.itemID]
	self.canBuyCount = HappyShopProxy.Instance:GetCanBuyCount(self.shopItemData)
end

function UIListItemControllerBaihuoItemSale:LoadView()
	if(self.itemConf == nil)then
		redlog("Good Is Nil:", self.itemID);
		return;
	end
	IconManager:SetItemIcon(self.itemConf.Icon, self.spIcon)
	self.labCount.text = self.shopItemData.goodsCount
	self.labName.text = self.itemConf.NameZh
	self.price = self:GetDiscountPrice()
	self.labPrice.text = FuncZenyShop.FormatMilComma(self.price)
	local canBuyCount = HappyShopProxy.Instance:GetCanBuyCount(self.shopItemData)
	if self:IsSoldOut() then
		self.goSoldOut:SetActive(true)
	else
		self.goSoldOut:SetActive(false)
	end
	if self:IsDiscount() then
		self.goOriginPrice:SetActive(true)
		self.labOriginPrice.text = ZhString.HappyShop_originalCost .. FuncZenyShop.FormatMilComma(self.shopItemData.ItemCount)
		self.goDiscount:SetActive(true)
		self.labPercent.text = self.shopItemData.actDiscount
		local mulSpriteState, color = UIPriceDiscount.GetDiscountColor(self.shopItemData.actDiscount)
		self.labPercent.effectColor = color
		self.labPercentSymbol.effectColor = color
		self.spPercentBG.CurrentState = mulSpriteState
	else
		self.goOriginPrice:SetActive(false)
		self.goDiscount:SetActive(false)
	end
end

function UIListItemControllerBaihuoItemSale:OnClickForSelf()
	UISubViewControllerBaihuoShopItem.instance:LoadPurchaseDetailView(self.shopItemData)
	HappyShopProxy.Instance:SetSelectId(self.shopItemData.id)
end

function UIListItemControllerBaihuoItemSale:OnClickForPriceView()
	if self:IsHaveEnoughCostForBuy() then
		if not self.shopItemData:CheckCanRemove() then
			ServiceSessionShopProxy.Instance:CallBuyShopItem(self.shopItemData.id, 1)
		end
	end
end

function UIListItemControllerBaihuoItemSale:OnClickForIconView()
	local itemData = ItemData.new(nil, self.itemID)
	local tab = ReusableTable.CreateTable()
	tab.itemdata = itemData
	self:ShowItemTip(tab, UISubViewControllerBaihuoShopItem.instance.widgetTipRelative, NGUIUtil.AnchorSide.Center, {-148, -37})
	ReusableTable.DestroyAndClearTable(tab)
end

function UIListItemControllerBaihuoItemSale:IsHaveEnoughCostForBuy()
	local bCatGold = MyselfProxy.Instance:GetLottery()
	if bCatGold < self.price then
		local sysMsgID = 3634
		MsgManager.ShowMsgByID(sysMsgID)
		return false
	else
		return true
	end
end

function UIListItemControllerBaihuoItemSale:GetDiscountPrice()
	local retValue = nil
	if self.shopItemData.actDiscount == 0 then
		retValue = self.shopItemData.ItemCount
	else
		retValue = self.shopItemData.ItemCount * self.shopItemData.actDiscount / 100
	end
	return math.ceil(retValue)
end

function UIListItemControllerBaihuoItemSale:IsDiscount()
	return self:GetDiscountPrice() < self.shopItemData.ItemCount
end

function UIListItemControllerBaihuoItemSale:IsSoldOut()
	return self.canBuyCount ~= nil and self.canBuyCount <= 0
end