enum InnerBunkerCoreStatus
{
	Online = 0,
	Offline = 1,
	Unresponsive = 2,
}

enum InnerBunkerCoreStage
{
	Normal = 0,
	Malfunction = 1,
	Shutdown = 2,
}

class BaseInnerBunkerComputerGameController extends gameuiBaseBunkerComputerGameController
{

	protected function GetGame() : GameInstance
	{
		var gameInstance : GameInstance;
		var gameObject : GameObject;
		gameObject = ( ( GameObject )( GetOwnerEntity() ) );
		if( gameObject )
		{
			gameInstance = gameObject.GetGame();
		}
		return gameInstance;
	}

}

class InnerBunkerCoreScreenGameController extends BaseInnerBunkerComputerGameController
{
	editable var m_systems : inkWidgetRef[ 6 ];
	editable var m_statuses : InnerBunkerCoreStatus[ 6 ];
	editable var m_shutdownButton : inkWidgetRef;
	editable var m_processingPanel : inkWidgetRef;
	editable var m_failurePopup : inkWidgetRef;
	editable var m_successPopup : inkWidgetRef;
	editable var m_systemCheckTimeOffline : Float;
	default m_systemCheckTimeOffline = 2.5f;
	editable var m_systemCheckTimeUnresponsive : Float;
	default m_systemCheckTimeUnresponsive = 3.5f;
	editable var m_showResultTime : Float;
	default m_showResultTime = 3.5f;
	editable var m_systemsCheckAnimName : CName;
	default m_systemsCheckAnimName = 'systems_check_loop';
	editable var m_coreStatusNormalAnimName : CName;
	default m_coreStatusNormalAnimName = 'state_nominal';
	editable var m_coreStatusMalfunctionAnimName : CName;
	default m_coreStatusMalfunctionAnimName = 'state_malfunction';
	editable var m_coreStatusShutdownAnimName : CName;
	default m_coreStatusShutdownAnimName = 'state_shutdown';
	editable var m_coreStatusShutingDownAnimName : CName;
	default m_coreStatusShutingDownAnimName = 'state_shuting_down';
	editable var m_failurePopupAnimName : CName;
	default m_failurePopupAnimName = 'popup_01_fail';
	editable var m_successPopupAnimName : CName;
	default m_successPopupAnimName = 'popup_02_success';
	var m_stage : InnerBunkerCoreStage;
	var m_sysIndex : Int32;
	default m_sysIndex = 0;
	var m_systemsCheckAnimProxy : inkAnimProxy;
	var m_resultPopupAnimProxy : inkAnimProxy;
	var m_coreStatusAnimProxy : inkAnimProxy;

	protected event OnInitialize()
	{
		inkWidgetRef.RegisterToCallback( m_shutdownButton, 'OnRelease', this, 'OnShutdownButtonClicked' );
		InitFromFacts();
		ListenToFact( 'q305_all_subsystems_down' );
		ListenToFact( 'q305_core_shutdown_full_set' );
		ListenToFact( 'q305_start_final_shutdown_sequence' );
	}

	protected event OnFactChanged( fact : CName, value : Int32 )
	{
		InitFromFacts();
	}

