local BoxItemTips = class("BoxItemTips", BaseUI)

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

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

function BoxItemTips:getPrefabPath()
	return "assets/prefabs/ui/tips/box_item_tips.prefab"
end

function BoxItemTips:isFullScreen()
    return false
end

function BoxItemTips:onLoadRootComplete()
    local cfg = ConfigManager:getConfig("item")[self.itemId]
    if cfg == nil then
        return self:closeUI()
    end
    local drop = nil
    local titleTx = I18N:getText("item", self.itemId, "desc")
    self.isFixed = false
    if cfg.box_drop then
        drop = cfg.box_drop
    else
        drop = cfg.reward
        self.isFixed = true
    end
    if drop == nil then
        return self:closeUI()
    end
    self.itemList = {}
    for _, v in ipairs(drop) do
        local itemCfg = ConfigManager:getConfig("item")[v.id]
        local unit = {
            reward = v,
            sort = itemCfg.qlt * 10000 + v.id
        }
        table.insert(self.itemList, unit)
    end
    table.sort(self.itemList, function(a, b)
        return a.sort > b.sort
    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["box_item_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["box_item_tips.title_bg_img.card"]:setSprite(GConst.ATLAS_PATH.ICON_ITEM, cfg.icon)
    uiMap["box_item_tips.title_bg_img.desc"]:setText(titleTx)
    uiMap["box_item_tips.title_bg_img.title_text"]:setText(I18N:getText("item", self.itemId, "name"))
    uiMap["box_item_tips.title_bg_img.close_btn"]:addClickListener(function()
        self:closeUI()
    end)

    self:initScrollRect()
end

function BoxItemTips:initScrollRect()
    local scrollRectComp = self.uiMap["box_item_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:refreshByConfig(self.itemList[index].reward)
        if not self.isFixed then
            cell:hideCountTx()
        end
	end)
    scrollRectComp:clearCells()
    scrollRectComp:refillCells(#self.itemList)
end

return BoxItemTips