local BoxTips = class("BoxTips", BaseUI)

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

function BoxTips:getPrefabPath()
	return "assets/prefabs/ui/tips/box_tips.prefab"
end

function BoxTips:isFullScreen()
    return false
end

function BoxTips:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.uiMap = uiMap
    local itemTextInfo = I18N:getConfig("item")[self.itemId]
    if itemTextInfo then
        uiMap["box_tips.bg.title"]:setText(itemTextInfo.name)
    else
        uiMap["box_tips.bg.title"]:setText(GConst.EMPTY_STRING)
    end
    local itemInfo = ConfigManager:getConfig("item")[self.itemId]
    uiMap["box_tips.bg.box"]:setSprite(ModuleManager.ItemManager:getItemIcon(self.itemId))
    uiMap["box_tips.close_btn"]:addClickListener(function()
        self:closeUI()
    end)

    self.rewardNodeList = {}
    local itemCfg = ConfigManager:getConfig("item")
    local rewardList = itemInfo.reward or GConst.EMPTY_TABLE
    for i = 1, 4 do
        local rewardNode = uiMap["box_tips.bg.item_" .. i]
        self.rewardNodeList[i] = rewardNode
        if rewardList[i] then
            local num =  uiMap["box_tips.bg.item_" .. i .. ".num"]
            num:setText(GFunc.intToString(GFunc.getRewardNum(rewardList[i])))
            local help =  uiMap["box_tips.bg.item_" .. i .. ".help"]
            local itemInfo = itemCfg[rewardList[i].id]
            if itemInfo and itemInfo.type == GConst.ItemConst.ITEM_TYPE.RANDOM_FRAGMENT then
                help:setVisible(true)
            else
                help:setVisible(false)
            end
            rewardNode:setVisible(true)
            rewardNode:addClickListener(function()
                ModuleManager.TipsManager:showRewardTips(rewardList[i].id, rewardList[i].type, rewardNode)
            end)
            local rewardComp = uiMap["box_tips.bg.item_" .. i .. ".reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
            rewardComp:refreshByConfig(rewardList[i])
            rewardComp:setTouchEnable(false)
            rewardComp:setNumTx(GConst.EMPTY_STRING)
        else
            rewardNode:setVisible(false)
        end
    end
    self:adpatRewardNode(#rewardList)
end

function BoxTips:adpatRewardNode(count)
    if count == 0 then
        return
    end
    local width = self.rewardNodeList[1]:getRectWidth() + 10
    if count % 2 == 0 then
        for i = 1, count do
            self.rewardNodeList[i]:setAnchoredPositionX((i - count / 2 - 0.5) * width)
        end
    else
        for i = 1, count do
            self.rewardNodeList[i]:setAnchoredPositionX((i - (count - 1) / 2 - 1) * width)
        end
    end
end

return BoxTips