	public function InitFromFacts()
	{
		var q305_all_subsystems_down : Int32;
		var q305_core_shutdown_full_set : Int32;
		var q305_start_final_shutdown_sequence : Int32;
		var i, count : Int32;
		q305_all_subsystems_down = GetFact( GetGame(), 'q305_all_subsystems_down' );
		q305_core_shutdown_full_set = GetFact( GetGame(), 'q305_core_shutdown_full_set' );
		q305_start_final_shutdown_sequence = GetFact( GetGame(), 'q305_start_final_shutdown_sequence' );
		count = m_systems.Size();
		for( i = 0; i < count; i = i + 1 )
		{
			inkWidgetRef.SetState( m_systems[ i ], 'Default' );
		}
		ResetTimer( 'system_check' );
		ResetTimer( 'show_result' );
		m_sysIndex = 0;
		if( m_systemsCheckAnimProxy )
		{
			m_systemsCheckAnimProxy.Stop();
			m_systemsCheckAnimProxy = NULL;
		}
		if( m_resultPopupAnimProxy )
		{
			m_resultPopupAnimProxy.GotoStartAndStop();
			m_resultPopupAnimProxy = NULL;
		}
		if( m_coreStatusAnimProxy )
		{
			m_coreStatusAnimProxy.Stop();
			m_coreStatusAnimProxy = NULL;
		}
		if( q305_all_subsystems_down > 0 )
		{
			m_stage = InnerBunkerCoreStage.Shutdown;
			SetSystemsStatus( InnerBunkerCoreStatus.Offline );
			if( q305_all_subsystems_down > 1 )
			{
				UpdateVisibility( false );
				SetCoreStatus( m_coreStatusShutdownAnimName );
				AddTimer( 'show_result', m_showResultTime );
			}
			else
			{
				UpdateVisibility( false );
				if( q305_start_final_shutdown_sequence > 0 )
				{
					Shutdown();
				}
			}
		}
		else if( q305_core_shutdown_full_set > 0 )
		{
			m_stage = InnerBunkerCoreStage.Malfunction;
			UpdateVisibility( false );
			SetSystemsStatus();
			SetCoreStatus( m_coreStatusMalfunctionAnimName );
			AddTimer( 'show_result', m_showResultTime );
		}
		else
		{
			m_stage = InnerBunkerCoreStage.Normal;
			UpdateVisibility( true );
			SetSystemsStatus( InnerBunkerCoreStatus.Online );
			SetCoreStatus( m_coreStatusNormalAnimName );
		}
	}

