local SummonShopCell = class("SummonShopCell", BaseCell)

function SummonShopCell:init()
	local uiMap = self.baseObject:genAllChildren()

	self.heroBg = uiMap["summon_shop_cell.bg.hero_bg"]
	self.icon = uiMap["summon_shop_cell.bg.hero_bg.icon"]
	self.heroDec = uiMap["summon_shop_cell.bg.hero_bg.dec"]
	self.matchImg = uiMap["summon_shop_cell.bg.hero_bg.match_img"]
	self.nameTx = uiMap["summon_shop_cell.bg.hero_bg.name_tx"]
	self.costTx = uiMap["summon_shop_cell.bg.cost_bg.cost_tx"]
	self.costIcon = uiMap["summon_shop_cell.bg.cost_bg.icon"]
	self.maskImg = uiMap["summon_shop_cell.bg.mask_img"]
	self.unlockTx = uiMap["summon_shop_cell.bg.mask_img.unlock_tx"]
	self.lockImg = uiMap["summon_shop_cell.bg.mask_img.lock_img"]
	self.infoBtn = uiMap["summon_shop_cell.bg.hero_bg.info_btn"]
	self.itemBg = uiMap["summon_shop_cell.bg.item_bg"]
	self.itemIcon = uiMap["summon_shop_cell.bg.item_bg.icon"]
	self.itemNumTx = uiMap["summon_shop_cell.bg.item_bg.num_tx"]
	self.disBg = uiMap["summon_shop_cell.bg.dis_bg"]
	self.disTx = uiMap["summon_shop_cell.bg.dis_bg.dis_tx"]

	self.unlockTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_7))
	-- local meshPro = self.unlockTx:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
	-- local width = meshPro.preferredWidth
	GFunc.centerImgAndTx(self.lockImg, self.unlockTx, 6)

	self:addClickListener(function()
        if self.clickCallback ~= nil then
            self.clickCallback(self.heroId)
        end
    end)
	self.infoBtn:addClickListener(function()
		if self.heroId ~= nil then
			ModuleManager.HeroManager:showHeroDetailUI(self.heroId, true)
		else
			 ModuleManager.TipsManager:showRewardTips(self.id, self.type)
		end
	end)
end

function SummonShopCell:refresh(cfg)
	self.heroId = nil
	self.cfg = cfg
	if self.cfg.force_id ~= nil then
		self:refreshHero()
	else
		self:refreshItem()
	end
	self:setCost()
end

function SummonShopCell:refreshHero()
	-- 英雄
	self.heroId = self.cfg.hero.id
	self.num = self.cfg.hero.num
	self.isOwn = DataManager.HeroData:getHeroIsUnlock(self.cfg.force_id)
	self.itemBg:setActive(false)
	self.heroBg:setActive(true)
	local heroInfo = ConfigManager:getConfig("hero")[self.heroId]
	self.heroBg:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.FRAME_QLT[heroInfo.qlt])
	self.heroDec:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.HERO_DEC_QLT[heroInfo.qlt])
	self.icon:setSprite(GConst.ATLAS_PATH.ICON_HERO_2, tostring(heroInfo.icon))
	self.nameTx:setText(ModuleManager.HeroManager:getHeroName(self.heroId))
	self.maskImg:setActive(not self.isOwn)
	self.matchImg:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.HeroConst.MATCH_ICON_NAME[heroInfo.position])
end

function SummonShopCell:refreshItem()
	-- 道具
	self.id = self.cfg.hero.id
	self.itemBg:setActive(true)
	self.heroBg:setActive(false)
	local itemInfo = ConfigManager:getConfig("item")[self.id]
	self.itemBg:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.FRAME_QLT[itemInfo.qlt])
	self.itemIcon:setSprite(GConst.ATLAS_PATH.ICON_ITEM, tostring(itemInfo.icon))
	self.nameTx:setText(ModuleManager.ItemManager:getItemName(self.id))
	self.itemNumTx:setText(self.cfg.hero.num)
	self.maskImg:setActive(false)
end

function SummonShopCell:setCost()
	local cost
	local exchangesCount = DataManager.SummonData:getExchangesCount(self.cfg.id)    --购买次数
	if self.cfg.discount_limit == nil or exchangesCount >= self.cfg.discount_limit then
		cost = self.cfg.cost
		self.disBg:setActive(false)
	else
		cost = self.cfg.discount
		if self.cfg.discount_display ~= nil then
			self.disBg:setActive(self.isOwn)
			-- self.disTx:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESCR_8, self.cfg.discount_display // 1000))
			self.disTx:setText("-" .. self.cfg.discount_display // 1000 .. "%")
		else
			self.disBg:setActive(false)
		end
	end
	if cost ~= nil then
		local costId = GFunc.getRewardId(cost)
		local costCount = GFunc.getRewardNum(cost)

		local haveItemNum = DataManager.BagData.ItemData:getItemNumById(costId)     --拥有货币
		if haveItemNum >= costCount then
			self.costTx:setText(costCount)
		else
			self.costTx:setText("<color=#FF5D5D>"..costCount.."</color>")
		end
		self.costIcon:setSprite(GFunc.getIconRes(costId))
		GFunc.centerImgAndTx(self.costIcon, self.costTx, -5)
	end
end

function SummonShopCell:setShowType(showType, func, teamType)
    self.showType = showType
    self.clickCallback = func
end

return SummonShopCell
