local PopRewardCell = class("PopRewardCell", BaseCell)

function PopRewardCell:init()
	local uiMap = self.baseObject:genAllChildren()
    self.bg = uiMap["pop_reward_cell.bg"]
    self.choose = uiMap["pop_reward_cell.choose"]
    self.imgChoose = uiMap["pop_reward_cell.img_choose"]
    if not self.itemCell then
	    self.itemCell = CellManager:addCellComp(uiMap["pop_reward_cell.item_cell"], GConst.TYPEOF_LUA_CLASS.ITEM_CELL)
    end
    if not self.popHeroCell then
        self.popHeroCell = CellManager:addCellComp(uiMap["pop_reward_cell.pop_hero_cell"], GConst.TYPEOF_LUA_CLASS.POP_HERO_CELL)
	end
    self.descTx = uiMap["pop_reward_cell.desc_text"]
    self:addClickListener(function()
        if self.clickCallback then
            self.clickCallback()
        end
    end)
end

-- 显示道具
function PopRewardCell:refresh(id, num, showBg)
    self.choose:setActive(false)
    self.imgChoose:setActive(false)
    self:setClickListener(nil)
	local cfgInfo = ConfigManager:getConfig("item")[id]
    if cfgInfo then
        self.bg:setVisible(showBg)
        if cfgInfo.type == GConst.ItemConst.ITEM_TYPE.HERO_FRAGMENT then -- 英雄展示
            self.itemCell:getBaseObject():setActive(false)
            self.popHeroCell:getBaseObject():setActive(true)

            local heroEntity = DataManager.HeroData:getHeroById(id)
            if heroEntity then
                self.popHeroCell:refresh(heroEntity, num)
            else
                self.popHeroCell:refreshWithCfgId(id, num)
            end
            self.popHeroCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(id, GConst.REWARD_TYPE.ITEM, self.popHeroCell:getBaseObject())
			end)
            self.descTx:setText(ModuleManager.HeroManager:getHeroName(id))
        else -- 通用道具
            self.itemCell:getBaseObject():setActive(true)
            self.popHeroCell:getBaseObject():setActive(false)

            self.itemCell:refreshByCfg(id, 0)
            self.itemCell:setNum("")
            self.itemCell:addClickListener(function()
				ModuleManager.TipsManager:showRewardTips(id, GConst.REWARD_TYPE.ITEM, self.itemCell:getBaseObject())
			end)
            self.descTx:setText(num)
        end
    end
end

-- 显示可选
function PopRewardCell:refreshChoose(clickFunc)
    self.imgChoose:setActive(true)
    self:setClickListener(clickFunc)
end

-- 只显示可选
function PopRewardCell:refreshOnlyChoose(clickFunc)
    self.choose:setActive(true)
    self.imgChoose:setActive(false)
    self.itemCell:getBaseObject():setActive(false)
    self.popHeroCell:getBaseObject():setActive(false)
	self.descTx:setText(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_12))
    self:setClickListener(clickFunc)
end

function PopRewardCell:setClickListener(callback)
	self.clickCallback = callback
end

function PopRewardCell:setVisible(visible, scale)
	self.baseObject:setVisible(visible, scale)
end

return PopRewardCell