	protected event OnShutdownButtonClicked( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			Shutdown();
		}
	}

	public function Shutdown()
	{
		var playbackOptions : inkAnimOptions;
		if( m_systemsCheckAnimName != '' )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			m_systemsCheckAnimProxy = PlayLibraryAnimation( m_systemsCheckAnimName, playbackOptions );
		}
		SetCoreStatus( m_coreStatusShutingDownAnimName );
		m_sysIndex = 0;
		AddTimer( 'system_check', ( ( m_statuses[ m_sysIndex ] == InnerBunkerCoreStatus.Unresponsive ) ? ( m_systemCheckTimeUnresponsive ) : ( m_systemCheckTimeOffline ) ) );
	}

	protected event OnTimer( timerName : CName )
	{
		var i, count : Int32;
		var checkState, defaultState : CName;
		if( timerName == 'system_check' )
		{
			ResetTimer( 'system_check' );
			checkState = 'Check';
			defaultState = 'Default';
			count = m_systems.Size();
			for( i = 0; i < count; i = i + 1 )
			{
				inkWidgetRef.SetState( m_systems[ i ], ( ( m_sysIndex == i ) ? ( checkState ) : ( defaultState ) ) );
			}
			if( m_sysIndex < count )
			{
				SetFactValue( GetGame(), 'q305_shutdown_error', 0 );
				SetFactValue( GetGame(), 'q305_shutdown_accepted', 0 );
				if( m_stage == InnerBunkerCoreStage.Normal )
				{
					SetStatus( inkWidgetRef.GetController( m_systems[ m_sysIndex ] ), m_statuses[ m_sysIndex ] );
					if( m_statuses[ m_sysIndex ] == InnerBunkerCoreStatus.Unresponsive )
					{
						SetFactValue( GetGame(), 'q305_shutdown_error', 1 );
					}
					else if( m_statuses[ m_sysIndex ] == InnerBunkerCoreStatus.Offline )
					{
						SetFactValue( GetGame(), 'q305_shutdown_accepted', 1 );
					}
				}
				else if( m_stage == InnerBunkerCoreStage.Shutdown )
				{
					SetFactValue( GetGame(), 'q305_shutdown_accepted', 1 );
				}
				m_sysIndex = m_sysIndex + 1;
				if( m_sysIndex < count )
				{
					AddTimer( 'system_check', ( ( m_statuses[ m_sysIndex ] == InnerBunkerCoreStatus.Unresponsive ) ? ( m_systemCheckTimeUnresponsive ) : ( m_systemCheckTimeOffline ) ) );
				}
				else
				{
					AddTimer( 'system_check', m_systemCheckTimeUnresponsive );
				}
			}
			else
			{
				if( m_systemsCheckAnimProxy )
				{
					m_systemsCheckAnimProxy.Stop();
					m_systemsCheckAnimProxy = NULL;
				}
				UpdateFacts();
			}
		}
		else if( timerName == 'show_result' )
		{
			ResetTimer( 'show_result' );
			UpdateFacts();
			ShowResult();
		}
	}

	public function UpdateVisibility( isButtonVisible : Bool )
	{
		inkWidgetRef.SetOpacity( m_shutdownButton, ( ( isButtonVisible ) ? ( 1.0 ) : ( 0.0 ) ) );
		inkWidgetRef.SetOpacity( m_processingPanel, 0.0 );
		inkWidgetRef.SetOpacity( m_failurePopup, 0.0 );
		inkWidgetRef.SetOpacity( m_successPopup, 0.0 );
	}

	public function UpdateFacts()
	{
		var q305_all_subsystems_down : Int32;
		q305_all_subsystems_down = GetFact( GetGame(), 'q305_all_subsystems_down' );
		if( m_stage == InnerBunkerCoreStage.Normal )
		{
			SetFactValue( GetGame(), 'q305_core_shutdown_full_set', 1 );
		}
		else if( m_stage == InnerBunkerCoreStage.Shutdown )
		{
			if( q305_all_subsystems_down > 1 )
			{
				SetFactValue( GetGame(), 'q305_shutdown_finished', 1 );
			}
			else
			{
				SetFactValue( GetGame(), 'q305_all_subsystems_down', 2 );
			}
		}
	}

	public function ShowResult()
	{
		var playbackOptions : inkAnimOptions;
		playbackOptions.loopType = inkanimLoopType.Cycle;
		playbackOptions.loopInfinite = true;
		if( m_stage == InnerBunkerCoreStage.Malfunction )
		{
			if( m_failurePopupAnimName != '' )
			{
				m_resultPopupAnimProxy = PlayLibraryAnimation( m_failurePopupAnimName, playbackOptions );
			}
		}
		else if( m_stage == InnerBunkerCoreStage.Shutdown )
		{
			if( m_successPopupAnimName != '' )
			{
				m_resultPopupAnimProxy = PlayLibraryAnimation( m_successPopupAnimName, playbackOptions );
			}
		}
	}

	public function SetCoreStatus( coreStatusAnimName : CName )
	{
		var playbackOptions : inkAnimOptions;
		if( m_coreStatusAnimProxy )
		{
			m_coreStatusAnimProxy.Stop();
			m_coreStatusAnimProxy = NULL;
		}
		playbackOptions.loopType = inkanimLoopType.Cycle;
		playbackOptions.loopInfinite = true;
		m_coreStatusAnimProxy = PlayLibraryAnimation( coreStatusAnimName, playbackOptions );
	}

	public function SetSystemsStatus()
	{
		var i, count : Int32;
		count = m_systems.Size();
		for( i = 0; i < count; i = i + 1 )
		{
			SetStatus( inkWidgetRef.GetController( m_systems[ i ] ), m_statuses[ i ] );
		}
	}

	public function SetSystemsStatus( status : InnerBunkerCoreStatus )
	{
		var i, count : Int32;
		count = m_systems.Size();
		for( i = 0; i < count; i = i + 1 )
		{
			SetStatus( inkWidgetRef.GetController( m_systems[ i ] ), status );
		}
	}

	public function SetStatus( controller : weak< inkLogicController >, status : InnerBunkerCoreStatus )
	{
		var statusController : InnerBunkerSystemStatusLogicController;
		statusController = ( ( InnerBunkerSystemStatusLogicController )( controller ) );
		if( statusController )
		{
			statusController.SetStatus( status );
		}
	}

}

class InnerBunkerSystemStatusLogicController extends inkLogicController
{
	editable var m_onlineRoot : inkWidgetRef;
	editable var m_offlineRoot : inkWidgetRef;
	editable var m_onlineIco : inkWidgetRef;
	editable var m_offlineIco : inkWidgetRef;
	editable var m_sysIndicator : inkWidgetRef;
	editable var m_stateAnimName : CName;
	const editable var m_widgetsToColor : array< inkWidgetRef >;
	const editable var m_textStatuses : array< inkTextRef >;

