--------------------------------------------------------------------------------
-- redesign

PopUpScreenManagerClass = CreateInheritedClass(LuaStack);

--------------------------------------------------------------------------------
function PopUpScreenManagerClass:Push(screen,
									  hideActiveScreen,
									  onScreenDestroyHandler)
	hideActiveScreen = (hideActiveScreen == nil and true or hideActiveScreen);
	screen.onDestroyHandler = onScreenDestroyHandler;
    
    local curScr = self:GetTop();
    local layerCurScr = 1.0;
    if curScr then
        if hideActiveScreen then
            curScr:ScreenHide()
        else
            curScr:Stop()
        end
        
        layerCurScr = curScr:GetLayer()
    end
    LuaStack.Push(self, screen);
    
    screen:SetLayer(layerCurScr + 1.0)
    screen:SetOpacity(0)
	screen:SetFadeSpeedMultiplier(2)
    screen:ScreenShow()
    
    GM:SetCurrentScreen(screen)
    
    SetCursor('CursorDefault', true)
end

-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function PopUpScreenManagerClass:auxDestroyScreen(screen)
    assert(screen)

	if (screen.onDestroyHandler) then
		screen.onDestroyHandler(screen)
	end
	
	if screen:IsPaused() then
	    screen:Destroy()
    else
        screen:ScreenDestroy()
    end
end

--------------------------------------------------------------------------------
function PopUpScreenManagerClass:Pop()
    --
    local curScr = self:GetTop()
    assert(curScr ~= nil)
    
    LuaStack.Pop(self)
    self:auxDestroyScreen(curScr)
    
    curScr = self:GetTop()
    if curScr then
        curScr:ScreenShow()
    end
end

--------------------------------------------------------------------------------
function PopUpScreenManagerClass:DestroyAll()
    local screen = PopUpScreensMgr:GetTop()
    while (screen)
    do
        self:auxDestroyScreen(screen)
        LuaStack.Pop(self)
        screen = PopUpScreensMgr:GetTop()
    end
end

--------------------------------------------------------------------------------
-- convenient to be passed as a callback
function PopCurrentPopUpScreen()
    PopUpScreensMgr:Pop()
end

--------------------------------------------------------------------------------
PopUpScreensMgr = PopUpScreenManagerClass:New();
