using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using static SerializableBone;

public class HandleManager : MonoBehaviour
{
    public static HandleManager Instance;
    public GameObject HandlesPanel;
    public GameObject Pfb_HandleDisplay;
    public UIPopupPanel Pfb_Popup;
    public Button Pfb_PopupButton;

    public Material LineRendererMaterial;

    public List<BoneTags> EnabledHandles = new List<BoneTags>() { BoneTags.Humanoid };
    public bool EnabledLines = true;

    public static bool InteractionInProgress;

    private List<UIHandle> AllHandles = new List<UIHandle>();

    public static System.Action<HandleUndoData> RegisterRuntimeGizmoUndoAction;
    public static System.Action<List<HandleUndoData>> RegisterRuntimeGizmoUndoActions;

    private void Awake()
    {
        Instance = this;
    }

    private void Update()
    {
        var camera = Camera.main;
        //var poseModeOn = UmaViewerUI.Instance.PoseManager.PoseModeOn;

        foreach (var handle in AllHandles)
        {
            //handle.ForceDisplayOff(!poseModeOn);
            handle.UpdateManual(camera, EnabledLines);

            if (handle.Popup.gameObject.activeInHierarchy)
            {
                handle.Popup.UpdateManual(camera);
            }
        }
    }

    public static void RegisterHandle(UIHandle handle)
    {
        var hm = HandleManager.Instance;
        
        if (hm.AllHandles.Contains(handle))
        {
            return;
        }

        hm.AllHandles.Add(handle);
    }

    public static void UnregisterHandle(UIHandle handle)
    {
        var hm = HandleManager.Instance;

        if (!hm.AllHandles.Contains(handle))
        {
            return;
        }

        hm.AllHandles.Remove(handle);
    }

    public static void CloseAllPopups()
    {
        var hm = HandleManager.Instance;

        foreach(var handle in hm.AllHandles)
        {
            if (handle.Popup.gameObject.activeInHierarchy)
            {
                handle.TogglePopup();
            }
        }
    }

    /// <summary> Used on UI buttons </summary>
    public void ToggleBonesVisible(string tag)
    {
        var enumTag = (BoneTags)System.Enum.Parse(typeof(BoneTags), tag);
        if (EnabledHandles.Contains(enumTag))
        {
            EnabledHandles.Remove(enumTag);
        }
        else
        {
            EnabledHandles.Add(enumTag);
        }
    }

    /// <summary> Used on UI buttons </summary>
    public void ToggleLinesVisible(bool value)
    {
        EnabledLines = value;
    }
}