	protected event OnInitialize()
	{
		var targets : inkWidgetsSet;
		var playbackOptions : inkAnimOptions;
		GetRootWidget().SetVisible( false );
		if( m_stateAnimName != '' )
		{
			targets = new inkWidgetsSet;
			if( inkWidgetRef.IsValid( m_onlineIco ) )
			{
				targets.Select( inkWidgetRef.Get( m_onlineIco ) );
			}
			if( inkWidgetRef.IsValid( m_sysIndicator ) )
			{
				targets.Select( inkWidgetRef.Get( m_sysIndicator ) );
			}
			if( inkWidgetRef.IsValid( m_offlineIco ) )
			{
				targets.Select( inkWidgetRef.Get( m_offlineIco ) );
			}
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			PlayLibraryAnimationOnTargets( m_stateAnimName, targets, playbackOptions );
		}
	}

	public function SetStatus( status : InnerBunkerCoreStatus )
	{
		var isOnline : Bool;
		var state : CName;
		var locKey : String;
		var i, count : Int32;
		switch( status )
		{
			case InnerBunkerCoreStatus.Online:
				isOnline = true;
			state = 'Online';
			locKey = "LocKey#93579";
			break;
			case InnerBunkerCoreStatus.Offline:
				isOnline = false;
			state = 'Offline';
			locKey = "LocKey#93580";
			break;
			case InnerBunkerCoreStatus.Unresponsive:
				isOnline = true;
			state = 'Unresponsive';
			locKey = "LocKey#93624";
			break;
		}
		inkWidgetRef.SetVisible( m_onlineRoot, isOnline );
		inkWidgetRef.SetVisible( m_offlineRoot, !( isOnline ) );
		count = m_widgetsToColor.Size();
		for( i = 0; i < count; i = i + 1 )
		{
			inkWidgetRef.SetState( m_widgetsToColor[ i ], state );
		}
		count = m_textStatuses.Size();
		for( i = 0; i < count; i = i + 1 )
		{
			inkTextRef.SetLocalizedTextScript( m_textStatuses[ i ], locKey );
		}
		GetRootWidget().SetVisible( true );
	}

}

class InnerSubsystemScreenGameController extends BaseInnerBunkerComputerGameController
{
	editable var m_loopAnimName : CName[ 3 ];
	editable var m_adminAccessPopupAnimName : CName;
	default m_adminAccessPopupAnimName = 'popup_00_admin_access';
	editable var m_unrecognizedPopupAnimName : CName;
	default m_unrecognizedPopupAnimName = 'popup_01_unrecognized';
	editable var m_preAuthorizingPopupAnimName : CName;
	default m_preAuthorizingPopupAnimName = 'popup_02_authorizing_01';
	editable var m_postAuthorizingPopupAnimName : CName;
	default m_postAuthorizingPopupAnimName = 'popup_02_authorizing_02';
	editable var m_deniedPopupAnimName : CName;
	default m_deniedPopupAnimName = 'popup_03_denied';
	editable var m_successPopupAnimName : CName;
	default m_successPopupAnimName = 'popup_04_success';
	editable var m_errorPopupAnimName : CName;
	default m_errorPopupAnimName = 'popup_05_error';
	editable var m_icePopupAnimName : CName;
	default m_icePopupAnimName = 'popup_06_ice';
	editable var m_shutdownButton : inkWidgetRef[ 3 ];
	editable var m_adminPanelButton : inkWidgetRef[ 3 ];
	editable var m_adminPanelPopupButton : inkWidgetRef;
	editable var m_transitionToAuthorization : inkWidgetRef;
	editable var m_transitionToMinigame : inkWidgetRef;
	editable var m_transitionToAdminPanel : inkWidgetRef;
	var m_subsystemIndex : Int32;
	var m_adminAccessPopupAnimProxy : inkAnimProxy;
	var m_successPopupAnimProxy : inkAnimProxy;
	var m_errorPopupAnimProxy : inkAnimProxy;

