using Godot;
using System;
using System.Collections.Generic;
using Cirno.Scripts.Interactables;

public partial class Selector : Node2D
{
    private Interactable _lastInteractable;

    private List<IInteractable> _interactables = [];

    private int _selectedInteractable;

    private int SelectedInteractableIndex
    {
        get => _selectedInteractable;
        set
        {
            if (value >= _interactables.Count)
            {
                _selectedInteractable = 0;
            }

            if (value < 0)
            {
                _selectedInteractable = _interactables.Count - 1;
            }
        }
    }

    //[Signal]
    //public delegate void SelectedItemInteractableChangedEventHandler(IInteractable interactable);

    public IInteractable SelectedInteractable
    {
        get =>
            _interactables.Count > 0
                ? SelectedInteractableIndex >= _interactables.Count
                    ? _interactables[^1]
                    : _interactables[SelectedInteractableIndex]
                : null;
        set
        {
            // Passing null deselects the interactable
            if (value == null)
            {
                _selectedInteractable = -1;
                NotifyChanged(null);
                return;
            }

            // If it's already in the list, set the current one to it, otherwise add it
            if (!_interactables.Contains(value))
            {
                AddInteractable(value);
            }

            SelectedInteractableIndex = _interactables.IndexOf(value);
            NotifyChanged(value);
        }
    }

    // public override void _Process(double delta)
    // {
    //     if (Input.IsActionJustPressed("scan"))
    //     {
    //         SelectNext();
    //     }
    //
    //     // if (SelectedInteractable is not null) {
    //     //     this.Visible = true;
    //     //     this.GlobalPosition = SelectedInteractable.GlobalPosition;
    //     // }
    //     // else
    //     // {
    //     //     this.Visible = false;
    //     // }
    // }

    public void SelectNext()
    {
        SelectedInteractableIndex += 1;
        // _selectedInteractable += 1;
        // if (_selectedInteractable >= _interactables.Count)
        // {
        //     _selectedInteractable = 0;
        // }

        if (_interactables.Count > 0)
        {
            SelectedInteractable = _interactables[_selectedInteractable];
        }
        else
        {
            _selectedInteractable = -1;
        }

        UpdatePosition();
    }

    private void NotifyChanged(IInteractable interactable)
    {
        //EmitSignal(nameof(SelectedItemInteractableChanged), interactable);
    }

    public void AddInteractable(IInteractable interactable)
    {
        if (!_interactables.Contains(interactable))
        {
            _interactables.Add(interactable);

            if (_interactables.Count == 1)
            {
                _selectedInteractable = 0;
            }
        }

        UpdatePosition();
    }

    public void RemoveInteractable(IInteractable interactable)
    {
        if (_interactables.Contains(interactable))
        {
            _interactables.Remove(interactable);
        }

        UpdatePosition();
    }

    public void Deselect()
    {
        _selectedInteractable = -1;
        this.Visible = false;
    }

    public void UpdatePosition()
    {
        if (SelectedInteractable != null)
        {
            this.GlobalPosition = SelectedInteractable.GetGlobalPosition();
            this.Visible = true;
        }
        else
        {
            this.Visible = false;
        }
    }
}