class JukeboxController extends ScriptableDeviceComponent
{

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

}

class Jukebox extends InteractiveDevice
{
	default m_controllerTypeName = 'JukeboxController';
	protected var m_uiComponentBig : weak< IWorldWidgetComponent >;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		super.OnRequestComponents( ri );
		EntityRequestComponentsInterface.RequestComponent( ri, 'ui', 'worlduiWidgetComponent', false );
		EntityRequestComponentsInterface.RequestComponent( ri, 'bigAdvertScreen', 'worlduiWidgetComponent', false );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		super.OnTakeControl( ri );
		m_uiComponent = ( ( worlduiWidgetComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'ui' ) ) );
		m_uiComponentBig = ( ( worlduiWidgetComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'bigAdvertScreen' ) ) );
		m_controller = ( ( JukeboxController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	public function IsPlaying() : Bool
	{
		return GetDevicePS().IsPlaying();
	}

	protected function PlayGivenStation()
	{
		AudioSwitch( this, 'radio_station', GetDevicePS().GetRadioStationEventName(), 'radio' );
		SendDataToUIBlackboard( true );
	}

	protected function StopPlayingStation()
	{
		AudioSwitch( this, 'radio_station', 'station_none', 'radio' );
		SendDataToUIBlackboard( false );
	}

	protected event OnTogglePlay( evt : TogglePlay )
	{
		if( IsPlaying() )
		{
			PlayGivenStation();
		}
		else
		{
			StopPlayingStation();
		}
	}

	protected event OnNextStation( evt : NextStation )
	{
		PlayGivenStation();
	}

	protected event OnPreviousStation( evt : PreviousStation )
	{
		PlayGivenStation();
	}

	protected override function StartGlitching( glitchState : EGlitchState, optional intensity : Float )
	{
		AdvertGlitch( true, GetGlitchData( glitchState ) );
		PlaySoundEvent( this, GetDevicePS().GetGlitchSFX() );
		RefreshUI();
	}

	protected override function StopGlitching()
	{
		AdvertGlitch( false, GetGlitchData( EGlitchState.NONE ) );
	}

	protected override function TurnOnDevice()
	{
		m_uiComponent.Toggle( true );
		m_uiComponentBig.Toggle( true );
		PlayGivenStation();
		UpdateDeviceState();
	}

	protected override function TurnOffDevice()
	{
		super.TurnOffDevice();
		m_uiComponent.Toggle( false );
		m_uiComponentBig.Toggle( false );
		StopPlayingStation();
		UpdateDeviceState();
	}

	protected function AdvertGlitch( start : Bool, data : GlitchData )
	{
		SimpleGlitch( start );
		GetBlackboard().SetVariant( GetBlackboardDef().GlitchData, data, true );
		GetBlackboard().FireCallbacks();
	}

	protected function SimpleGlitch( on : Bool )
	{
		var evt : AdvertGlitchEvent;
		evt = new AdvertGlitchEvent;
		if( on )
		{
			evt.SetShouldGlitch( 1.0 );
		}
		else
		{
			evt.SetShouldGlitch( 0.0 );
		}
		QueueEvent( evt );
	}

	protected function GetGlitchData( glitchState : EGlitchState ) : GlitchData
	{
		var data : GlitchData;
		data.state = glitchState;
		if( glitchState != EGlitchState.NONE )
		{
			data.intensity = 1.0;
		}
		return data;
	}

	protected function SendDataToUIBlackboard( isPlaying : Bool )
	{
		GetBlackboard().SetBool( GetBlackboardDef().IsPlaying, isPlaying );
		GetBlackboard().FireCallbacks();
	}

	protected override function CreateBlackboard()
	{
		m_blackboard = IBlackboard.Create( GetAllBlackboardDefs().JukeboxBlackboard );
	}

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

	public const override function GetDevicePS() : JukeboxControllerPS
	{
		return GetController().GetPS();
	}

	protected const override function GetController() : JukeboxController
	{
		return ( ( JukeboxController )( m_controller ) );
	}

	public const override function DeterminGameplayRole() : EGameplayRole
	{
		return EGameplayRole.Distract;
	}

}

