#if UNITY_EDITOR
using System;
using System.IO;
using System.Reflection;
using UnityEditor;
#endif
using UnityEngine;

//I will be adding some useful stuff I use in my projects.
namespace ExtraTools
{
    public static class Utils
    {
        public static bool SetComponent<T>(this Transform t, out T component, bool needWarning = false)
        {
            component = t.GetComponent<T>();
            if (component == null && needWarning)
                Debug.LogWarning(string.Format("Couldn't find a component of type {0} on {1}!", typeof(T), t.name));

            return component != null;
        }

        public static void EnsureComponent<T>(this Transform t, out T component, bool needWarning = false) where T : Component
        {
            if (!t.SetComponent(out component))
                component = t.gameObject.AddComponent<T>();
        }

        public static bool FindOfType<T>(out T component, bool needWarning = false) where T : Component
        {
            component = UnityEngine.Object.FindObjectOfType<T>();
            if (!component && needWarning)
                Debug.LogWarning(string.Format("Couldn't find object of type {0} in the scene.", typeof(T)));
            return component;
        }

        #region Editor
#if UNITY_EDITOR
        /// <summary>
        /// Credit goes to KirillKuzyk from answers.unity.com.
        /// Got this from here: https://answers.unity.com/questions/707636/clear-console-window.html
        /// For more information about MenuItem see documentation: https://docs.unity3d.com/ScriptReference/MenuItem.html
        /// </summary>
        [MenuItem("Extra Tools/Clear Console %q")] // CTRL + Q
        private static void ClearConsole()
        {
            Assembly assembly = Assembly.GetAssembly(typeof(SceneView));
            Type type = assembly.GetType("UnityEditor.LogEntries");
            MethodInfo method = type.GetMethod("Clear");
            method.Invoke(new object(), null);
        }

        /// <summary>
        /// Credit goes to coeing from answers.unity.com
        /// Got this from here: https://answers.unity.com/questions/43422/how-to-implement-show-in-explorer.html
        /// </summary>
        [MenuItem("Extra Tools/Open Project Folder %e")] // CTRL + E
        public static void ShowExplorer()
        {
            string path = Directory.GetParent(Application.dataPath).FullName;
            EditorUtility.RevealInFinder(path);
        }
#endif
        #endregion
    }
}