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

namespace Onward.CustomContent.EditorTools
{
    public class OnwardShaderReplace : EditorWindow
    {
        Shader[] m_shaderAssets;
        string[] m_shaderNames;

        Shader m_srcShader;
        Shader m_dstShader;
        int m_shaderIndex = 0;

        [MenuItem("ONWARD/Shader replace")]
        static void Init()
        {
            OnwardShaderReplace window = (OnwardShaderReplace)EditorWindow.GetWindow(typeof(OnwardShaderReplace), true, "Shader replace", true);

            window.Show();
        }

        void OnGUI()
        {
            GUILayout.Label("Allows you to replace all scene materials using a specific shader to another shader.", EditorStyles.boldLabel);

            GUILayout.Space(15);

            if (GUILayout.Button("Scan scene for shaders"))
            {
                Scan();

            }

            GUILayout.Space(25);
            if (m_shaderAssets == null || m_shaderAssets.Length <= 0)
            {
                return;
            }

            GUILayout.Label($"Source: ");
            m_shaderIndex = EditorGUILayout.Popup(m_shaderIndex, m_shaderNames);

            try
            {
                m_srcShader = m_shaderAssets[m_shaderIndex];
            }
            catch
            {
                return;
            }

            if (m_srcShader == null)
            {
                return;
            }

            GUILayout.Space(15);
            GUILayout.Label($"Destination: ");
            m_dstShader = (Shader) EditorGUILayout.ObjectField(m_dstShader, typeof(Shader), false);

            if (m_dstShader == null)
            {
                return;
            }

            GUILayout.Space(15);

            if (GUILayout.Button($"Replace\n\n'{m_srcShader.name}'\nwith\n'{m_dstShader.name}'"))
            {
                Renderer[] rs = GameObject.FindObjectsOfType<Renderer>();

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

                        if (m.shader.name != m_srcShader.name)
                        {
                            continue;
                        }

                        m.shader = m_dstShader;

                        EditorUtility.SetDirty(m);
                    }
                }

                Scan();
            }
        }

        void Scan()
        {
            HashSet<Shader> shaders = new HashSet<Shader>();

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

                    shaders.Add(m.shader);
                }
            }

            m_shaderNames = new string[shaders.Count];
            m_shaderAssets = new Shader[shaders.Count];

            int i = 0;

            foreach (var s in shaders)
            {
                m_shaderNames[i] = s.name;
                m_shaderAssets[i] = s;

                ++i;
            }
        }

        List<Renderer> GetAllRenderers()
        {
            List<Renderer> renderers = new List<Renderer>();

            foreach (var go in UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects())
            {
                renderers.AddRange(go.GetComponentsInChildren<Renderer>(true));
            }

            return renderers;
        }
    }
}
#endif