clScreenManager = {}

function clScreenManager:Init()
    self.screen_stack = {};
end

function clScreenManager:Update(messenger_table)
    if(self.screen_stack == nil or #self.screen_stack == 0) then return end

    for key, value in pairs(self.screen_stack) do
        value:Update();

        if(messenger_table.on_click ~= "") then value:OnClick(messenger_table.on_click); end
        if(messenger_table.on_mouse_up ~= "") then value:OnMouseUp(messenger_table.on_mouse_up); end
        if(messenger_table.on_mouse_enter ~= "") then value:OnMouseEnter(messenger_table.on_mouse_enter); end
        if(messenger_table.on_mouse_leave ~= "") then value:OnMouseLeave(messenger_table.on_mouse_leave); end
        if(messenger_table.on_mouse_over ~= "") then value:OnMouseMove(messenger_table.on_mouse_over); end
        if(messenger_table.on_mouse_right_down ~= "") then value:OnMouseRightDown(messenger_table.on_mouse_right_down); end
        if(messenger_table.on_mouse_right_up ~= "") then value:OnMouseRightUp(messenger_table.on_mouse_right_up); end
        if(messenger_table.on_button_down ~= "") then value:OnButtonDown(messenger_table.on_button_down); end
        if(messenger_table.on_key_down ~= "") then value:OnKeyDown(messenger_table.on_key_down); end
    end 

end

-------------------------------------
function clScreenManager:add_screen(_screen_name)
    if(self.screen_stack == nil) then self.screen_stack = {} end
     
    local  new_screen = GameScreenList[_screen_name]; 
    if(new_screen.reset_states) then new_screen:reset_states() end
    new_screen:InitScreen();
    
    if(not self:__is_unique_screen(_screen_name)) then return end
    
    table.insert(self.screen_stack, new_screen);
    
    return new_screen;
end
-------------------------------------
function clScreenManager:remove_screen(_scr)
   if(_scr == nil) then return end
    
   for key, value in pairs(self.screen_stack) do
        if(value.name == _scr.name) then
            value:UnLoadScreen();
            table.remove(self.screen_stack, key);
            return true;
        end
    end 
    return false;
end

-------------------------------------
function clScreenManager:remove_screen_by_name(_name)
   if(_name == nil) then return end
    
   for key, value in pairs(self.screen_stack) do
        if(value.name == _name) then
            value:UnLoadScreen();
            table.remove(self.screen_stack, key);
            return true;
        end
    end 
    return false;
end
-------------------------------------
function clScreenManager:remove_all_screen()
   while( #self.screen_stack ~= 0 ) do
        self.screen_stack[1]:UnLoadScreen();  
		table.remove(self.screen_stack, 1);
	end
end
--------------------------------------------------------------------------
--Поверка скрина на уникальность
--Принимат указатель на скрин
--возвращает флаг - уникален ли скрин
function clScreenManager:__is_unique_screen(_screen_name)
    for key, value in pairs(self.screen_stack) do
        if(value.name == _screen_name) then
            return false;
        end
    end
    
    return true;
end