	protected event OnInitialize()
	{
		var shutdownButton, adminPanelButton : inkWidget;
		var loopAnimName, q305_ib_neural_shutdown_finished : CName;
		var playbackOptions : inkAnimOptions;
		var q305_subsystem_02_minigame_start : Int32;
		m_subsystemIndex = GetFact( GetGame(), 'q305_subsystem_index' ) - 1;
		if( m_subsystemIndex < 0 )
		{
			return false;
		}
		loopAnimName = m_loopAnimName[ m_subsystemIndex ];
		playbackOptions.loopType = inkanimLoopType.Cycle;
		playbackOptions.loopInfinite = true;
		if( loopAnimName != '' )
		{
			PlayLibraryAnimation( loopAnimName, playbackOptions );
		}
		if( m_subsystemIndex == 0 )
		{
			q305_ib_neural_shutdown_finished = 'q305_ib_neural_shutdown_finished';
			ListenToFact( q305_ib_neural_shutdown_finished );
			OnFactChanged( q305_ib_neural_shutdown_finished, GetFact( GetGame(), q305_ib_neural_shutdown_finished ) );
		}
		else if( m_subsystemIndex == 1 )
		{
			q305_subsystem_02_minigame_start = GetFact( GetGame(), 'q305_subsystem_02_minigame_start' );
			if( q305_subsystem_02_minigame_start < 3 )
			{
				shutdownButton = inkWidgetRef.Get( m_shutdownButton[ m_subsystemIndex ] );
				if( shutdownButton )
				{
					shutdownButton.RegisterToCallback( 'OnRelease', this, 'OnShutdownButtonClicked' );
				}
				adminPanelButton = inkWidgetRef.Get( m_adminPanelButton[ m_subsystemIndex ] );
				if( adminPanelButton )
				{
					adminPanelButton.RegisterToCallback( 'OnRelease', this, 'OnAdminPanelButtonClicked' );
				}
				inkWidgetRef.RegisterToCallback( m_adminPanelPopupButton, 'OnRelease', this, 'OnAdminPanelButtonClicked' );
			}
			if( q305_subsystem_02_minigame_start > 4 )
			{
				ShowPostAuthorizingPopup();
			}
			else if( q305_subsystem_02_minigame_start > 3 )
			{
				ShowPreAuthorizingPopup( true );
			}
			else if( q305_subsystem_02_minigame_start > 2 )
			{
				ShowDeniedPopup();
			}
			else if( q305_subsystem_02_minigame_start > 1 )
			{
				if( GetFact( GetGame(), 'q305_subsystem_02_pre_authorizing' ) > 0 )
				{
					ShowPreAuthorizingPopup( false );
				}
				else
				{
					SetFactValue( GetGame(), 'q305_subsystem_02_pre_authorizing', 1 );
					ShowUnrecognizedUserPopup( true );
				}
			}
			else if( q305_subsystem_02_minigame_start > 0 )
			{
				ShowUnrecognizedUserPopup( true );
			}
		}
	}

	protected event OnFactChanged( fact : CName, value : Int32 )
	{
		if( fact == 'q305_ib_neural_shutdown_finished' )
		{
			if( value > 0 )
			{
				ShowSuccessPopup();
			}
			else
			{
				ShowErrorPopup();
			}
		}
	}

	public function DisableButtons()
	{
		var i, count : Int32;
		count = m_shutdownButton.Size();
		for( i = 0; i < count; i = i + 1 )
		{
			inkWidgetRef.SetInteractive( m_shutdownButton[ i ], false );
		}
		count = m_adminPanelButton.Size();
		for( i = 0; i < count; i = i + 1 )
		{
			inkWidgetRef.SetInteractive( m_adminPanelButton[ i ], false );
		}
	}

