local HeroListCell = class("HeroListCell", BaseCell)

local H = {
    NORMAL = 220,
    HAS_TITLE = 340
}

function HeroListCell:init()
	self.uiMap = self:getUIMap()
	self.heroCells = {
		self.uiMap["hero_list_cell.prop_node.hero_cell_1"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
		self.uiMap["hero_list_cell.prop_node.hero_cell_2"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
		self.uiMap["hero_list_cell.prop_node.hero_cell_3"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
		self.uiMap["hero_list_cell.prop_node.hero_cell_4"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
	}
	self.title = self.uiMap["hero_list_cell.title"]
	self.titleTx = self.uiMap["hero_list_cell.title.title_desc"]
end

function HeroListCell:refresh(index, heroList, stageFormation, allHeroCount, activeCount, func)
    self.baseObject:addClickListener(function()
        if func then
            func()
        end
    end)
    if activeCount > 0 and index == 1  then
        self.title:setVisible(true)
        self.title:setSprite(GConst.ATLAS_PATH.COMMON, "common_board_105")
        self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_6))
        self:getBaseObject():setSizeDeltaY(H.HAS_TITLE)
    elseif index == math.ceil(activeCount / 4) + 1 then
        self.title:setVisible(true)
        self.title:setSprite(GConst.ATLAS_PATH.COMMON, "common_board_116")
        self.titleTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_7))
        self:getBaseObject():setSizeDeltaY(H.HAS_TITLE)
    else
        self.title:setVisible(false)
        self:getBaseObject():setSizeDeltaY(H.NORMAL)
    end
    local heroStartIndex = (index-1)*4 + 1
    if heroStartIndex > activeCount then
        local count = (4 - activeCount%4)
        if count == 4 then
            count = 0
        end
        heroStartIndex = heroStartIndex - count
    end
    local heroIndex = heroStartIndex
    for i = 1, 4 do
        if heroIndex <= activeCount then
            local heroId = heroList[heroIndex].cfgId
            local heroEntity = DataManager.HeroData:getHeroById(heroId)
            local matchType = heroEntity:getMatchType()
            self.heroCells[i]:setVisible(true)
            self.heroCells[i]:refresh(heroEntity, not heroEntity:isActived())
            self.heroCells[i]:showCheck(stageFormation[matchType] == heroId)
            self.heroCells[i]:addClickListener(function()
                AudioManager:playEffect(AudioManager.CLICK_ID[GConst.CLICK_SOUND.NORMAL])
                if func then
                    func(self.heroCells[i], heroId)
                end
            end)
        elseif heroIndex <= allHeroCount then
            if heroStartIndex <= activeCount then
                self.heroCells[i]:setVisible(false)
            else
                local heroId = heroList[heroIndex].cfgId
                local heroEntity = DataManager.HeroData:getHeroById(heroId)
                self.heroCells[i]:setVisible(true)
                self.heroCells[i]:refresh(heroEntity, true)
                self.heroCells[i]:addClickListener(function()
                    AudioManager:playEffect(AudioManager.CLICK_ID[GConst.CLICK_SOUND.NORMAL])
                    if func then
                        func(self.heroCells[i], heroId)
                    end
                end)
            end
        else
            self.heroCells[i]:setVisible(false)
        end
        heroIndex = heroIndex + 1
    end
end

function HeroListCell:getHeroCellByHeroId(heroId)
    if not self.heroCells then
        return
    end
    for i, cell in ipairs(self.heroCells) do
        if cell:getHeroId() == heroId then
            return cell
        end
    end
end

return HeroListCell