using Godot;
using System;
using System.Diagnostics;
using Cirno.Scripts;

public partial class Door : Activable
{
	// protected AnimatedSprite2D _animatedSprite;
	//protected CollisionShape2D _collisionShape;
	//protected CollisionShape2D _solidShape;

	protected AudioStreamPlayer2D _activationSound;
    protected AudioStreamPlayer2D _deactivationSound;
	// Called when the node enters the scene tree for the first time.
	
	[Export]
	public DoorState State { get; set; }
	
	[Signal]
	public delegate void DoorOpenedEventHandler();

	[Signal]
	public delegate void DoorClosedEventHandler();
	
	[Signal]
	public delegate void DestroyedEventHandler();

	public override void _Ready()
	{
		// _animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
		//_collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
		//_solidShape = GetNode<CollisionShape2D>("RigidBody2D/CollisionShape2D");
		
		_activationSound = GetNodeOrNull<AudioStreamPlayer2D>("ActivationSound");
        _deactivationSound = GetNodeOrNull<AudioStreamPlayer2D>("DeactivationSound");

		SetState(State);
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	public virtual void Open()
	{
		// _animatedSprite.Play("Opening");
		State = DoorState.Open;
		CallDeferred(MethodName.DeferredDisableCollision, true);
		_deactivationSound?.Play();
		EmitSignal(SignalName.DoorOpened);
		//_collisionShape.Disabled = true;
		//_solidShape.Disabled = true;
	}

	public virtual void Close()
	{
		// _animatedSprite.Play("Closing");
		State = DoorState.Closed;
		CallDeferred(MethodName.DeferredDisableCollision, false);
		_activationSound?.Play();
		EmitSignal(SignalName.DoorClosed);
		//_collisionShape.Disabled = false;
		//_solidShape.Disabled = false;
	}

	public void Destroy()
	{
		// _animatedSprite.Play("Destroyed");
		
		State = DoorState.Destroyed;
		CallDeferred(MethodName.DeferredDisableCollision, true);
		EmitSignal(SignalName.Destroyed);
	}

	private void DeferredDisableCollision(bool state)
	{
		//_collisionShape.Disabled = state;
		//_solidShape.Disabled = state;
	}
	
	public override bool Activate(ActivationType activationType = ActivationType.Toggle)
	{
		switch (activationType)
		{
			case ActivationType.Toggle:
				ToggleDoor();
				break;
			case ActivationType.Enable:
				Close();
				break;
			case ActivationType.Disable:
				Open();
				break;
			case ActivationType.Use:
				ToggleDoor();
				break;
			case ActivationType.Destroy:
				Destroy();
				break;
			case ActivationType.Open:
				Open();
				break;
			case ActivationType.Close:
				Close();
				break;
			default:
				ToggleDoor();
				break;
		}

		return true;
	}

	private void ToggleDoor()
	{
		switch (State)
		{
			case DoorState.Closed:
				Open();
				break;
			case DoorState.Open:
				Close();
				break;
			default:
				throw new ArgumentOutOfRangeException();
		}
	}

	private void SetState(DoorState state)
	{
		switch (state)
		{
			case DoorState.Closed:
				Close();
				break;
			case DoorState.Open:
				Open();
				break;
			case DoorState.Destroyed:
				Destroy();
				break;
			default:
				throw new ArgumentOutOfRangeException();
		}
	}

	// public void _on_animated_sprite_2d_animation_changed()
	// {
	// 	switch (_animatedSprite.Animation)
	// 	{
	// 		case "Opening":
	// 			
	// 			break;
	// 		case "Closing":
	// 			
	// 			break;
	// 		default:
	// 			break;
	// 	}
	// }
}

public enum DoorState
{
	Closed,
	Open,
	Destroyed
}
