﻿#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace Onward.CustomContent.EditorTools
{
    public class ScanForNonPPShaders : EditorWindow
    {
        [MenuItem("ONWARD/Find materials incompatible with Onward")]
        public static void ShowWorkshopWindow()
        {
            ScanForNonPPShaders onwardDashboard = GetWindow<ScanForNonPPShaders>(false, "Find materials incompatible with Onward", true);
        }

        HashSet<Material> m_materials = new HashSet<Material>();

        public class Occurence
        {
            public Material material;
            public Renderer renderer;
            public bool potentiallyCompatible = false;
        }

        List<Occurence> m_occurences = new List<Occurence>();

        private void OnGUI()
        {
            if (GUILayout.Button("Scan"))
            {
                m_materials.Clear();
                m_occurences.Clear();

                foreach (var go in UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().GetRootGameObjects())
                {
                    foreach (var r in go.GetComponentsInChildren<Renderer>(true))
                    {
                        if (r == null)
                        {
                            continue;
                        }

                        foreach (var m in r.sharedMaterials)
                        {
                            if (m == null)
                            {
                                continue;
                            }

                            if (m.name == "M_Internal" || m.name == "M_OnwardCustomContent_Spawnpoint")
                            {
                                continue;
                            }

                            if (m.shader.name.Contains("Shader Graphs/PP_"))
                            {
                                continue;
                            }

                            bool potentiallyCompatible = false;

                            try
                            {
                                // Scan shader source for GUIDs used by PreProcessing
                                // If we find them we assume it's a ShaderGraph making use of the PreProcessing features and should thus be compatible
                                //
                                string content = System.IO.File.ReadAllText(AssetDatabase.GetAssetPath(m.shader));

                                string PPGUID = "906eff91f52b69846b5522d23074530b";
                                string PPLiteGUID = "30c964ff6f8c29d49800b117941637c3";

                                if (content.Contains(PPGUID) || content.Contains(PPLiteGUID))
                                {
                                    potentiallyCompatible = true;
                                }
                            }
                            catch
                            {
                            }

                            if (m_materials.Add(m))
                            {
                                m_occurences.Add(new Occurence()
                                {
                                    material = m,
                                    renderer = r,
                                    potentiallyCompatible = potentiallyCompatible
                                });
                            }
                        }
                    }
                }
            }

            if (m_materials == null || m_materials.Count <= 0)
            {
                return;
            }

            GUILayout.Space(25);

            foreach (var o in m_occurences)
            {
                EditorGUILayout.BeginHorizontal();
                {
                    Color oldColor = GUI.color;

                    GUI.color = Color.green;
                    EditorGUILayout.LabelField(o.potentiallyCompatible ? "Potentially compatible" : "", GUILayout.Width(175));
                    GUI.color = oldColor;

                    EditorGUILayout.LabelField(o.material.shader.name);
                    EditorGUILayout.ObjectField(o.material, typeof(Material), true);

                    if (GUILayout.Button("Select material", GUILayout.Width(100)))
                    {
                        UnityEditor.Selection.activeObject = o.material;
                    }

                    if (GUILayout.Button("Select renderer", GUILayout.Width(100)))
                    {
                        UnityEditor.Selection.activeObject = o.renderer;
                    }
                }

                EditorGUILayout.EndHorizontal();
            }
        }
    }
}
#endif