local PlayerLevelUpUI = class("PlayerLevelUpUI", BaseUI)

function PlayerLevelUpUI:isFullScreen()
    return false
end

function PlayerLevelUpUI:getPrefabPath()
    return "assets/prefabs/ui/player/player_level_up_ui.prefab"
end

function PlayerLevelUpUI:ctor(params)
    params = params or GConst.EMPTY_TABLE

    self.fromLevel = params.fromLevel
    self.rewards = params.rewards or GConst.EMPTY_TABLE
end

function PlayerLevelUpUI:onLoadRootComplete()
    self:_display()
end

function PlayerLevelUpUI:_display()
    local uiMap = self.root:genAllChildren()
    self.uiMap = uiMap

    uiMap["player_level_up_ui.bg"]:addClickListener(function()
        self:closeUI()
    end)

    local spineTitle = uiMap["player_level_up_ui.spine_title"]
    spineTitle:playAnimComplete("idle1", false, false, function()
        spineTitle:playAnim("idle2", true, false)
    end)

    local toLevel = DataManager.PlayerData:getLv()
    uiMap["player_level_up_ui.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.PLAYER_LEVEL_UP_DESC, toLevel))
    uiMap["player_level_up_ui.reward_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.LEVEL_UP_REWARD))
    uiMap["player_level_up_ui.continue"]:setText(I18N:getGlobalText(I18N.GlobalConst.CLICK_TO_CONTINUE))

    uiMap["player_level_up_ui.lv_img_l.text"]:setText(tostring(self.fromLevel))
    uiMap["player_level_up_ui.lv_img_r.text"]:setText(tostring(toLevel))
    local width = uiMap["player_level_up_ui.reward_title"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth
    uiMap["player_level_up_ui.r"]:setAnchoredPositionX(width/2 + 40)
    uiMap["player_level_up_ui.l"]:setAnchoredPositionX(-width/2 - 40)

    self:refreshRewards()
    AudioManager:playEffect(AudioManager.EFFECT_ID.PLAYER_UP)
end

function PlayerLevelUpUI:refreshRewards()
    if self.scrollRectComp then
        self.scrollRectComp:updateAllCell()
        return
    end

    local uiMap = self.root:genAllChildren()
    local scrollRect = uiMap["player_level_up_ui.scroll_rect"]
    self.scrollRectComp = scrollRect:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRectComp:addInitCallback(function()
		return GConst.TYPEOF_LUA_CLASS.REWARD_CELL
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.rewards[index])
	end)
    self.scrollRectComp:setFadeArgs(0.05, 0.3)
    self.scrollRectComp:clearCells()
    local rewardCount = #self.rewards
    if rewardCount > 10 then
        local comp = scrollRect:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SCROLL_RECT)
		comp.movementType = CS.UnityEngine.UI.ScrollRect.MovementType.Elastic
        self.scrollRectComp:setPerLineNum(5)
        scrollRect:setSizeDeltaX(560)
	else
        local comp = scrollRect:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_SCROLL_RECT)
        comp.movementType = CS.UnityEngine.UI.ScrollRect.MovementType.Clamped
        if rewardCount >= 5 then
            self.scrollRectComp:setPerLineNum(5)
            scrollRect:setSizeDeltaX(560)
        elseif rewardCount <= 0 then
            self.scrollRectComp:setPerLineNum(1)
            scrollRect:setSizeDeltaX(560)
        else
            self.scrollRectComp:setPerLineNum(rewardCount)
            scrollRect:setSizeDeltaX(112*rewardCount)
        end
	end
    self.scrollRectComp:refillCells(rewardCount, true)
end

return PlayerLevelUpUI