struct NcartTimetableSetup
{
	[ rangeMin = "0" ][ rangeMax = "60" ]
	instanceeditable var departFrequency : Int32;
	default departFrequency = 5;
	[ rangeMin = "0" ][ rangeMax = "30" ]
	instanceeditable var uiUpdateFrequency : Int32;
	default uiUpdateFrequency = 1;
	const instanceeditable var trainLines : array< Int32 >;
}

class NcartTimetableController extends ScriptableDeviceComponent
{

	public const override function GetPS() : NcartTimetableControllerPS
	{
		return ( ( NcartTimetableControllerPS )( GetBasePS() ) );
	}

}

class NcartTimetableControllerPS extends ScriptableDeviceComponentPS
{
	default m_deviceName = "LocKey#1653";
	default m_tweakDBRecord = T"Devices.NcartTimetable";
	default m_tweakDBDescriptionRecord = T"device_descriptions.NcartTimetable";
	default m_hasAuthorizationModule = true;
	private instanceeditable var m_ncartTimetableSetup : NcartTimetableSetup;
	private persistent var m_currentTimeToDepart : Int32;
	private persistent var m_currentLine : Int32;

	protected event OnInstantiated()
	{
		super.OnInstantiated();
		if( !( IsStringValid( m_deviceName ) ) )
		{
			m_deviceName = "LocKey#1653";
		}
	}

	protected override function Initialize()
	{
		super.Initialize();
		ResetTimeToDepart();
	}

	protected const override function CanCreateAnyQuickHackActions() : Bool
	{
		return true;
	}

	protected override function GetQuickHackActions( out outActions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		var currentAction : ScriptableDeviceAction;
		currentAction = ActionGlitchScreen( T"DeviceAction.GlitchScreenSuicide", T"QuickHack.SuicideHackBase" );
		currentAction.SetDurationValue( GetDistractionDuration( currentAction ) );
		outActions.PushBack( currentAction );
		currentAction = ActionGlitchScreen( T"DeviceAction.GlitchScreenBlind", T"QuickHack.BlindHack" );
		currentAction.SetDurationValue( GetDistractionDuration( currentAction ) );
		outActions.PushBack( currentAction );
		currentAction = ActionGlitchScreen( T"DeviceAction.GlitchScreenGrenade", T"QuickHack.GrenadeHackBase" );
		currentAction.SetDurationValue( GetDistractionDuration( currentAction ) );
		outActions.PushBack( currentAction );
		if( !( GlitchScreen.IsDefaultConditionMet( this, context ) ) )
		{
			SetActionsInactiveAll( outActions, "LocKey#7003" );
		}
		currentAction = ActionQuickHackDistraction();
		currentAction.SetObjectActionID( T"DeviceAction.MalfunctionClassHack" );
		currentAction.SetDurationValue( GetDistractionDuration( currentAction ) );
		currentAction.SetInactiveWithReason( QuickHackDistraction.IsDefaultConditionMet( this, context ), "LocKey#7003" );
		outActions.PushBack( currentAction );
		if( IsGlitching() || IsDistracting() )
		{
			SetActionsInactiveAll( outActions, "LocKey#7004" );
		}
		FinalizeGetQuickHackActions( outActions, context );
	}

	public override function GetQuestActions( out outActions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		super.GetQuestActions( outActions, context );
	}

	public const function GetDepartFrequency() : Int32
	{
		return m_ncartTimetableSetup.departFrequency;
	}

	public const function GetUiUpdateFrequency() : Float
	{
		return ( ( Float )( m_ncartTimetableSetup.uiUpdateFrequency ) );
	}

	public const function GetCurrentTimeToDepart() : Int32
	{
		return m_currentTimeToDepart;
	}

	public const function GetCurrentLine() : Int32
	{
		return m_currentLine;
	}

	public const function GetCurrentTimeToDepartAsString() : String
	{
		var timeString : String;
		var secondString : String;
		var seconds : Int32;
		var time : GameTime;
		time = GameTime.MakeGameTime( 0, 0, 0, m_currentTimeToDepart );
		seconds = GameTime.Seconds( time );
		if( seconds < 10 )
		{
			secondString = "0" + IntToString( seconds );
		}
		else
		{
			secondString = IntToString( seconds );
		}
		timeString = ( IntToString( GameTime.Minutes( time ) ) + ":" ) + secondString;
		return timeString;
	}

	public function UpdateCurrentTimeToDepart()
	{
		m_currentTimeToDepart -= m_ncartTimetableSetup.uiUpdateFrequency;
		if( m_currentTimeToDepart <= 0 )
		{
			ResetTimeToDepart();
			m_currentLine = SelectNewRandomLine();
		}
	}

	private function ResetTimeToDepart()
	{
		var time : GameTime;
		time = GameTime.MakeGameTime( 0, 0, m_ncartTimetableSetup.departFrequency, 0 );
		m_currentTimeToDepart = GameTime.GetSeconds( time );
		m_currentLine = SelectNewRandomLine();
	}

	private function SelectNewRandomLine() : Int32
	{
		var size : Int32;
		var random : Int32;
		size = m_ncartTimetableSetup.trainLines.Size();
		if( size == 0 )
		{
			random = RandRange( 1, 6 );
			return random;
		}
		else
		{
			random = RandDifferent( m_currentLine, size );
		}
		return m_ncartTimetableSetup.trainLines[ random ];
	}

	public const override function GetBlackboardDef() : NcartTimetableBlackboardDef
	{
		return GetAllBlackboardDefs().NcartTimetableBlackboard;
	}

	protected override function GetDeviceIconTweakDBID() : TweakDBID
	{
		return T"DeviceIcons.ScreenDeviceIcon";
	}

	protected override function GetBackgroundTextureTweakDBID() : TweakDBID
	{
		return T"DeviceIcons.ScreenDeviceBackground";
	}

}

