struct JukeboxSetup
{
	instanceeditable var m_randomizeStartingStation : Bool;
	instanceeditable var m_startingStation : ERadioStationList;
	[ customEditor = "AudioEvent" ]
	instanceeditable var m_glitchSFX : CName;
	[ customEditor = "TweakDBGroupInheritance;ActionPayment" ]
	instanceeditable var m_paymentRecordID : TweakDBID;
}

class JukeboxControllerPS extends ScriptableDeviceComponentPS
{
	default m_deviceName = "LocKey#165";
	default m_tweakDBRecord = T"Devices.Jukebox";
	default m_tweakDBDescriptionRecord = T"device_descriptions.Jukebox";
	protected instanceeditable var m_jukeboxSetup : JukeboxSetup;
	protected persistent var m_activeStation : Int32;
	protected var m_isPlaying : Bool;
	default m_isPlaying = true;

	protected override function GameAttached()
	{
		m_activeStation = ( ( Int32 )( GetStartingStation() ) );
	}

	private function GetStartingStation() : ERadioStationList
	{
		return ( ( m_jukeboxSetup.m_randomizeStartingStation ) ? ( RadioStationDataProvider.GetRandomStation() ) : ( m_jukeboxSetup.m_startingStation ) );
	}

	public override function GetActions( out actions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		super.GetActions( actions, context );
		if( context.requestType == gamedeviceRequestType.Remote )
		{
			return false;
		}
		if( TogglePlay.IsDefaultConditionMet( this, context ) )
		{
			actions.PushBack( ActionTogglePlay() );
		}
		if( NextStation.IsDefaultConditionMet( this, context ) )
		{
			actions.PushBack( ActionPreviousStation() );
			actions.PushBack( ActionNextStation() );
		}
		SetActionIllegality( actions, m_illegalActions.regularActions );
		return true;
	}

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

	protected override function GetQuickHackActions( out actions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		var action : ScriptableDeviceAction;
		action = ActionQuickHackDistraction();
		action.SetInactiveWithReason( !( IsDistracting() ), "LocKey#7004" );
		actions.PushBack( action );
		FinalizeGetQuickHackActions( actions, context );
	}

	public const function GetPaymentRecordID() : TweakDBID
	{
		return m_jukeboxSetup.m_paymentRecordID;
	}

	protected function ActionTogglePlay() : TogglePlay
	{
		var action : TogglePlay;
		action = new TogglePlay;
		action.SetUp( this );
		action.SetProperties( !( m_isPlaying ) );
		action.AddDeviceName( m_deviceName );
		action.CreateActionWidgetPackage();
		return action;
	}

	protected function ActionPreviousStation() : PreviousStation
	{
		var action : PreviousStation;
		action = new PreviousStation;
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		action.SetExecutor( GetPlayer( GetGameInstance() ) );
		action.SetInkWidgetTweakDBID( T"DevicesUIDefinitions.JukeboxPreviousActionWidget" );
		action.CreateActionWidgetPackage();
		if( TDBID.IsValid( GetPaymentRecordID() ) )
		{
			action.SetObjectActionID( GetPaymentRecordID() );
		}
		return action;
	}

	public function ActionNextStation() : NextStation
	{
		var action : NextStation;
		action = new NextStation;
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		action.SetExecutor( GetPlayer( GetGameInstance() ) );
		action.SetInkWidgetTweakDBID( T"DevicesUIDefinitions.JukeboxNextActionWidget" );
		action.CreateActionWidgetPackage();
		if( TDBID.IsValid( GetPaymentRecordID() ) )
		{
			action.SetObjectActionID( GetPaymentRecordID() );
		}
		return action;
	}

	protected override function ActionQuickHackDistraction() : QuickHackDistraction
	{
		var action : QuickHackDistraction;
		action = super.ActionQuickHackDistraction();
		action.SetDurationValue( GetDistractionDuration( action ) );
		action.SetObjectActionID( T"DeviceAction.MalfunctionClassHack" );
		return action;
	}

	public function GetGlitchSFX() : CName
	{
		return m_jukeboxSetup.m_glitchSFX;
	}

	public function IsPlaying() : Bool
	{
		return m_isPlaying;
	}

	public function OnTogglePlay( evt : TogglePlay ) : EntityNotificationType
	{
		if( !( IsON() ) )
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
		m_isPlaying = ( ( Bool )evt.prop.first );
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public function OnNextStation( evt : NextStation ) : EntityNotificationType
	{
		if( !( IsON() ) || !( evt.CanPayCost( GetPlayer( GetGameInstance() ) ) ) )
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
		m_activeStation = ( ( Int32 )( RadioStationDataProvider.GetNextStationTo( ( ( ERadioStationList )( m_activeStation ) ) ) ) );
		m_isPlaying = true;
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public function OnPreviousStation( evt : PreviousStation ) : EntityNotificationType
	{
		if( !( IsON() ) || !( evt.CanPayCost( GetPlayer( GetGameInstance() ) ) ) )
		{
			return EntityNotificationType.DoNotNotifyEntity;
		}
		m_activeStation = ( ( Int32 )( RadioStationDataProvider.GetPreviousStationTo( ( ( ERadioStationList )( m_activeStation ) ) ) ) );
		m_isPlaying = true;
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	public override function OnQuickHackDistraction( evt : QuickHackDistraction ) : EntityNotificationType
	{
		var type : EntityNotificationType;
		type = super.OnQuickHackDistraction( evt );
		if( type == EntityNotificationType.DoNotNotifyEntity )
		{
			return type;
		}
		if( evt.IsStarted() )
		{
			if( IsOFF() )
			{
				ExecutePSAction( ActionSetDeviceON() );
			}
			ExecutePSAction( ActionNextStation() );
		}
		return EntityNotificationType.SendThisEventToEntity;
	}

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

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

	public const override function GetBlackboardDef() : JukeboxBlackboardDef
	{
		return GetAllBlackboardDefs().JukeboxBlackboard;
	}

	public function GetRadioStationEventName() : CName
	{
		return RadioStationDataProvider.GetStationNameByIndex( m_activeStation );
	}

}

