local HotCell = class("HotCell", BaseCell)

local AD_REWARD_ICON = {
    [GConst.ItemConst.ITEM_ID_GOLD] = "shop_species_1",
    [GConst.ItemConst.ITEM_ID_GEM] = "shop_diamond_1"
}

function HotCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.bg = uiMap["hot_cell.bg"]
    self.icon = uiMap["hot_cell.icon"]
    self.nameText = uiMap["hot_cell.name"]
    self.adNode = uiMap["hot_cell.ad"]
    self.adImg = uiMap["hot_cell.ad.image"]
    self.adText = uiMap["hot_cell.ad.desc"] 
    self.costNode = uiMap["hot_cell.cost"]
    self.costImg = uiMap["hot_cell.cost.icon"]
    self.costText = uiMap["hot_cell.cost.num"]
    self.heroNode = uiMap["hot_cell.hero"]
    self.heroCell = CellManager:addCellComp(uiMap["hot_cell.hero.hero_cell"], GConst.TYPEOF_LUA_CLASS.HERO_CELL) 
    self.heroNumText = uiMap["hot_cell.hero.num_tx"]
    self.sellOutText = uiMap["hot_cell.sell_out"]

    self.sellOutText:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20)) -- 已购买
end

function HotCell:refresh(data)
    self.data = data
    local id = data.id
    local index = data.good_index
    local bought = data.bought
    local cfgInfo = DataManager.ShopData:getMallDailyConfig()[id]
    local reward = cfgInfo.good and cfgInfo.good[index]
    local cost = cfgInfo.cost and cfgInfo.cost[index]

    local isHeroReward = id ~= 1 -- 约定
    local isFree = id == 1 -- 约定
    
    if isHeroReward then
        self.icon:setVisible(false)
        self.heroNode:setVisible(true)

        local heroEntity = DataManager.HeroData:getHeroById(reward.id)
        if heroEntity then
            self.heroCell:refresh(heroEntity)
        else
            self.heroCell:refreshWithCfgId(reward.id)
        end
        self.heroNumText:setText("X" .. tostring(reward.num))
        self.nameText:setText(ModuleManager.HeroManager:getHeroName(reward.id, false))
    else
        self.icon:setVisible(true)
        self.heroNode:setVisible(false)

        self.icon:setSprite(GConst.ATLAS_PATH.SHOP, AD_REWARD_ICON[reward.id])
        self.nameText:setText(reward.num)
    end
    if isFree then -- (1)广告商品
        self.adNode:setVisible(true)
        self.costNode:setVisible(false)

        GFunc.setAdsSprite(self.adImg)
        local adLeftCount = DataManager.ShopData:getMallDailyFirstItemAdMaxCount() - bought
        if adLeftCount > 0 then
            self.adText:setText(I18N:getGlobalText(I18N.GlobalConst.STR_FREE) .. "(" .. tostring(adLeftCount) .. ")") -- 免费(x)
            GFunc.centerImgAndTx(self.adImg, self.adText, 5)
            self.sellOutText:setVisible(false)

            self:getBaseObject():addRedPoint(100, 145, 0.6)
        else
            self.adNode:setVisible(false)
            self.sellOutText:setVisible(true)

            self:getBaseObject():removeRedPoint()
        end
    else -- (2,3)金币(4,5,6)钻石商品
        self.adNode:setVisible(false)
        self.costNode:setVisible(true)

        local leftCount = DataManager.ShopData:getMallDailyGoodsLimitCount() - bought
        if leftCount > 0 then
            self.costImg:setSprite(GFunc.getIconRes(cost.id))
            self.costText:setText(cost.num)
            GFunc.centerImgAndTx(self.costImg, self.costText, 5)
            self.sellOutText:setVisible(false)
        else
            self.costNode:setVisible(false)
            self.sellOutText:setVisible(true)
        end
    end

    self:addClickListener(function()
        self:onClickGift(id, isFree)
    end)
end

function HotCell:onClickGift(id, isAd)
    local bought = self.data.bought
    if isAd then
        local adLeftCount = DataManager.ShopData:getMallDailyFirstItemAdMaxCount() - bought
        if adLeftCount > 0 then
            SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.MALL_DAILY, function ()
                ModuleManager.ShopManager:buyMallDailyGift(id, isAd)
            end)
        end
	else
        local leftCount = DataManager.ShopData:getMallDailyGoodsLimitCount() - bought
        if leftCount > 0 then
		    ModuleManager.ShopManager:buyMallDailyGift(id, isAd)
        end
	end
end

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

return HotCell