struct MediaResaveData
{
	var m_mediaDeviceData : MediaDeviceData;
}

struct MediaDeviceData
{
	var m_initialStation : Int32;
	var m_amountOfStations : Int32;
	var m_activeChannelName : String;
	var m_isInteractive : Bool;
}

class MediaDeviceController extends ScriptableDeviceComponent
{

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

}

class MediaDeviceControllerPS extends ScriptableDeviceComponentPS
{
	protected var m_previousStation : Int32;
	protected var m_activeChannelName : String;
	protected persistent var m_dataInitialized : Bool;
	protected persistent var m_amountOfStations : Int32;
	protected persistent var m_activeStation : Int32;

	protected const function ActionMediaDeviceStatus() : MediaDeviceStatus
	{
		var action : MediaDeviceStatus;
		action = new MediaDeviceStatus;
		action.clearanceLevel = DefaultActionsParametersHolder.GetStatusClearance();
		action.SetUp( this );
		action.SetProperties( this );
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public virtual function ActionNextStation() : NextStation
	{
		var action : NextStation;
		action = new NextStation;
		action.clearanceLevel = DefaultActionsParametersHolder.GetInteractiveClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		action.CreateActionWidgetPackage();
		action.CreateInteraction();
		return action;
	}

	public virtual function ActionPreviousStation() : PreviousStation
	{
		var action : PreviousStation;
		action = new PreviousStation;
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		action.CreateActionWidgetPackage();
		action.CreateInteraction();
		return action;
	}

	protected function ActionQuestSetChannel() : QuestSetChannel
	{
		var action : QuestSetChannel;
		action = new QuestSetChannel;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties( m_activeStation );
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public override function GetActions( out actions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		super.GetActions( actions, context );
		if( context.requestType == gamedeviceRequestType.Direct && !( IsInteractive() ) )
		{
			return false;
		}
		if( !( IsUserAuthorized( context.processInitiatorObject.GetEntityID() ) ) )
		{
			return false;
		}
		if( context.requestType == gamedeviceRequestType.Remote )
		{
			return false;
		}
		if( ToggleON.IsDefaultConditionMet( this, context ) )
		{
			actions.PushBack( ActionToggleON() );
		}
		if( MediaDeviceStatus.IsDefaultConditionMet( this, context ) && context.requestType == gamedeviceRequestType.External )
		{
			actions.PushBack( ActionMediaDeviceStatus() );
		}
		if( NextStation.IsDefaultConditionMet( this, context ) )
		{
			actions.PushBack( ActionPreviousStation() );
			actions.PushBack( ActionNextStation() );
		}
		SetActionIllegality( actions, m_illegalActions.regularActions );
		return true;
	}

	public override function GetQuestActionByName( actionName : CName ) : DeviceAction
	{
		var action : DeviceAction;
		action = super.GetQuestActionByName( actionName );
		if( action == NULL )
		{
			switch( actionName )
			{
				case 'EnableInteraction':
					action = ActionQuestEnableInteraction();
				break;
				case 'DisableInteraction':
					action = ActionQuestDisableInteraction();
				break;
				case 'ForcePower':
					action = ActionQuestForcePower();
				break;
				case 'SetChannel':
					action = ActionQuestSetChannel();
				break;
			}
		}
		return action;
	}

	public override function GetQuestActions( out outActions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		super.GetQuestActions( outActions, context );
		outActions.PushBack( ActionQuestEnableInteraction() );
		outActions.PushBack( ActionQuestDisableInteraction() );
		outActions.PushBack( ActionQuestNextStation() );
		outActions.PushBack( ActionQuestPreviousStation() );
		outActions.PushBack( ActionQuestDefaultStation() );
		if( Clearance.IsInRange( context.clearance, ActionQuestForcePower().clearanceLevel ) )
		{
			outActions.PushBack( ActionQuestSetChannel() );
		}
	}

	public virtual function OnNextStation( evt : NextStation ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		if( ( IsUnpowered() || IsDisabled() ) || !( IsON() ) )
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
		m_previousStation = m_activeStation;
		m_activeStation = ( m_activeStation + 1 ) % m_amountOfStations;
		Notify( notifier, evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public virtual function OnPreviousStation( evt : PreviousStation ) : EntityNotificationType
	{
		var notifier : ActionNotifier;
		notifier = new ActionNotifier;
		notifier.SetNone();
		if( ( IsUnpowered() || IsDisabled() ) || !( IsON() ) )
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
		m_previousStation = m_activeStation;
		m_activeStation = ( ( m_activeStation - 1 ) + m_amountOfStations ) % m_amountOfStations;
		Notify( notifier, evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public export function OnQuestSetChannel( evt : QuestSetChannel ) : EntityNotificationType
	{
		var stationIDX : Int32;
		var prop : array< DeviceActionProperty >;
		prop = evt.GetProperties();
		DeviceActionPropertyFunctions.GetProperty_Int( prop[ 0 ], stationIDX );
		SetActiveStationIndex( stationIDX );
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected function ActionQuestEnableInteraction() : QuestEnableInteraction
	{
		var action : QuestEnableInteraction;
		action = new QuestEnableInteraction;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public virtual function OnQuestEnableInteraction( evt : QuestEnableInteraction ) : EntityNotificationType
	{
		SetInteractionState( true );
		UseNotifier( evt );
		return EntityNotificationType.SendPSChangedEventToEntity;
	}

	protected function ActionQuestDisableInteraction() : QuestDisableInteraction
	{
		var action : QuestDisableInteraction;
		action = new QuestDisableInteraction;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public virtual function OnQuestDisableInteraction( evt : QuestDisableInteraction ) : EntityNotificationType
	{
		SetInteractionState( false );
		UseNotifier( evt );
		return EntityNotificationType.SendPSChangedEventToEntity;
	}

	protected function ActionQuestDefaultStation() : QuestDefaultStation
	{
		var action : QuestDefaultStation;
		action = new QuestDefaultStation;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	protected function ActionQuestNextStation() : QuestNextStation
	{
		var action : QuestNextStation;
		action = new QuestNextStation;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public virtual function OnQuestNextStation( evt : QuestNextStation ) : EntityNotificationType
	{
		ExecutePSAction( ActionNextStation() );
		return EntityNotificationType.SendPSChangedEventToEntity;
	}

	protected function ActionQuestPreviousStation() : QuestPreviousStation
	{
		var action : QuestPreviousStation;
		action = new QuestPreviousStation;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public virtual function OnQuestPreviousStation( evt : QuestPreviousStation ) : EntityNotificationType
	{
		ExecutePSAction( ActionPreviousStation() );
		return EntityNotificationType.SendPSChangedEventToEntity;
	}

	private function GetQuickHackDistractionActions() {}

	public const override function GetDeviceStatusTextData() : inkTextParams
	{
		var textData : inkTextParams;
		var channelName : String;
		var channelAsNumber : Int32;
		textData = super.GetDeviceStatusTextData();
		if( IsON() )
		{
			channelName = GetActiveStationName();
			channelAsNumber = StringToInt( channelName );
			if( textData )
			{
				if( channelAsNumber > 0 )
				{
					textData.AddString( "TEXT_SECONDARY", channelName );
				}
				else
				{
					textData.AddLocalizedString( "TEXT_SECONDARY", channelName );
				}
			}
		}
		else
		{
			if( textData )
			{
				textData.AddString( "TEXT_SECONDARY", "" );
			}
		}
		return textData;
	}

	protected override function ActionThumbnailUI() : ThumbnailUI
	{
		var action : ThumbnailUI;
		action = super.ActionThumbnailUI();
		if( IsON() )
		{
			action.CreateThumbnailWidgetPackage( "LocKey#42211" );
		}
		return action;
	}

	public const override function GetDeviceStatusAction() : MediaDeviceStatus
	{
		return ActionMediaDeviceStatus();
	}

	public const function GetPreviousStationIndex() : Int32
	{
		return m_previousStation;
	}

	public virtual function GetActiveStationIndex() : Int32
	{
		return m_activeStation;
	}

	public const function GetActiveStationName() : String
	{
		return m_activeChannelName;
	}

	public function SetActiveStationIndex( stationIDX : Int32 )
	{
		m_previousStation = m_activeStation;
		m_activeStation = stationIDX;
	}

	public function PassChannelName( const channelName : ref< String > )
	{
		m_activeChannelName = channelName;
	}

}

