using System;
using Godot;

namespace Cirno.Scripts.Interactables;

public partial class Valve : Interactable
{
    [Export] public Node2D Target { get; set; }

    [Export]
    private DoorState _activationState = DoorState.Closed;

    private AnimatedSprite2D _animatedSprite;
    
    public override void _Ready()
    {
        _animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
        
        SetState(_activationState);
    }
    
    public override bool Activate(ActivationType activationType = ActivationType.Toggle)
    {
        if (MeetsRequirements() && Target is IActivable activatable)
        {
            switch (_activationState)
            {
                case DoorState.Closed:
                    activatable.Activate(ActivationType.Enable);
                    Open();
                    break;
                case DoorState.Open:
                    activatable.Activate(ActivationType.Disable);
                    Close();
                    break;
                default:
                    return false;
            }
            
            return true;
        }

        return false;
    }
    
    public void Open()
    {
        _animatedSprite.Play("Open");
        _activationState = DoorState.Open;
        
    }

    public void Close()
    {
        _animatedSprite.Play("Closed");
        _activationState = DoorState.Closed;
		
    }
    
    private void SetState(DoorState state)
    {
        switch (state)
        {
            case DoorState.Closed:
                Close();
                break;
            case DoorState.Open:
                Open();
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
}