local HeroFragmentTips = class("HeroFragmentTips", BaseUI)

function HeroFragmentTips:ctor(params)
	self.itemId = params.itemId
end

function HeroFragmentTips:onPressBackspace()
	self:closeUI()
end

function HeroFragmentTips:getPrefabPath()
	return "assets/prefabs/ui/tips/hero_fragment_tips.prefab"
end

function HeroFragmentTips:isFullScreen()
    return false
end

function HeroFragmentTips:onLoadRootComplete()
    local cfg = ConfigManager:getConfig("item")[self.itemId]
    if cfg == nil then
        return self:closeUI()
    end
    local drop = nil
    if cfg.hero_drop then
        drop = cfg.hero_drop
    elseif cfg.box_drop then
        drop = {}
        for idx, item in ipairs(cfg.box_drop) do
            local temp = ConfigManager:getConfig("item")[item.id]
            if temp and temp.hero_drop then
                table.addArray(drop, temp.hero_drop)
            end
        end
    end
    if drop == nil then
        return self:closeUI()
    end
    self.heroList = {}
    for _, v in ipairs(drop) do
        if DataManager.HeroData:getHeroIsUnlock(v.id) then
            table.insert(self.heroList, v.id)
        end
    end
    local heroCfg = ConfigManager:getConfig("hero")
    local heroA
    local heroB
    table.sort(self.heroList, function(a, b)
        heroA = heroCfg[a]
        heroB = heroCfg[b]
        if heroA.qlt == heroB.qlt then
            return a > b
        else
            return heroA.qlt > heroB.qlt
        end
    end)
    local uiMap = self.root:genAllChildren()
    self.uiMap = uiMap
    local itemQlt = cfg.qlt
    if itemQlt < 1 then
        itemQlt = 1
    elseif itemQlt > 4 then
        itemQlt = 4
    end
    local bgQlt = uiMap["hero_fragment_tips.title_bg_img.bg_qlt"]
    bgQlt:setVisible(false)
    bgQlt:setTexture("assets/arts/textures/background/shop/shop_card_bg_" .. itemQlt .. ".png", function()
        bgQlt:setVisible(true)
    end)
    uiMap["hero_fragment_tips.title_bg_img.card"]:setSprite(GConst.ATLAS_PATH.ICON_ITEM, cfg.icon)
    uiMap["hero_fragment_tips.title_bg_img.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_CARD_TIPS_DESC))
    uiMap["hero_fragment_tips.title_bg_img.title_text"]:setText(I18N:getText("item", self.itemId, "name"))
    uiMap["hero_fragment_tips.title_bg_img.close_btn"]:addClickListener(function()
        self:closeUI()
    end)

    if cfg.type == GConst.ItemConst.ITEM_TYPE.EPIC_HERO_FRAMENT then
        uiMap["hero_fragment_tips.title_bg_img.desc"]:setText(I18N:getText("item", self.itemId, "desc"))
    end

    self:initScrollRect()
end

function HeroFragmentTips:initScrollRect()
    local scrollRectComp = self.uiMap["hero_fragment_tips.title_bg_img.scroll_rect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    scrollRectComp:addInitCallback(function()
		return GConst.TYPEOF_LUA_CLASS.REWARD_CELL
	end)
	scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refreshItemById(self.heroList[index], 0)
	end)
    scrollRectComp:clearCells()
    scrollRectComp:refillCells(#self.heroList)
end

return HeroFragmentTips