local HeroCell = class("HeroCell", BaseCell)

function HeroCell:init()
	local uiMap = self.baseObject:genAllChildren()
	-- 通用
	self.icon = uiMap["hero_cell.hero_bg.icon"]
	self.heroBg = uiMap["hero_cell.hero_bg"]
	self.heroDec = uiMap["hero_cell.hero_bg.dec"]
	self.check = uiMap["hero_cell.hero_bg.mask"]
	self.matchImg = uiMap["hero_cell.hero_bg.match_img"]
	self.lvTx = uiMap["hero_cell.hero_bg.lv_tx"]
	self.sImg = uiMap["hero_cell.hero_bg.s"]
	-- 个人节点
	self.selfNode = uiMap["hero_cell.hero_bg.self_node"]
	self.progressBg = uiMap["hero_cell.hero_bg.self_node.progress_bg"]
	self.progress = uiMap["hero_cell.hero_bg.self_node.progress_bg.progress"]
	self.progressTx = uiMap["hero_cell.hero_bg.self_node.progress_bg.progress_tx"]
	self.unlockTx = uiMap["hero_cell.hero_bg.self_node.unlock_tx"]
	self.fragmenImg = uiMap["hero_cell.hero_bg.self_node.progress_bg.fragment_img"]
	self.lvUpArrow = uiMap["hero_cell.hero_bg.self_node.effect_node.ui_spine_obj"]
	-- 他人节点
	self.otherNode = uiMap["hero_cell.hero_bg.other_node"]
	self.otherTxName = uiMap["hero_cell.hero_bg.other_node.tx_name"]

	self.isGray = false
	self.baseObject:addClickListener(function()
		if self.clickCallback then
			self.clickCallback()
		end
	end)
	self:bind(DataManager.BagData.ItemData, "dirty", function()
        self:refreshRedPoint()
    end)
	self:bind(DataManager.SkinData, "isDirty", function()
        self:refreshHeroIcon()
    end)
end

function HeroCell:refresh(heroEntity, isGray)
	self.heroEntity = heroEntity
	self:bind(self.heroEntity, "isDirty", function()
        self:refreshRedPoint()
    end)

	self.selfNode:setVisible(true)
	self.otherNode:setVisible(false)

	local heroInfo = heroEntity:getConfig()
	self:_refresh(heroInfo, isGray)

	local canLvUp = heroEntity:canLvUp()
	self.lvUpArrow:setVisible(canLvUp)
	if canLvUp then
		self.lvUpArrow:playAnim("animation", true, false)
	end
	self.fragmenImg:setVisible(not canLvUp)
	local materials = heroEntity:getLvUpMaterials() or {}
    local fragmentCount = DataManager.BagData.ItemData:getItemNumById(heroEntity:getFragmentId())
    local needFragmentCount = materials[1] or 1
	self.progressTx:setText(fragmentCount .. "/" .. needFragmentCount)
	if fragmentCount >= needFragmentCount then
		self.progress:setSprite(GConst.ATLAS_PATH.COMMON, "common_progress_1", nil, self.progress:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER))
	else
		self.progress:setSprite(GConst.ATLAS_PATH.COMMON, "common_progress_2", nil, self.progress:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER))
	end
	self.progress:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = fragmentCount / needFragmentCount
	if heroEntity:isUnlock() then
		if heroEntity:isActived() then
			self.lvTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, heroEntity:getLv()))
			self.lvTx:setVisible(true)
		else
			self.lvTx:setVisible(false)
		end
		self.progressBg:setVisible(not heroEntity:isMaxLv())
		self.unlockTx:setVisible(false)
	else
		self.lvTx:setVisible(false)
		if canLvUp then
			self.unlockTx:setVisible(false)
			self.progressBg:setVisible(true)
		else
			self.unlockTx:setVisible(true)
			self.progressBg:setVisible(false)
			-- local unlcokChapter = heroEntity:getUnlcokChapter()
			-- local unlockDan = heroEntity:getUnlockDan()
			-- if unlcokChapter then
			-- 	self.unlockTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_10, unlcokChapter))
			-- elseif unlockDan then
			-- 	self.unlockTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_12, I18N:getGlobalText(I18N.GlobalConst["ARENA_RANKNAME_" .. unlockDan])))
			-- else
			-- 	self.unlockTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_11))
			-- end
		end
	end
	self:refreshRedPoint()
