using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.EventSystems.EventTrigger;
using static UnityEngine.GraphicsBuffer;

public class KF3ModelViewerInterface : ModelViewerInterface
{
    public bool HandlesVisible = true;
    public static KF3ModelViewerInterface Instance => GetInstance<KF3ModelViewerInterface>();

    [Header("Current")]
    public Transform Timeline;
    public UIPopupMessage PopupMessagePrefab;
    public Transform HandlesPanel;

    public KF3.UI.Panels.SpawnPanel SpawnPanel;

    [Header("Obsolete")]
    public ScrollRect SelectionToggle;

    public GameObject LightPanel;

    public InputField WidthInput, HeightInput;

    private GUIStyle _style = new GUIStyle();

    private KF3ModelViewerMain Main => KF3ModelViewerMain.Instance;

    private IEnumerator Start()
    {
        _mainInstance = this;

        while(KF3ModelViewerMain.Instance == null || KF3AssetLibrary.Instance == null)
        {
            yield return 0;
        }

        _style.normal.textColor = Color.white;

        FindControls();
        StartCoroutine(SpawnPanel.LoadAll(Main));
        TooltipLabels[0].transform.parent.gameObject.SetActive(Settings.Instance.showHints);
    }

    private void OnGUI()
    {
        if (ModelViewerMain.GetInstance() == null) return;
        if (Settings.Instance.showHints)
        {
            int fontSize = Mathf.Clamp((int)(15.0f * ((float)Screen.height / (float)1080)), 10, int.MaxValue);
            _style.fontSize = fontSize;
            int n = 2;
            float height = Mathf.Clamp((int)(18.0f * ((float)Screen.height / (float)1080)), 12, int.MaxValue);
            GUI.Label(new Rect(10, n++ * height, 100, height), "E - open spawn menu", _style);
            switch (Main.GetCameraHandler().Settings.CameraMode)
            {
                case 0:
                case 1:
                    GUI.Label(new Rect(10, n++ * height, 100, height), "WASD - move camera (alt)", _style);
                    GUI.Label(new Rect(10, n++ * height, 100, height), "LMB + mouse move - move camera", _style);
                    GUI.Label(new Rect(10, n++ * height, 100, height), "RMB + mouse move - move sun", _style);
                    GUI.Label(new Rect(10, n++ * height, 100, height), "MMB + mouse up/down - camera target height", _style);
                    GUI.Label(new Rect(10, n++ * height, 100, height), "LMB + mouse scroll - camera distance", _style);
                    break;
                case 2:
                    GUI.Label(new Rect(10, n++ * height, 100, height), "LMB + WASD - move camera", _style);
                    GUI.Label(new Rect(10, n++ * height, 100, height), "RMB + WASD - pan camera", _style);
                    break;
            }
            if (Main.SelectedObject)
            {
                GUI.Label(new Rect(10, n++ * height, 100, height), "Arrow Keys - change animation speed", _style);
                GUI.Label(new Rect(10, n++ * height, 100, height), "Spacebar - pause/play animation", _style);
                GUI.Label(new Rect(10, n++ * height, 100, height), "Ctrl + LMB - move invisibly", _style);
            }
            GUI.Label(new Rect(10, n++ * height, 100, height), "H - hide this", _style);
        }
        if (Main.GetCameraHandler() == Main.WorkCameraOrbit)
        {
            var centeredStyle = GUI.skin.GetStyle("Label");
            centeredStyle.alignment = TextAnchor.UpperCenter;
            GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 25, 100, 50), "USING WORK CAMERA", centeredStyle);
        }
    }

    private void FixedUpdate()
    {
        if (KF3ModelViewerMain.Instance == null) return;
        if (Main.SelectedObject != null && Main.SelectedObject.GetDataType() == DataType.Character)
        {
            var simpleAnim = Main.GetSelectedObject<KF3ObjectContainer>().AsCharacter.BodySimpleAnimation;
            if (simpleAnim != null && simpleAnim.clip != null)
            {
                SimpleAnimation.State currentClip = simpleAnim.GetStates().FirstOrDefault(state => state.clip == simpleAnim.clip);
                //TimeScaleText.text = Strings.TimeScaleString + currentClip.speed.ToString("F2");
            }
        }
    }

    private void FindControls()
    {
        WidthInput.text = Screen.width.ToString();
        HeightInput.text = Screen.height.ToString();
    }

    public void OnSceneDelete()
    {
        Main.EmptyScene<KF3SceneContainer>();
    }

    public void SelectionAdd(Transform entry)
    {
        var go = Instantiate(Main.Resources.SelectionEntry, SelectionToggle.content.transform);
        go.SetTarget(entry);
    }

    public void SelectionRemove(Transform target)
    {
        var entry = SelectionToggle.content.transform.GetComponentsInChildren<UISelectionEntry>().FirstOrDefault(c => c.Target == target);
        if(entry != null)
        {
            Destroy(entry);
        }
    }

    public void SelectionClear()
    {
        if (SelectionToggle.content == null) return;
        for (int i = SelectionToggle.content.transform.childCount - 1; i >= 0; i--)
        {
            DestroyImmediate(SelectionToggle.content.transform.GetChild(i).gameObject);
        }
    }
}
