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

public class ModelViewerInterface : MonoBehaviour
{
    public static ModelViewerInterface _mainInstance;

    public Transform DynamicPanels;
    public TMPro.TMP_Text[] TooltipLabels;

    public ScrollRect ScenePresetToggle;
    public ScrollRect PosePresetToggle;
    public ScrollRect SelectedHandlesPanel;

    public UISaveLoadPanel SaveLoadPanel;
    public ProgressBar ProgressBar;

    public Canvas MainCanvas;

    public RectTransform Tooltip;
    public TMPro.TextMeshProUGUI TooltipText;

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

    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);
                };
            } 
        }
    }

    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;
    }
}