local RunesLevelUI = class("RunesLevelUI", BaseUI)

local ARROW_LIGHT = "common_arrow_2"
local ARROW_GRAY = "common_arrow_3"

function RunesLevelUI:isFullScreen()
    return false
end

function RunesLevelUI:getPrefabPath()
    return "assets/prefabs/ui/runes/runes_level_ui.prefab"
end

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

function RunesLevelUI:ctor()
    self.curLevel = DataManager.RunesData:getLevel()
end

function RunesLevelUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.txTitle = uiMap["runes_level_ui.bg.title.tx_title"]
    self.btnClose = uiMap["runes_level_ui.bg.close_btn"]
    self.btnRight = uiMap["runes_level_ui.bg.select.btn_right"]
    self.btnLeft = uiMap["runes_level_ui.bg.select.btn_left"]
    self.txSelect = uiMap["runes_level_ui.bg.select.tx_select"]
    self.txDesc = uiMap["runes_level_ui.bg.bottom.tx_desc"]

    self.quality = {}
    for i = 1, 8 do
        self.quality[i] = uiMap["runes_level_ui.bg.list.qlt_cell_" .. i]
    end

    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_7))
    self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_8))

    self.btnRight:addClickListener(function()
        self.curLevel = self.curLevel + 1
        self:onRefresh()
    end)
    self.btnLeft:addClickListener(function()
        self.curLevel = self.curLevel - 1
        self:onRefresh()
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
end

function RunesLevelUI:onRefresh()
    self.txSelect:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, self.curLevel))
    if self.curLevel >= DataManager.RunesData:getMaxLevel() then
        self.btnRight:setTouchEnable(false)
        self.btnRight:setSprite(GConst.ATLAS_PATH.COMMON, ARROW_GRAY)
    else
        self.btnRight:setTouchEnable(true)
        self.btnRight:setSprite(GConst.ATLAS_PATH.COMMON, ARROW_LIGHT)
    end
    if self.curLevel <= 1 then
        self.btnLeft:setTouchEnable(false)
        self.btnLeft:setSprite(GConst.ATLAS_PATH.COMMON, ARROW_GRAY)
    else
        self.btnLeft:setTouchEnable(true)
        self.btnLeft:setSprite(GConst.ATLAS_PATH.COMMON, ARROW_LIGHT)
    end

    for index, obj in ipairs(self.quality) do
        local map = obj:genAllChildren()
        local icon = map["img_qlt"]
        local rate = map["tx_rate"]
        icon:setSprite(GConst.ATLAS_PATH.HERO, GConst.RunesConst.QUALITY_ICON[index])
        rate:setText((DataManager.RunesData:getQualityRate(self.curLevel, index) or 0) .. "%")
	end
end

return RunesLevelUI