end

-- 刷新英雄icon
function HeroCell:refreshHeroIcon(iconName)
	if self.heroEntity ~= nil then
		self.icon:setSprite(GConst.ATLAS_PATH.ICON_HERO_2, DataManager.SkinData:getIcon(self.heroEntity:getSkinId()))
	elseif iconName then
		self.icon:setSprite(GConst.ATLAS_PATH.ICON_HERO_2, tostring(iconName))
	end
end

-- 刷新红点
function HeroCell:refreshRedPoint()
	if self.heroEntity == nil then
		return
	end

	if not self.selfNode or self.selfNode:isDestroyed() or CS.BF.Utils.IsNull(self.selfNode:getGameObject()) then
		return
	end

	if not self.notShowRedPoint and
	DataManager.FormationData:heroInFormation(GConst.BattleConst.FORMATION_TYPE.STAGE, self.heroEntity:getCfgId()) and
	DataManager.EquipData:canUpgradeEquip(self.heroEntity:getCfgId()) then
        self.selfNode:addRedPoint(55, -35, 0.64)
    else
        self.selfNode:removeRedPoint()
    end
end

-- 设置是否展示红点
function HeroCell:setShowRedPoint(show)
	self.notShowRedPoint = not show
end

function HeroCell:getHeroId()
	if not self.heroEntity then
		return
	end
	return self.heroEntity:getCfgId()
end

function HeroCell:refreshBriefInfo(heroEntity)
	self.heroEntity = heroEntity
	local id = self.heroEntity:getCfgId()
	local level = self.heroEntity:getLv()
	self.selfNode:setVisible(false)
	self.otherNode:setVisible(true)

	self:_refresh(ConfigManager:getConfig("hero")[id])

	self.otherTxName:setText(ModuleManager.HeroManager:getHeroName(id))
	self.lvTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, level))
end

function HeroCell:refreshWithCfgId(id, isGray)
	local heroInfo = ConfigManager:getConfig("hero")[id]
	self:_refresh(heroInfo, isGray)

	local lv = heroInfo.begin_lv
	local lvInfo = ConfigManager:getConfig("hero_level")[lv]
	local materials = lvInfo["cost_" .. heroInfo.qlt]
    local fragmentCount = DataManager.BagData.ItemData:getItemNumById(heroInfo.item_id)
    local needFragmentCount = materials[1] or 1
	self.progressTx:setText(fragmentCount .. "/" .. needFragmentCount)
	if fragmentCount >= needFragmentCount then
		self.progress:setSprite(GConst.ATLAS_PATH.COMMON, "common_progress_1", nil, self.progress:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER))
	else
		self.progress:setSprite(GConst.ATLAS_PATH.COMMON, "common_progress_2", nil, self.progress:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER))
	end
	self.progress:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = fragmentCount / needFragmentCount
	self.lvTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, lv))
	self.lvTx:setVisible(true)
	self.progressBg:setVisible(true)
	self.unlockTx:setVisible(false)
	self.lvUpArrow:setVisible(false)
	self.fragmenImg:setVisible(true)
end

function HeroCell:_refresh(heroInfo, isGray)
	self.clickCallback = nil
	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:refreshHeroIcon(heroInfo.icon)
	self:setGray(isGray)
end

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

function HeroCell:addClickListener(callback)
	self.clickCallback = callback
end

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

function HeroCell:setSpineVisible(visible, scale)
	self.lvUpArrow:setVisible(visible, scale)
end

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

return HeroCell
