local baseCell = autoImport("BaseCell")
ExchangeShopItemCell = class("ExchangeShopItemCell", baseCell)

local ICON_BG_TEX = "guild_bg_05"

local EMPTY = ExchangeShopProxy.GoodsTYPE.EMPTY

function ExchangeShopItemCell:Init()
	self:FindObjs()
	self:AddEvts()
end

local BTN_CONTENT_CFG = 
{
	[ExchangeShopProxy.EnchangeType.FRESS] = ZhString.ExchangeShop_Free,
	[ExchangeShopProxy.EnchangeType.PROGRESS] = ZhString.ExchangeShop_Donate,
	[ExchangeShopProxy.EnchangeType.NO_PROGRESS] = ZhString.ExchangeShop_Exchange,
	[ExchangeShopProxy.EnchangeType.Limited_PROGRESS] = ZhString.ExchangeShop_Exchange,
}

function ExchangeShopItemCell:FindObjs()
	self.btn = self:FindGO("Btn")
	self.costPos = self:FindGO("CostPos")
	self.name = self:FindComponent("Name",UILabel)
	self.icon = self:FindComponent("Icon", UISprite)
	self.iconBg = self:FindComponent("IconBg",UITexture)
	self.btnContent = self:FindComponent("BtnContent",UILabel)
	self.costItemIcon = self:FindComponent("CostItemIcon",UISprite)
	self.costItemNum = self:FindComponent("CostItemNum",UILabel)
	self.empty = self:FindGO("Empty")
	self.desContent = self:FindComponent("Desc",UILabel)
	self.leftTime = self:FindComponent("LeftTime",UILabel)
end

function ExchangeShopItemCell:AddEvts()
	self:AddClickEvent(self.btn.gameObject, function ()
		if(self.data and self.data.status~=ExchangeShopProxy.GoodsTYPE.EMPTY)then
			self:PassEvent(MouseEvent.MouseClick, self)
		end
	end)
	self:AddCellClickEvent()
end

function ExchangeShopItemCell:SetData(data)
	self.data = data
	if data then
		self:Show(self.gameObject)
		PictureManager.Instance:SetGuildBuilding(ICON_BG_TEX,self.iconBg)
		if data.staticData then
			self.empty:SetActive(EMPTY==data.status)
			self.btn:SetActive(EMPTY~=data.status)
			self.leftTime.gameObject:SetActive(EMPTY~=data.status)
			if(EMPTY~=data.status)then 
				self:SetBtnState() 
				self:ResetLeftTime()
			end
			self.desContent.text = data.staticData.Desc
			self.name.text=data.staticData.Name
			if(false==IconManager:SetItemIcon(data.staticData.Icon, self.icon))then
				IconManager:SetUIIcon(data.staticData.Icon, self.icon)
			end
		end
	else
		self:Hide(self.gameObject)
	end
end

function ExchangeShopItemCell:ResetLeftTime()
	if nil~=self.data.leftTime and 0<self.data:GetLeftTime() then
		if(nil~=self.timeTick)then
			self:ClearTick()
		end
		self.timeTick = TimeTickManager.Me():CreateTick(0, 1000, self.UpdateLeftTime, self)
		self:Show(self.leftTime)
	else
		self:Hide(self.leftTime)
		self:ClearTick()
	end
end

function ExchangeShopItemCell:UpdateLeftTime()
	if not self.data then 
		self:Hide(self.leftTime)
		self:ClearTick()
		return
	end
	local totalSec = self.data:GetLeftTime()
	if totalSec > 0 then
		local day,hour,min,sec = ClientTimeUtil.FormatTimeBySec(totalSec)
		self.leftTime.text = day>0 and string.format(ZhString.QuotaCard_Surplus,day) or 
		string.format(ZhString.MVPFightInfoBord_LeftTime, hour, min, sec)
	end
end

function ExchangeShopItemCell:SetBtnState()
	local staticData = self.data.staticData
	if(staticData.ExchangeType == ExchangeShopProxy.EnchangeType.COINS)then
		self:Show(self.costPos)
		self:Show(self.costItemIcon)
		self:Hide(self.btnContent)
		local costCFG = staticData.Cost
		if(2>#costCFG)then
			-- errorLog("ExchangeShop 未配置兌換消耗欄位 ,errorID: "..staticData.ID)
			return
		end
		local _icon = Table_Item[costCFG[1]] and Table_Item[costCFG[1]].Icon or ""
		self.costItemNum.text = costCFG[2]
		if(false==IconManager:SetItemIcon(_icon,self.costItemIcon))then
			IconManager:SetUIIcon(_icon, self.costItemIcon)
		end
	else
		self:Hide(self.costPos)
		self:Hide(self.costItemIcon)
		self:Show(self.btnContent)
		self.btnContent.text = BTN_CONTENT_CFG[staticData.ExchangeType]
	end
end

function ExchangeShopItemCell:ClearTick()
	if nil ~= self.timeTick then
		TimeTickManager.Me():ClearTick(self)
		self.timeTick = nil
	end
end

function ExchangeShopItemCell:OnDestroy()
	self:ClearTick()
	PictureManager.Instance:UnloadGuildBuilding()
end