	protected event OnShutdownButtonClicked( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			DisableButtons();
			ShowAdminAccessPopup();
		}
	}

	protected event OnAdminPanelButtonClicked( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			DisableButtons();
			ShowUnrecognizedUserPopup( false );
		}
	}

	public function ShowAdminAccessPopup()
	{
		var playbackOptions : inkAnimOptions;
		if( m_adminAccessPopupAnimName != '' )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			m_adminAccessPopupAnimProxy = PlayLibraryAnimation( m_adminAccessPopupAnimName, playbackOptions );
		}
	}

	public function ShowUnrecognizedUserPopup( fromInit : Bool )
	{
		var animProxy : inkAnimProxy;
		var playbackOptions : inkAnimOptions;
		if( m_adminAccessPopupAnimProxy )
		{
			m_adminAccessPopupAnimProxy.Stop();
		}
		if( m_unrecognizedPopupAnimName != '' )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			animProxy = PlayLibraryAnimation( m_unrecognizedPopupAnimName, playbackOptions );
			if( !( fromInit ) )
			{
				animProxy.RegisterToCallback( inkanimEventType.OnEndLoop, this, 'OnUnrecognizedUserPopupEndLoop' );
			}
		}
	}

	protected event OnUnrecognizedUserPopupEndLoop( proxy : inkAnimProxy )
	{
		var webPageController : WebPage;
		webPageController = ( ( WebPage )( GetRootWidget().GetController() ) );
		if( proxy )
		{
			proxy.UnregisterFromAllCallbacks( inkanimEventType.OnEndLoop );
			proxy.Stop();
		}
		webPageController.ActivateCanvasLink( inkWidgetRef.Get( m_transitionToAuthorization ) );
	}

	public function ShowPreAuthorizingPopup( startMinigame : Bool )
	{
		var animProxy : inkAnimProxy;
		if( m_preAuthorizingPopupAnimName != '' )
		{
			animProxy = PlayLibraryAnimation( m_preAuthorizingPopupAnimName );
			if( startMinigame )
			{
				animProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnShowPreAuthorizingPopupAnimFinished' );
			}
		}
	}

	protected event OnShowPreAuthorizingPopupAnimFinished( proxy : inkAnimProxy )
	{
		var webPageController : WebPage;
		webPageController = ( ( WebPage )( GetRootWidget().GetController() ) );
		if( proxy )
		{
			proxy.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
		}
		webPageController.ActivateCanvasLink( inkWidgetRef.Get( m_transitionToMinigame ) );
	}

	public function ShowPostAuthorizingPopup()
	{
		var animProxy : inkAnimProxy;
		if( m_postAuthorizingPopupAnimName != '' )
		{
			animProxy = PlayLibraryAnimation( m_postAuthorizingPopupAnimName );
			animProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnShowPostAuthorizingPopupAnimFinished' );
		}
	}

	protected event OnShowPostAuthorizingPopupAnimFinished( proxy : inkAnimProxy )
	{
		var webPageController : WebPage;
		webPageController = ( ( WebPage )( GetRootWidget().GetController() ) );
		if( proxy )
		{
			proxy.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
		}
		webPageController.ActivateCanvasLink( inkWidgetRef.Get( m_transitionToAdminPanel ) );
	}

	public function ShowDeniedPopup()
	{
		var animProxy : inkAnimProxy;
		if( m_deniedPopupAnimName != '' )
		{
			animProxy = PlayLibraryAnimation( m_deniedPopupAnimName );
			animProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnDeniedPopupAnimFinished' );
		}
	}

	protected event OnDeniedPopupAnimFinished( proxy : inkAnimProxy )
	{
		var playbackOptions : inkAnimOptions;
		if( proxy )
		{
			proxy.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
		}
		if( m_icePopupAnimName != '' )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			PlayLibraryAnimation( m_icePopupAnimName, playbackOptions );
		}
	}

	public function ShowErrorPopup()
	{
		var playbackOptions : inkAnimOptions;
		m_successPopupAnimProxy.Stop();
		if( m_errorPopupAnimName != '' )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			m_errorPopupAnimProxy = PlayLibraryAnimation( m_errorPopupAnimName, playbackOptions );
		}
	}

	public function ShowSuccessPopup()
	{
		var playbackOptions : inkAnimOptions;
		m_errorPopupAnimProxy.Stop();
		if( m_successPopupAnimName != '' )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			m_successPopupAnimProxy = PlayLibraryAnimation( m_successPopupAnimName, playbackOptions );
		}
	}

}

