local BaseCell = require (GConst.TYPEOF_LUA_CLASS.BASE_CELL)
local ResourceCell = class("ResourceCell", BaseCell)

function ResourceCell:ctor()

end

function ResourceCell:init()
	local uiMap = self:getUIMap()

	self.resImg = uiMap['currency_cell.res_img']
	self.addImg = uiMap['currency_cell.add_img']
	self.numTx = uiMap['currency_cell.num_tx']
	self.timeTx = uiMap['currency_cell.time_tx']
	self.addTx = uiMap["currency_cell.add_tx"]
end

function ResourceCell:show(itemId, hideAddImg)
	hideAddImg = hideAddImg or false
	self:hide()
	if not itemId then
		return
	end
	self.baseObject:setActive(true)

	local obj = DataManager.BagData.ItemData:getItemById(itemId)
	self.resImg:setSprite(obj:getIconRes())

	self:unBindAll()
	self:bind(obj, "isDirty", function(binder, value)
		self:refreshTextRightNow()
	end)

	if itemId == GConst.ItemConst.ITEM_ID_VIT then
		self.baseObject:addClickListener(function()
			ModuleManager.CommerceManager:showBuyVitUI()
		end)
		self.addImg:setVisible(true)
		self.timeTx:setVisible(true)
	elseif itemId == GConst.ItemConst.ITEM_ID_GOLD then
		self.baseObject:addClickListener(function()
			EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.GO_SHOP, {page = GConst.ShopConst.PAGE_TYPE.MAIN, subType = GConst.ShopConst.MAIN_PAGE_TYPE.GOLD})
		end)
		self.addImg:setVisible(true)
	elseif itemId == GConst.ItemConst.ITEM_ID_GEM then
		self.baseObject:addClickListener(function()
			EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.GO_SHOP, {page = GConst.ShopConst.PAGE_TYPE.MAIN, subType = GConst.ShopConst.MAIN_PAGE_TYPE.GEM})
		end)
		self.addImg:setVisible(true)
	elseif itemId == GConst.ItemConst.ITEM_ID_ARENA_TICKET then
		self.baseObject:addClickListener(function()
			ModuleManager.CommerceManager:showBuyArenaTicketUI()
		end)
		self.addImg:setVisible(true)
	elseif itemId == GConst.ItemConst.ITEM_ID_SLIVER_WING then
		self.baseObject:addClickListener(function()
			local runeData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.RUNES_OPEN)
			local reward = {
				type = GConst.REWARD_TYPE.ITEM,
				id = GConst.ItemConst.ITEM_ID_SLIVER_WING,
				num = 1
			}
			ModuleManager.CommonManager:showExchangeUI(1, runeData:getRemainSliverWingCount(), reward, runeData:getBuySliverCost(), function(count)
				ModuleManager.DungeonRuneManager:reqBuySliverWing(count)
			end)
		end)
		self.addImg:setVisible(true)
	else
		self.baseObject:removeClickListener()
		self.addImg:setVisible(false)
		self.timeTx:setVisible(false)
	end
	self.itemId = itemId
end

function ResourceCell:updateTime()
	if self.itemId and DataManager.BagData.ItemData:isTimingRecovery(self.itemId) then
		local curTime = DataManager.BagData.ItemData:getTimelyItemRecoveryTime(self.itemId)
		if curTime <= 0 then
			self.timeTx:setText(GConst.EMPTY_STRING)
		else
			self.timeTx:setText(GFunc.getTimeStrWithMS(curTime))
		end
	end
end

function ResourceCell:hide()
	self.baseObject:setActive(false)
end

function ResourceCell:setAnchoredPosition(x, y)
	self.baseObject:setAnchoredPosition(x, y, 0)
end

function ResourceCell:getAnchoredPosition()
	return self.baseObject:getAnchoredPosition()
end

function ResourceCell:refreshTextRightNow()
	if not self.itemId then
		return
	end
	local obj = DataManager.BagData.ItemData:getItemById(self.itemId)
	local count = obj:getNum()
	if count < 0 then
		self.numTx:setText("0")
	elseif obj:getId() == GConst.ItemConst.ITEM_ID_VIT then
		self.numTx:setText(GFunc.num2Str(count) .. "/" .. DataManager.PlayerData:getMaxVit())
	elseif obj:getId() == GConst.ItemConst.ITEM_ID_ARENA_TICKET then
		self.numTx:setText(GFunc.num2Str(count) .. "/" .. DataManager.PlayerData:getMaxArenaTicket())
	else
		self.numTx:setText(GFunc.num2Str(count))
	end
end

function ResourceCell:refreshText()
	if self.numTx == nil then
		return
	end
	local text = self.numTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).text
	self.numTx:setText(GConst.EMPTY_STRING)
	self.numTx:setText(text)
end

function ResourceCell:doScaleFlyImg()
	GFunc.doScaleFlyImg(self.resImg)
end

return ResourceCell