local PopHeroCell = class("PopHeroCell", BaseCell)

function PopHeroCell:init()
	local uiMap = self.baseObject:genAllChildren()
    self.bg = uiMap["pop_hero_cell.bg"]
	self.icon = uiMap["pop_hero_cell.icon"]
	self.matchImg = uiMap["pop_hero_cell.match_img"]
	self.progressBg = uiMap["pop_hero_cell.progress_bg"]
	self.progress = uiMap["pop_hero_cell.progress_bg.progress"]
	self.progressTx = uiMap["pop_hero_cell.progress_bg.progress_tx"]
	self.lvUpArrow = uiMap["pop_hero_cell.effect_node.ui_spine_obj"]
	self.fragmenImg = uiMap["pop_hero_cell.progress_bg.fragment_img"]
	self.sImg = uiMap["pop_hero_cell.s"]
    self.newBg = uiMap["pop_hero_cell.new_bg"]
    self.newTx = uiMap["pop_hero_cell.new_bg.text"]
    self.numTx = uiMap["pop_hero_cell.num_tx"]
	
	self.baseObject:addClickListener(function()
		if self.clickCallback then
			self.clickCallback()
		end
	end)
end

function PopHeroCell:refresh(heroEntity, num)
	local heroInfo = heroEntity:getConfig()
	self:_refresh(heroInfo)

	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
		self.progressBg:setVisible(not heroEntity:isMaxLv())
	else
		if canLvUp then
			self.progressBg:setVisible(true)
		else
			self.progressBg:setVisible(false)
		end
	end
	if heroEntity:isActived() or fragmentCount > 0 then
		self.newBg:setVisible(false)
	else
		self.newBg:setVisible(true)
        self.newTx:setText("New")
	end
    self.numTx:setText("X" .. tostring(num))
end

function PopHeroCell:refreshWithCfgId(id, num)
	local heroInfo = ConfigManager:getConfig("hero")[id]
	self:_refresh(heroInfo)
	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)
    if fragmentCount > 0 then -- 已拥有,则不是NEW了
        local needFragmentCount = materials[1] or 1
        self.progressTx:setText(fragmentCount .. "/" .. needFragmentCount)
        self.progress:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = fragmentCount / needFragmentCount
        self.progressBg:setVisible(true)
    else
        self.progressBg:setVisible(false)
    end
	self.lvUpArrow:setVisible(false)
	self.fragmenImg:setVisible(true)

	if fragmentCount > 0 then
		self.newBg:setVisible(false)
	else
		self.newBg:setVisible(true)
        self.newTx:setText("New")
	end
    self.numTx:setText("X" .. tostring(num))
end

function PopHeroCell:_refresh(heroInfo)
    self.bg:setSprite(GConst.ATLAS_PATH.ICON_ITEM, GConst.FRAME_QLT[heroInfo.qlt])
	self.icon:setSprite(GConst.ATLAS_PATH.ICON_HERO, tostring(heroInfo.icon))
	self.matchImg:setSprite(GConst.ATLAS_PATH.ICON_HERO, GConst.HeroConst.MATCH_ICON_NAME[heroInfo.position])
	self.sImg:setVisible(heroInfo.qlt >= 4)
end

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

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

return PopHeroCell
