using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ModelViewerInterface : MonoBehaviour
{
    public static ModelViewerInterface _mainInstance;

    public TMPro.TMP_Text[] TooltipLabels;

    [Header("Required")]
    public CanvasContainer CanvasContainer;

    private Vector2 _previousResolution = Vector2.zero;
    private int _resolutionUpdateCountdown;

    public Canvas MainCanvas => CanvasContainer.MainCanvas;
    public RectTransform Tooltip => CanvasContainer.Tooltip;
    public ProgressBar ProgressBar => CanvasContainer.ProgressBar;
    public Transform DynamicPanels => CanvasContainer.DynamicPanels;
    public UISaveLoadPanel SaveLoadPanel => CanvasContainer.SaveLoadPanel;
    public TMPro.TextMeshProUGUI TooltipText => CanvasContainer.TooltipText;
    public ScrollRect SelectedHandlesPanel => CanvasContainer.SelectedHandlesPanel;
    public UIToolbar SelectedObjectToolbar => CanvasContainer.SelectedObjectToolbar;

    public static ModelViewerInterface GetInstance()
    {
        return _mainInstance;
    }

    public static T GetInstance<T>() where T : ModelViewerInterface
    {
        return _mainInstance as T;
    }

    protected virtual void Awake()
    {
        _mainInstance = this;
    }

    protected void Update()
    {
        var resolution = new Vector2(Screen.width, Screen.height);
        if (resolution != _previousResolution)
        {
            _previousResolution = resolution;
            _resolutionUpdateCountdown = 10;
        }

        if (_resolutionUpdateCountdown > 0)
        {
            if (--_resolutionUpdateCountdown == 0)
            {
                var draggables = GameObject.FindObjectsOfType<UIElementDragger>();
                foreach (var draggable in draggables)
                {
                    draggable.transform.position = draggable.RecalculatePosition(draggable.transform.position);
                };
            }
        }

        Tooltip.position = Input.mousePosition;
        if (string.IsNullOrEmpty(TooltipText.text) && Tooltip.gameObject.activeSelf)
        {
            Tooltip.gameObject.SetActive(false);
        }
        else if (!string.IsNullOrEmpty(TooltipText.text) && !Tooltip.gameObject.activeSelf)
        {
            Tooltip.gameObject.SetActive(true);
        }
    }

    public static void SetTooltip(int index, string tooltip)
    {
        var tooltips = GetInstance().TooltipLabels;
        if(tooltips.Length > index)
        {
            tooltips[index].SetText(tooltip);
        }
    }

    public static void ToggleVisible(GameObject go)
    {
        ToggleVisible(go, false);
    }

    public static void ToggleVisible(GameObject go, bool force = false, bool forceValue = false)
    {
        if (force)
        {
            go.SetActive(forceValue);
        }
        else
        {
            go.SetActive(!go.activeSelf);
        }
    }

    public static void SetDropdownData(TMPro.TMP_Dropdown dd, List<string> values, bool nullValue = false, bool sortValues = false)
    {
        dd.ClearOptions();
        if (nullValue)
            dd.AddOptions(new List<string>() { Strings.NoValueSelectedString });
        if (values != null)
        {
            if (sortValues)
            {
                values = new List<string>(values);
                values.Sort();
            }
            dd.AddOptions(values);
        }
    }

    public static void DropdownPrevious(TMPro.TMP_Dropdown dropdown)
    {
        Error.Log(Color.red, "Remove this");
        dropdown.value = dropdown.value - 1 < 0 ? dropdown.options.Count - 1 : dropdown.value - 1;
    }

    public static void DropdownNext(TMPro.TMP_Dropdown dropdown)
    {
        Error.Log(Color.red, "Remove this");
        dropdown.value = dropdown.value + 1 >= dropdown.options.Count ? 0 : dropdown.value + 1;
    }
}