local RenameUI = class("RenameUI", BaseUI)

function RenameUI:isFullScreen()
    return  false
end

function RenameUI:getPrefabPath()
    return "assets/prefabs/ui/player_info/rename_ui.prefab"
end

function RenameUI:ctor()
end

function RenameUI:onCover()
end

function RenameUI:onReshow()
end

function RenameUI:onClose()
end

function RenameUI:onLoadRootComplete()
    self.uiMap = self.root:genAllChildren()

    self.txTitle = self.uiMap["rename_ui.bg.title.tx_title"]
    self.txDesc = self.uiMap["rename_ui.bg.tx_desc"]
    self.input = self.uiMap["rename_ui.bg.input"]
    self.txTips = self.uiMap["rename_ui.bg.group.tx_tips"]
    self.btnOk = self.uiMap["rename_ui.bg.group.btn_ok"]
    self.iconCost = self.uiMap["rename_ui.bg.group.btn_ok.icon"]
    self.txConst = self.uiMap["rename_ui.bg.group.btn_ok.tx_const"]
    self.txFree = self.uiMap["rename_ui.bg.group.btn_ok.tx_free"]
    self.closeBtn = self.uiMap["rename_ui.bg.close_btn"]
    self.inputField = self.input:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TMP_INPUT_FIELD)

    self.btnOk:addClickListener(function ()
        if self.constId ~= nil and self.constNum ~= nil and not GFunc.checkCost(self.constId, self.constNum, true) then
            return
        end
		self:onRename()
	end)
    self.closeBtn:addClickListener(function ()
        UIManager:showUI("app/ui/player_info/player_info_ui")
		self:closeUI()
	end)
    self:addEventListener(EventManager.CUSTOM_EVENT.RENAME_SUCCESS, function()
        UIManager:showUI("app/ui/player_info/player_info_ui")
        self:closeUI()
    end)
end

function RenameUI:onRefresh()
    self.inputField.text = ""
    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.RENAME_DESC))
    self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ENTER_NAME_DESC))
    
    if DataManager.PlayerData:isFreeRename() then
        self.txTips:setActive(true)
        self.txFree:setActive(true)
        self.iconCost:setActive(false)
        self.txConst:setActive(false)
        self.txTips:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_THIS_TIME_DESC))
        self.txFree:setText(I18N:getGlobalText(I18N.GlobalConst.STR_FREE))
    else
        local const = DataManager.PlayerData:getRenameCost()
        self.constId = GFunc.getRewardId(const)
        self.constType = GFunc.getRewardType(const)
        self.constNum = GFunc.getRewardNum(const)

        self.txTips:setActive(false)
        self.txFree:setActive(false)
        self.iconCost:setActive(true)
        self.txConst:setActive(true)
        self.iconCost:setSprite(GFunc.getRewardIconRes(self.constType, self.constId))

        if not GFunc.checkCost(self.constId, self.constNum) then
            -- 不满足
            self.btnOk:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_1")
            self.txConst:setText("<color=#ff3434>x" .. self.constNum .. "</color>")
        else
            self.btnOk:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_1")
            self.txConst:setText("x" .. self.constNum)
        end
        GFunc.centerImgAndTx(self.iconCost, self.txConst)
    end
end

function RenameUI:onRename()
    local input = self.inputField.text

    if not input or input == "" or #input == 0 then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.CANNOT_RENAME_DESC_1))
        return
    end
    if GFunc.getTextLen2(input) > GConst.NAME_MAX_LENTH then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.CANNOT_RENAME_DESC_2))
        return
    end
    if CS.XLua.Utils.IsSpecialChar(input) then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.CANNOT_RENAME_DESC_3))
        return
    end
    if input == DataManager.PlayerData:getNickname() then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.CANNOT_RENAME_DESC_5))
        return
    end

    ModuleManager.PlayerManager:reqChangeNickname(input)
end

return RenameUI