class ConfessionCompletedEvent extends Event
{
}

class ConfessionBooth extends BasicDistractionDevice
{
	default m_controllerTypeName = 'ConfessionBoothController';
	private var m_isShortGlitchActive : Bool;
	private var m_shortGlitchDelayID : DelayID;

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

	public override function ResavePersistentData( ps : PersistentState ) : Bool
	{
		return false;
	}

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

	protected override function ResolveGameplayState()
	{
		super.ResolveGameplayState();
		if( IsUIdirty() && m_isInsideLogicArea )
		{
			RefreshUI();
		}
	}

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

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

	private const override function GetController() : ConfessionBoothController
	{
		return ( ( ConfessionBoothController )( m_controller ) );
	}

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

	protected override function TurnOnDevice()
	{
		TurnOnLights();
	}

	protected override function TurnOffDevice()
	{
		TurnOffLights();
	}

	protected function TurnOnLights()
	{
		var evt : ToggleLightEvent;
		evt = new ToggleLightEvent;
		evt.toggle = true;
		QueueEvent( evt );
	}

	protected function TurnOffLights()
	{
		var evt : ToggleLightEvent;
		evt = new ToggleLightEvent;
		evt.toggle = false;
		QueueEvent( evt );
	}

	private function StartBlinking()
	{
		var evt : ChangeCurveEvent;
		evt = new ChangeCurveEvent;
		evt.time = 1.0;
		evt.curve = 'blink_01';
		evt.loop = true;
		QueueEvent( evt );
	}

	private function StopBlinking()
	{
		var evt : ChangeCurveEvent;
		evt = new ChangeCurveEvent;
		QueueEvent( evt );
	}

	protected override function StartGlitching( glitchState : EGlitchState, optional intensity : Float )
	{
		var glitchData : GlitchData;
		var evt : AdvertGlitchEvent;
		glitchData.state = glitchState;
		glitchData.intensity = intensity;
		if( intensity == 0.0 )
		{
			intensity = 1.0;
		}
		evt = new AdvertGlitchEvent;
		evt.SetShouldGlitch( intensity );
		QueueEvent( evt );
		GetBlackboard().SetVariant( GetBlackboardDef().GlitchData, glitchData, true );
		GetBlackboard().FireCallbacks();
		StartBlinking();
	}

	protected override function StopGlitching()
	{
		var glitchData : GlitchData;
		var evt : AdvertGlitchEvent;
		evt = new AdvertGlitchEvent;
		evt.SetShouldGlitch( 0.0 );
		QueueEvent( evt );
		glitchData.state = EGlitchState.NONE;
		GetBlackboard().SetVariant( GetBlackboardDef().GlitchData, glitchData );
		GetBlackboard().FireCallbacks();
		StopBlinking();
	}

	protected event OnHitEvent( hit : gameHitEvent )
	{
		super.OnHitEvent( hit );
		StartShortGlitch();
	}

	private function StartShortGlitch()
	{
		var evt : StopShortGlitchEvent;
		if( GetDevicePS().IsGlitching() )
		{
			return;
		}
		if( !( m_isShortGlitchActive ) )
		{
			evt = new StopShortGlitchEvent;
			StartGlitching( EGlitchState.DEFAULT, 1.0 );
			m_shortGlitchDelayID = GameInstance.GetDelaySystem( GetGame() ).DelayEvent( this, evt, 0.25 );
			m_isShortGlitchActive = true;
		}
	}

	protected event OnStopShortGlitch( evt : StopShortGlitchEvent )
	{
		m_isShortGlitchActive = false;
		if( !( GetDevicePS().IsGlitching() ) )
		{
			StopGlitching();
		}
	}

	protected event OnConfessionCompleted( evt : ConfessionCompletedEvent )
	{
		if( !( GetDevicePS().IsGlitching() ) )
		{
			StopBlinking();
		}
		StopSound( this, 'dev_confession_booth_confessing' );
	}

	protected event OnConfess( evt : Confess )
	{
		if( evt.IsCompleted() )
		{
			StartConfessing();
			PlaySound( this, 'dev_confession_booth_confessing' );
		}
	}

	private function StartConfessing()
	{
		GetBlackboard().SetBool( GetBlackboardDef().IsConfessing, true );
		GetBlackboard().FireCallbacks();
		StartBlinking();
	}

	private function StopConfessing()
	{
		GetBlackboard().SetBool( GetBlackboardDef().IsConfessing, false );
		GetBlackboard().FireCallbacks();
		StopBlinking();
	}

}

