local HotSellCell = class("HotSellCell", BaseCell)
local HOT_CELL = "app/ui/shop/cell/hot_cell"
local CELL_COUNT = 6

function HotSellCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.cellHeight = self.baseObject:fastGetSizeDeltaY() + 20

	uiMap["hot_sell_cell.title_bg.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_28)) -- 每日特惠
	uiMap["hot_sell_cell.help_btn"]:addClickListener(function()
		ModuleManager.TipsManager:showDescTips(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_29), uiMap["hot_sell_cell.help_btn"])
	end)
	self.time1Tx = uiMap["hot_sell_cell.time_icon_1.text"]
	self.time2Tx = uiMap["hot_sell_cell.time_icon_2.text"]
	self.time3Tx = uiMap["hot_sell_cell.time_icon_3.text"]
	self.cells = {}
	for i = 1, CELL_COUNT do
		local cellObj = uiMap["hot_sell_cell.cell_" .. i]
		local cell = cellObj:addLuaComponent(HOT_CELL)
		table.insert(self.cells, cell)
	end

	self.refreshBtn = uiMap["hot_sell_cell.refresh_btn"]
	self.refreshAdImg = uiMap["hot_sell_cell.refresh_btn.ad_img"]
	self.refreshGemImg = uiMap["hot_sell_cell.refresh_btn.gem_img"] 
	self.refreshText = uiMap["hot_sell_cell.refresh_btn.desc"] 

	self.refreshBtn:addClickListener(function()
		self:onClickRefresh()
	end)
end

function HotSellCell:refresh()
	local goods = DataManager.ShopData:getMallDailyGoods()
	local freeRefreshCount = DataManager.ShopData:getMallDailyAdLeftCount() -- 剩余免费刷新次数
	local gemRefreshCount = DataManager.ShopData:getMallDailyDiamondLeftCount() -- 剩余钻石刷新次数
	local gemRefreshCost = DataManager.ShopData:getMallDailyDiamondResetCost() -- 钻石刷新消耗
	if freeRefreshCount > 0 then
		self.refreshBtn:setActive(true)
		self.refreshAdImg:setVisible(true)
		GFunc.setAdsSprite(self.refreshAdImg)
		self.refreshGemImg:setVisible(false)
		self.refreshText:setText(I18N:getGlobalText(I18N.GlobalConst.STR_REFRESH))
		self.refreshBtn:addRedPoint(80, 30, 0.6)
	elseif gemRefreshCount > 0 then
		self.refreshBtn:setActive(true)
		self.refreshAdImg:setVisible(false)
		self.refreshGemImg:setVisible(true)
		self.refreshText:setText(gemRefreshCost)
		self.refreshBtn:removeRedPoint()
	else
		self.refreshBtn:setActive(false)
		self.refreshBtn:removeRedPoint()
	end

	local goodsCount = goods and #goods or 0
	for i = 1, CELL_COUNT do
		if i <= goodsCount then
			self.cells[i]:setVisible(true)
			self.cells[i]:refresh(goods[i])
		else
			self.cells[i]:setVisible(false)
		end
	end
end

function HotSellCell:refreshTime()
	local remainTime = Time:getOverOfServerToday() - Time:getServerTime()
	if remainTime < 0 then
		remainTime = 0
	end
	local h,m,s = GFunc.getTimeWithHMS(remainTime)
	self.time1Tx:setText(h)
	self.time2Tx:setText(m)
	self.time3Tx:setText(s)
end

function HotSellCell:getCellHeight()
	return self.cellHeight
end

function HotSellCell:getIsOpen()
	return ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.MALL_DAILY, true)
end

function HotSellCell:setVisible(visible)
	self.baseObject:setVisible(visible)
end

function HotSellCell:onClickRefresh()
	local freeRefreshCount = DataManager.ShopData:getMallDailyAdLeftCount() -- 免费刷新次数
	local gemRefreshCount = DataManager.ShopData:getMallDailyDiamondLeftCount() -- 钻石刷新次数
	local gemRefreshCost = DataManager.ShopData:getMallDailyDiamondResetCost() -- 钻石刷新消耗
	
	if freeRefreshCount > 0 then
		SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.MALL_DAILY_RESET, function ()
			ModuleManager.ShopManager:resetMallDailyGift(1)
		end)
	elseif gemRefreshCount > 0 then
		if GFunc.checkCost(GConst.ItemConst.ITEM_ID_GEM, gemRefreshCost, true) then
			-- 二次提示
			local params = {
				content = I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_30), -- 是否使用钻石刷新每日特惠?
				boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
				okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
				okFunc = function()
					ModuleManager.ShopManager:resetMallDailyGift(2)
				end
			}
			GFunc.showMessageBox(params)
		end
	end
end

return HotSellCell