class InnerAdminPanelScreenGameController extends BaseInnerBunkerComputerGameController
{
	editable var m_introAnimName : CName;
	default m_introAnimName = 'intro';
	editable var m_loopAnimName : CName;
	default m_loopAnimName = 'code_loop';
	editable var m_buttonAnimName : CName[ 3 ];
	editable var m_commandAnimName : CName[ 3 ];
	editable var m_successAnimName : CName[ 3 ];
	editable var m_successPopupAnimName : CName;
	default m_successPopupAnimName = 'popup_01_success';
	editable var m_attemptAnimName : CName;
	default m_attemptAnimName = 'datafort_command_animation_loop';
	editable var m_instantAttemptAnimName : CName;
	default m_instantAttemptAnimName = 'datafort_command_instant_loop';
	editable var m_instantAttemptPopupAnimName : CName;
	default m_instantAttemptPopupAnimName = 'popup_03_datafort_instant_loop';
	editable var m_shutdownButton : inkWidgetRef;
	var m_subsystemIndex : Int32;
	var m_animProxyFull1 : inkAnimProxy;
	var m_animProxyFull2 : inkAnimProxy;
	var m_animProxySuccess : inkAnimProxy;
	var m_animProxySuccessPopup : inkAnimProxy;
	var m_animProxyAttempt : inkAnimProxy;
	var m_animProxyAttemptPopup : inkAnimProxy;
	var m_zoomUICallbackHandle : CallbackHandle;
	var m_isUIZoomDevice : Bool;

	protected event OnInitialize()
	{
		var fact : CName;
		m_subsystemIndex = GetFact( GetGame(), 'q305_subsystem_index' ) - 1;
		if( m_subsystemIndex == 1 )
		{
			fact = 'q305_ib_thermal_shutdown_finished';
		}
		else if( m_subsystemIndex == 2 )
		{
			fact = 'q305_ib_datafort_shutdown_finished';
		}
		if( fact != '' )
		{
			ListenToFact( fact );
			OnFactChanged( fact, GetFact( GetGame(), fact ) );
		}
	}

	protected event OnUninitialize()
	{
		OnPlayerDetach( GetPlayerControlledObject() );
	}

	protected event OnPlayerAttach( playerPuppet : GameObject )
	{
		var blackboard : IBlackboard;
		blackboard = GetPSMBlackboard( playerPuppet );
		if( blackboard )
		{
			m_zoomUICallbackHandle = blackboard.RegisterListenerBool( GetAllBlackboardDefs().PlayerStateMachine.IsUIZoomDevice, this, 'OnIsUIZoomDeviceChange' );
		}
	}

	protected event OnPlayerDetach( playerPuppet : GameObject )
	{
		var blackboard : IBlackboard;
		blackboard = GetPSMBlackboard( playerPuppet );
		if( blackboard && m_zoomUICallbackHandle )
		{
			blackboard.UnregisterListenerBool( GetAllBlackboardDefs().PlayerStateMachine.IsUIZoomDevice, m_zoomUICallbackHandle );
		}
	}

	protected event OnIsUIZoomDeviceChange( value : Bool )
	{
		var fact : CName;
		m_isUIZoomDevice = value;
		if( m_subsystemIndex == 2 )
		{
			fact = 'q305_ib_datafort_shutdown_finished';
			OnFactChanged( fact, GetFact( GetGame(), fact ) );
		}
	}

	protected event OnFactChanged( fact : CName, value : Int32 )
	{
		m_animProxyFull1.Stop();
		m_animProxyFull2.Stop();
		m_animProxySuccess.Stop();
		m_animProxySuccessPopup.Stop();
		m_animProxyAttempt.Stop();
		m_animProxyAttemptPopup.Stop();
		if( value > 0 )
		{
			StartSuccessLine();
		}
		else
		{
			if( fact == 'q305_ib_thermal_shutdown_finished' )
			{
				StartFullLine();
			}
			else if( fact == 'q305_ib_datafort_shutdown_finished' )
			{
				StartAttemptLine();
			}
		}
	}

	public function StartFullLine()
	{
		if( m_introAnimName != '' )
		{
			m_animProxyFull1 = PlayLibraryAnimation( m_introAnimName );
			m_animProxyFull1.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnIntroAnimFinished' );
		}
		inkWidgetRef.RegisterToCallback( m_shutdownButton, 'OnRelease', this, 'OnShutdownButtonClicked' );
	}

