function GameScreen:InitScreen()
    self:LoadScreen()
    
    self.disable = true;
    DelayedExec(0.5, function()
        self.disable = false;
    end)
    
    self.start_player = cpp_get_cur_player()
    
    self:UpdateList()
    
    self:update_buttons();
    
    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("cancel")
    end
    if(key == VK_ENTER and clApplication.curret_win.name == self.name) then
        self:OnClick("ok")
    end
end

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

    if("cancel" == item_name) then
        SoundPlay("sound/interface/button.ogg")
        self.disable = true;
        self:FadeIn(0.5);
        cpp_set_current_player(self.start_player)
        if(self.end_fun) then self:end_fun() end
        
        DelayedExec(0.7, function() 
            clScreenManager:remove_screen(self); 
        end)
    elseif("ok" == 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)
        --[[
        if(self.start_player ~= cpp_get_cur_player()) then
            local last_level = "000_before"
            if(GetState("GLOBAL", "last_screen") ~= "") then
                last_level = GetState("GLOBAL", "last_screen");
            end
            clApplication:ChangeLocation(last_level)
            cpp_save_players()
        end--]]
        
        Inventory:ClearData();
        cpp_update_cursor();
    elseif("create_new" == item_name)then
        self.disable = true;
        SoundPlay("sound/interface/button.ogg")
        local palyer_amount = cpp_get_players_amount()
        
        if(palyer_amount >= 5) then
            self:FadeIn(0.5);
            clApplication.curret_win = clScreenManager:add_screen("list_is_full_screen") 
            clApplication.curret_win.end_fun = function()
                self.disable = false;
                clApplication.curret_win = self;
                self:update_buttons();
                self:FadeOut(0.5);
            end
        else
            self:FadeIn(0.5);
            clApplication.curret_win = clScreenManager:add_screen("new_player_screen") 
            clApplication.curret_win.end_fun = function()
                self.disable = false;
                clApplication.curret_win = self;
                self:update_buttons();
                self:FadeOut(0.5);
                self:UpdateList()
                self:UpdateCursor()
            end
        end
    elseif("delete" == item_name and cpp_get_players_amount() > 1)then
        self.disable = true;
        SoundPlay("sound/interface/button.ogg")
        self:FadeIn(0.5);
        clApplication.curret_win = clScreenManager:add_screen("delete_player_screen") 
        clApplication.curret_win.end_fun = function()
            self.disable = false;
            self:update_buttons();
            clApplication.curret_win = self;
            self:FadeOut(0.5);
            self:UpdateList()
        end
    elseif("rename" == item_name)then
        self.disable = true;
        SoundPlay("sound/interface/button.ogg")
        self:FadeIn(0.5);
        old_player_name = cpp_get_cur_player();
        clApplication.curret_win = clScreenManager:add_screen("rename_player_screen")
        clApplication.curret_win.old_name = cpp_get_cur_player();
        clApplication.curret_win.end_fun = function()
            self.disable = false;
            clApplication.curret_win = self;
            self:FadeOut(0.5);
            self:UpdateList()
        end
    end
    
    local palyer_amount = cpp_get_players_amount()
    for i = 1, palyer_amount do
        if(string.sub(item_name, 15, 15) == ""..i) then
            cpp_set_current_player(cpp_get_player_name_by_index(i - 1))
            self:UpdateCursor()
        end
    end
end 

function GameScreen:UpdateList()
    for i = 1, 10 do
        ItemRemove(self.name, "paiyer_name_"..i)
    end
    
    local palyer_amount = cpp_get_players_amount()
    
    for i = 1, palyer_amount do
        self:CreateLabel("paiyer_name_"..i, cpp_get_player_name_by_index(i - 1), 340, 482 + 75 * (i - 1), 50, FONT_QUICKSAND)
        ItemSetColor("paiyer_name_"..i, 1, 1, 1, 1.0)
    end
    
    self:UpdateCursor();
end

function GameScreen:UpdateCursor()
    local palyer_amount = cpp_get_players_amount()
    
    for i = 1, 5 do
        ItemSetOpacity("player_select_"..i, 0.0)
    end
    
    for i = 1, palyer_amount do
        if(cpp_get_player_name_by_index(i - 1) == cpp_get_cur_player()) then
            ItemSetOpacity("player_select_"..i, 1.0)
            break;
        end
    end
end

function GameScreen:update_buttons()
    local palyer_amount = cpp_get_players_amount()
    if(palyer_amount > 1) then
        ItemSetOpacity("delete", 1.0)
    else
        ItemSetOpacity("delete", 0.0)
    end
end

function GameScreen:LoadScreen()
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "quit_back_trigger_change", "graphics/trigger", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    ItemSetOpacity("quit_back_trigger_change", 0.85)
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "players_window", "graphics/interface/menu/players_window", 263, 294)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "player_select_1", "graphics/interface/menu/player_select", 305, 476)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "player_select_2", "graphics/interface/menu/player_select", 305, 476 + 75)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "player_select_3", "graphics/interface/menu/player_select", 305, 476 + 150)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "player_select_4", "graphics/interface/menu/player_select", 305, 476 + 225)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "player_select_5", "graphics/interface/menu/player_select", 305, 476 + 300)
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "cancel", "graphics/interface/menu/cancel", 286, 957)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "ok", "graphics/interface/menu/ok", 801, 954)
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "rename", "graphics/interface/menu/rename", 282, 861)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "delete", "graphics/interface/menu/delete", 808, 860)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "create_new", "graphics/interface/menu/create_new", 547, 360)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "players_header", "graphics/interface/menu/players_header", 462, 110)
end
