struct FanResaveData
{
	var m_animationType : EAnimationType;
	var m_rotateClockwise : Bool;
	var m_randomizeBladesSpeed : Bool;
	var m_maxRotationSpeed : Float;
	var m_timeToMaxRotation : Float;
}

class Fan extends BasicDistractionDevice
{
	default m_controllerTypeName = 'FanController';
	var m_animationType : EAnimationType;
	var m_rotateClockwise : Bool;
	default m_rotateClockwise = true;
	var m_randomizeBladesSpeed : Bool;
	default m_randomizeBladesSpeed = false;
	var m_maxRotationSpeed : Float;
	default m_maxRotationSpeed = 150.f;
	var m_timeToMaxRotation : Float;
	default m_timeToMaxRotation = 3.f;
	private var m_animFeature : AnimFeature_RotatingObject;
	private var m_updateComp : UpdateComponent;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		EntityRequestComponentsInterface.RequestComponent( ri, 'update', 'UpdateComponent', false );
		EntityRequestComponentsInterface.RequestComponent( ri, 'anim', 'AnimatedComponent', false );
		super.OnRequestComponents( ri );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		m_updateComp = ( ( UpdateComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'update' ) ) );
		super.OnTakeControl( ri );
		m_animFeature = new AnimFeature_RotatingObject;
		m_controller = ( ( FanController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected event OnDeviceVisible( evt : gameDeviceVisibilityChangedEvent )
	{
		super.OnDeviceVisible( evt );
	}

	protected override function ResolveGameplayState()
	{
		super.ResolveGameplayState();
		m_animFeature.rotateClockwise = m_rotateClockwise;
		m_animFeature.maxRotationSpeed = m_maxRotationSpeed;
		m_animFeature.timeToMaxRotation = m_timeToMaxRotation;
		m_animFeature.randomizeBladesRotation = m_randomizeBladesSpeed;
		AnimationControllerComponent.ApplyFeature( this, 'rotation', m_animFeature );
	}

	public export override function ResavePersistentData( ps : PersistentState ) : Bool
	{
		var fanData : FanResaveData;
		var psDevice : FanControllerPS;
		super.ResavePersistentData( ps );
		psDevice = ( ( FanControllerPS )( ps ) );
		fanData.m_animationType = m_animationType;
		fanData.m_rotateClockwise = m_rotateClockwise;
		fanData.m_randomizeBladesSpeed = m_randomizeBladesSpeed;
		fanData.m_maxRotationSpeed = m_maxRotationSpeed;
		fanData.m_timeToMaxRotation = m_timeToMaxRotation;
		psDevice.PushResaveData( fanData );
		return true;
	}

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

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

	protected override function CutPower()
	{
		StopFan();
	}

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

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

	private function StartFan()
	{
		if( GetDevicePS().GetAnimationType() == EAnimationType.REGULAR )
		{
			PlayRegularAnimation();
		}
		else if( GetDevicePS().GetAnimationType() == EAnimationType.TRANSFORM )
		{
			PLayTransformAnimation();
		}
	}

	private function StopFan()
	{
		if( GetDevicePS().GetAnimationType() == EAnimationType.REGULAR )
		{
			StopRegularAnimation();
		}
		else if( GetDevicePS().GetAnimationType() == EAnimationType.TRANSFORM )
		{
			StopTransformAnimation();
		}
	}

	private function PLayTransformAnimation()
	{
		var playEvent : gameTransformAnimationPlayEvent;
		playEvent = new gameTransformAnimationPlayEvent;
		playEvent.animationName = 'SPIN';
		playEvent.looping = true;
		playEvent.timesPlayed = 1;
		if( !( GetDevicePS().IsRotatingClockwise() ) )
		{
			playEvent.timeScale = -1.0;
		}
		else
		{
			playEvent.timeScale = 1.0;
		}
		QueueEvent( playEvent );
	}

	private function StopTransformAnimation()
	{
		var stopEvent : gameTransformAnimationPauseEvent;
		stopEvent = new gameTransformAnimationPauseEvent;
		stopEvent.animationName = 'SPIN';
		QueueEvent( stopEvent );
	}

	private function PlayRegularAnimation()
	{
		m_animFeature.rotateClockwise = GetDevicePS().IsRotatingClockwise();
		m_animFeature.maxRotationSpeed = GetDevicePS().GetMaxRotationSpeed();
		m_animFeature.timeToMaxRotation = GetDevicePS().GetTimeToMaxRotation();
		m_animFeature.randomizeBladesRotation = GetDevicePS().IsBladesSpeedRandomized();
		AnimationControllerComponent.ApplyFeature( this, 'rotation', m_animFeature );
	}

	private function StopRegularAnimation()
	{
		m_animFeature.rotateClockwise = false;
		m_animFeature.maxRotationSpeed = 0.0;
		m_animFeature.timeToMaxRotation = 0.0;
		m_animFeature.randomizeBladesRotation = false;
		AnimationControllerComponent.ApplyFeature( this, 'rotation', m_animFeature );
	}

	protected event OnAreaEnter( evt : AreaEnteredEvent )
	{
		var TDBid : TweakDBID;
		if( GetDevicePS().IsON() )
		{
			TDBid = T"Attacks.FanBlades";
			if( TDBID.IsValid( TDBid ) )
			{
				DoAttack( TDBid );
			}
		}
	}

	private function DoAttack( damageType : TweakDBID )
	{
		var attack : Attack_GameEffect;
		var hitFlags : array< SHitFlag >;
		var flag : SHitFlag;
		flag.flag = hitFlag.FriendlyFire;
		flag.source = 'FanDevice';
		hitFlags.PushBack( flag );
		attack = RPGManager.PrepareGameEffectAttack( GetGame(), this, this, damageType, , hitFlags );
		if( attack )
		{
			attack.StartAttack();
		}
	}

}

