local LargeHeroCell = class("LargeHeroCell", BaseCell)

local OUT_SCREEN_X = 10000

function LargeHeroCell:init()
	local uiMap = self.baseObject:genAllChildren()
	self.icon = uiMap["large_hero_cell.hero_bg.icon"]
	self.heroBg = uiMap["large_hero_cell.hero_bg"]
	self.heroDec = uiMap["large_hero_cell.hero_bg.dec"]
	self.check = uiMap["large_hero_cell.hero_bg.mask"]
	self.matchImg = uiMap["large_hero_cell.hero_bg.match_img"]
    self.infoBtnDesc = uiMap["large_hero_cell.hero_bg.info_btn.desc"]
    self.useBtnDesc = uiMap["large_hero_cell.hero_bg.use_btn.desc"]
	self.sImg = uiMap["large_hero_cell.hero_bg.s"]
	self.isGray = false

    self.useBtnDesc:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_8))
    uiMap["large_hero_cell.hero_bg.info_btn"]:addClickListener(function()
        if not self.heroId or not self.matchType then
            return
        end
		self.baseObject:setAnchoredPositionX(OUT_SCREEN_X)
        ModuleManager.HeroManager:showHeroDetailUI(self.heroId)
	end)

    uiMap["large_hero_cell.hero_bg.use_btn"]:addClickListener(function()
        if not self.heroId or not self.matchType then
            return
        end
		self.baseObject:setAnchoredPositionX(OUT_SCREEN_X)
		self.onClickUseFunc(self.heroId, self.matchType)
	end)
end

function LargeHeroCell:refresh(heroEntity, isGray, useFunc)
    self.heroId = heroEntity:getCfgId()
    self.matchType = heroEntity:getMatchType()
	local heroInfo = heroEntity:getConfig()
	self:_refresh(heroInfo, isGray)
	self.icon:setSprite(GConst.ATLAS_PATH.ICON_HERO_2, DataManager.SkinData:getIcon(heroEntity:getSkinId()))
    local str
    if heroEntity:canLvUp() then
        str = I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4)
    else
        str = I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_9)
    end
    self.infoBtnDesc:setText(str)
	self.onClickUseFunc = useFunc
end

function LargeHeroCell:refreshWithCfgId(id, isGray)
	local heroInfo = ConfigManager:getConfig("hero")[id]
    self.heroId = id
    self.matchType = heroInfo.position
	self:_refresh(heroInfo, isGray)
    self.infoBtnDesc:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_9))
end

function LargeHeroCell:_refresh(heroInfo, isGray)
	if isGray then
		self.heroBg:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.HERO_FRAME_GRAY_QLT[heroInfo.qlt])
	else
		self.heroBg:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.FRAME_QLT[heroInfo.qlt])
	end
	self.heroDec:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.HERO_DEC_QLT[heroInfo.qlt])
	self.matchImg:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.HeroConst.MATCH_ICON_NAME[heroInfo.position])
	self.check:setVisible(false)
	self.sImg:setVisible(heroInfo.qlt >= 4)
	self:setGray(isGray)
end

function LargeHeroCell:showCheck(visible)
	self.check:setVisible(visible)
end

function LargeHeroCell:addClickListener(func)
    self.baseObject:addClickListener(func)
end

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

function LargeHeroCell:setGray(isGray)
	if self.isGray == isGray then
		return
	end
	self.icon:setImageGray(isGray)
	self.matchImg:setImageGray(isGray)
end

return LargeHeroCell