	protected event OnIntroAnimFinished( proxy : inkAnimProxy )
	{
		var buttonAnimName : CName;
		if( proxy )
		{
			proxy.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
		}
		buttonAnimName = m_buttonAnimName[ m_subsystemIndex ];
		if( buttonAnimName != '' )
		{
			m_animProxyFull1 = PlayLibraryAnimation( buttonAnimName );
			m_animProxyFull1.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnButtonAnimFinished' );
		}
	}

	protected event OnButtonAnimFinished( proxy : inkAnimProxy )
	{
		if( proxy )
		{
			proxy.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
		}
		StartLoopAnim();
	}

	public function StartLoopAnim()
	{
		var playbackOptions : inkAnimOptions;
		if( m_loopAnimName != '' )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			m_animProxyFull1 = PlayLibraryAnimation( m_loopAnimName, playbackOptions );
		}
	}

	protected event OnShutdownButtonClicked( e : inkPointerEvent )
	{
		var commandAnimName : CName;
		if( e.IsAction( 'click' ) )
		{
			commandAnimName = m_commandAnimName[ m_subsystemIndex ];
			if( commandAnimName != '' )
			{
				m_animProxyFull2 = PlayLibraryAnimation( commandAnimName );
				m_animProxyFull2.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnCommandAnimFinished' );
				inkWidgetRef.SetInteractive( m_shutdownButton, false );
				if( m_subsystemIndex == 1 )
				{
					SetFactValue( GetGame(), 'q305_thermal_netrunner_shutdown', 1 );
				}
			}
		}
	}

	protected event OnCommandAnimFinished( proxy : inkAnimProxy )
	{
		if( proxy )
		{
			proxy.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
		}
		if( m_subsystemIndex == 2 )
		{
			StartAttemptLine();
		}
		else
		{
			StartSuccessLine();
		}
	}

	public function StartSuccessLine()
	{
		var playbackOptions : inkAnimOptions;
		var successAnimName : CName;
		successAnimName = m_successAnimName[ m_subsystemIndex ];
		if( successAnimName != '' )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			m_animProxySuccess = PlayLibraryAnimation( successAnimName, playbackOptions );
			m_animProxySuccess.RegisterToCallback( inkanimEventType.OnEndLoop, this, 'OnSuccessAnimFinished' );
		}
	}

	protected event OnSuccessAnimFinished( proxy : inkAnimProxy )
	{
		var playbackOptions : inkAnimOptions;
		if( proxy )
		{
			proxy.UnregisterFromAllCallbacks( inkanimEventType.OnEndLoop );
		}
		if( m_successPopupAnimName != '' )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			m_animProxySuccessPopup = PlayLibraryAnimation( m_successPopupAnimName, playbackOptions );
			if( m_subsystemIndex == 1 )
			{
				SetFactValue( GetGame(), 'q305_thermal_netrunner_shutdown', 2 );
			}
		}
	}

	public function StartAttemptLine()
	{
		var playbackOptions : inkAnimOptions;
		playbackOptions.loopType = inkanimLoopType.Cycle;
		playbackOptions.loopInfinite = true;
		if( m_isUIZoomDevice )
		{
			if( m_instantAttemptAnimName != '' )
			{
				m_animProxyAttempt = PlayLibraryAnimation( m_instantAttemptAnimName, playbackOptions );
				m_animProxyAttempt.RegisterToCallback( inkanimEventType.OnEndLoop, this, 'OnAttemptAnimFinished' );
			}
		}
		else
		{
			if( m_attemptAnimName != '' )
			{
				m_animProxyAttempt = PlayLibraryAnimation( m_attemptAnimName, playbackOptions );
			}
		}
		StartLoopAnim();
	}

	protected event OnAttemptAnimFinished( proxy : inkAnimProxy )
	{
		var playbackOptions : inkAnimOptions;
		if( proxy )
		{
			proxy.UnregisterFromAllCallbacks( inkanimEventType.OnEndLoop );
		}
		if( m_instantAttemptPopupAnimName != '' )
		{
			playbackOptions.loopType = inkanimLoopType.Cycle;
			playbackOptions.loopInfinite = true;
			m_animProxyAttemptPopup = PlayLibraryAnimation( m_instantAttemptPopupAnimName, playbackOptions );
		}
	}

}

