function GameScreen:InitScreen()
    self:LoadScreen()
    
    self.disable = false;
    
    self:FadeOut();
end

function GameScreen:OnKeyDown(key)
    if(self.disable) then return end

    if(key == VK_ESCAPE and clApplication.curret_win.name == self.name) then
        self:OnClick("rename_player_cancel")
    end
    if(key == VK_ENTER and clApplication.curret_win.name == self.name and cpp_get_label_text("player_name_edit") ~= "") then
        self:OnClick("rename_player_ok")
    end
end

function GameScreen:OnClick(item_name)
    if(self.disable) then return end

    if("rename_player_cancel" == item_name) then
        SoundPlay("sound/interface/button.ogg")
        self.disable = true;
        self:FadeIn(0.5);
        if(self.end_fun) then self:end_fun() end
        
        DelayedExec(0.7, function() 
            clScreenManager:remove_screen(self); 
        end)
    elseif("rename_player_ok" == item_name)then
        if(not cpp_is_player_enable(cpp_get_label_text("player_name_edit")) or self.old_name == cpp_get_label_text("player_name_edit"))then
            SoundPlay("sound/interface/button.ogg")
            self.disable = true;
            self:FadeIn(0.5);
            cpp_edit_player(self.old_name, cpp_get_label_text("player_name_edit"))
            cpp_save_players()
            
            if(self.end_fun) then self:end_fun() end
            
            DelayedExec(0.7, function() 
                clScreenManager:remove_screen(self); 
            end)
        else
            self.disable = true;
            SoundPlay("sound/interface/wrong.ogg")
            self:FadeIn(0.5);
            local option_screen = clScreenManager:add_screen("player_exist_screen") 
            option_screen.end_fun = function()
                cpp_set_focus(self.name)
                cpp_set_focus("player_name_edit")
                DelayedExec(0.5, function()
                    self.disable = false;
                end)
                self:FadeOut(0.5);
            end
        end
    end
end

function GameScreen:Update()
    if(cpp_get_label_text("player_name_edit") == "") then
        ItemSetOpacity("rename_player_ok", 0.0)
        ItemSetPosition("rename_player_ok", -100, -100)
    else
        ItemSetPosition("rename_player_ok", 803, 729)
        ItemSetOpacity("rename_player_ok", 1.0)
    end
end

function GameScreen:LoadScreen()
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "quit_back_trigger_ren", "graphics/trigger", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    ItemSetOpacity("quit_back_trigger_ren", 0.85)
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "enter_your_name", "graphics/interface/menu/enter_your_name", 267, 452);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "rename_player_cancel", "graphics/interface/menu/cancel", 286, 734);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "rename_player_ok", "graphics/interface/menu/ok", 803, 729);
    
    self:CreateTextEdit("player_name_edit", old_player_name, 300, 604, 58, FONT_QUICKSAND);
    ItemSetColor("player_name_edit", 1, 1, 1, 1.0)
    old_player_name = nil;
end
