﻿#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.Linq;
using System.Reflection;

public class MetallicFX600GUI : ShaderGUI
{
    private Font VrchatFont = (Font)Resources.Load(@"BankGothic");

    private BindingFlags bindingFlags = BindingFlags.Public |
                                    BindingFlags.NonPublic |
                                    BindingFlags.Instance |
                                    BindingFlags.Static;

    private MaterialProperty _MainTex = null;
    private MaterialProperty _Color = null;
    private MaterialProperty _EmissionMap = null;
    private MaterialProperty _EmissionColor = null;
    private MaterialProperty _BumpMap = null;
    private MaterialProperty _BumpScale = null;
    private MaterialProperty _MetallicTexture = null;
    private MaterialProperty _MetallicMask = null;
    private MaterialProperty _MetMul = null;
    private MaterialProperty _MetAdd = null;
    private MaterialProperty _MetEmission = null;
    private MaterialProperty _mColor1 = null;
    private MaterialProperty _mColor2 = null;
    private MaterialProperty _mColor3 = null;
    private MaterialProperty _Contrast = null;
    private MaterialProperty _RadialDistortion = null;
    private MaterialProperty _ShineSpeed = null;

    private MaterialProperty _Cull = null;
    private MaterialProperty _Stencil = null;
    private MaterialProperty _ReadMask = null;
    private MaterialProperty _WriteMask = null;
    private MaterialProperty _StencilComp = null;
    private MaterialProperty _StencilOp = null;
    private MaterialProperty _StencilFail = null;
    private MaterialProperty _StencilZFail = null;

    public override void OnGUI(MaterialEditor editor, MaterialProperty[] properties)
    {
        Material material = editor.target as Material;
        Shader shader = material.shader;

        //Thanks Xiexe
        foreach (var property in GetType().GetFields(bindingFlags))
        {
            if (property.FieldType == typeof(MaterialProperty))
            {
                try { property.SetValue(this, FindProperty(property.Name, properties)); } catch { }
            }
        }
        //

        DrawBanner(shader.name.Split('/').Last(), "Open Discord", "https://discord.gg/5PHerbf");

        EditorGUILayout.BeginVertical("GroupBox");

        MakeFoldout(material, "_MainGUI", "Main Settings");
        if (material.GetInt("_MainGUI") == 1)
        {
            DrawTexture(editor, _MainTex, _Color);
            DrawTexture(editor, _EmissionMap, _EmissionColor);
            DrawTexture(editor, _BumpMap, _BumpScale);
            //editor.ShaderProperty(_Specular, MakeLabel(_Specular));
        }

        MakeFoldout(material, "_MetallicGUI", "MetallicFX");
        if (material.GetInt("_MetallicGUI") == 1)
        {
            EditorGUILayout.BeginVertical(GUI.skin.box);
            editor.ShaderProperty(_MetEmission, MakeLabel(_MetEmission));
            editor.ShaderProperty(_MetMul, MakeLabel(_MetMul));
            editor.ShaderProperty(_MetAdd, MakeLabel(_MetAdd));
            EditorGUILayout.EndVertical();
            DrawTexture(editor, _MetallicMask);
            DrawTexture(editor, _MetallicTexture);
            EditorGUILayout.BeginVertical(GUI.skin.box);
            editor.ShaderProperty(_mColor1, MakeLabel(_mColor1));
            editor.ShaderProperty(_mColor2, MakeLabel(_mColor2));
            editor.ShaderProperty(_mColor3, MakeLabel(_mColor3));
            editor.ShaderProperty(_Contrast, MakeLabel(_Contrast));
            EditorGUILayout.EndVertical();
            EditorGUILayout.BeginVertical(GUI.skin.box);
            editor.ShaderProperty(_RadialDistortion, MakeLabel(_RadialDistortion));
            editor.ShaderProperty(_ShineSpeed, MakeLabel(_ShineSpeed));
            EditorGUILayout.EndVertical();
        }

        MakeFoldout(material, "_RendererGUI", "Renderer Settings");
        if (material.GetInt("_RendererGUI") == 1)
        {
            EditorGUILayout.BeginVertical(GUI.skin.box);
            editor.ShaderProperty(_Cull, MakeLabel(_Cull));
            EditorGUILayout.EndVertical();

            EditorGUILayout.BeginVertical(GUI.skin.box);
            editor.ShaderProperty(_Stencil, MakeLabel(_Stencil));
            editor.ShaderProperty(_ReadMask, MakeLabel(_ReadMask));
            editor.ShaderProperty(_WriteMask, MakeLabel(_WriteMask));
            editor.ShaderProperty(_StencilComp, MakeLabel(_StencilComp));
            editor.ShaderProperty(_StencilOp, MakeLabel(_StencilOp));
            editor.ShaderProperty(_StencilFail , MakeLabel(_StencilFail ));
            editor.ShaderProperty(_StencilZFail, MakeLabel(_StencilZFail));
            EditorGUILayout.EndVertical();
        }
        
        EditorGUILayout.EndVertical();

        DrawBanner("Patreon", "Open Patreon", "https://www.patreon.com/dopestuff");

        DrawCredits();
    }

    private void DrawTexture(MaterialEditor editor, MaterialProperty tex, MaterialProperty prop = null)
    {
        EditorGUILayout.BeginVertical(GUI.skin.box);
        GUILayout.Space(3);
        EditorGUILayout.BeginHorizontal();
        GUILayout.Space(2);
        editor.TexturePropertySingleLine(MakeLabel(tex), tex, prop);
        EditorGUILayout.EndHorizontal();
        editor.TextureScaleOffsetProperty(tex);
        EditorGUILayout.EndVertical();
    }

    private void DrawBanner(string MainText, string OtherText, string URL)
    {
        GUIStyle OtherStyle = new GUIStyle { font = VrchatFont, alignment = TextAnchor.LowerRight, fontSize = 12, padding = new RectOffset(0, 1, 0, 1) };
        OtherStyle.normal.textColor = new Color(0.9f, 0.9f, 0.9f);

        GUIStyle MainStyle = new GUIStyle { font = VrchatFont, alignment = TextAnchor.MiddleCenter, fontSize = 18, padding = new RectOffset(0, 1, 0, 1) };
        MainStyle.normal.textColor = new Color(1f, 1f, 1f);

        EditorGUILayout.BeginVertical("GroupBox");
        Rect BannerRect = GUILayoutUtility.GetRect(0, int.MaxValue, 35, 35);
        EditorGUI.DrawRect(BannerRect, new Color(0f, 0f, 0f));
        EditorGUI.LabelField(BannerRect, OtherText, OtherStyle);
        EditorGUI.LabelField(BannerRect, MainText, MainStyle);

        if (GUI.Button(BannerRect, string.Empty, new GUIStyle()))
        {
            Application.OpenURL(URL);
        }

        EditorGUILayout.EndVertical();
    }

    private static GUIContent MakeLabel(MaterialProperty property, string tooltip = null)
    {
        GUIContent staticLabel = new GUIContent { text = property.displayName, tooltip = tooltip };
        return staticLabel;
    }

    private void MakeFoldout(Material material, string foldout, string name) //Foldout without toggle
    {
        EditorGUILayout.BeginHorizontal("Button");
        GUILayout.Space(10);
        material.SetInt(foldout, EditorGUILayout.Foldout(material.GetInt(foldout) > 0, name, true) ? 1 : 0);
        EditorGUILayout.EndHorizontal();
    }

    private void DrawCredits()
    {
        EditorGUILayout.BeginVertical("GroupBox");
        GUIStyle TextStyle = new GUIStyle { font = VrchatFont, fontSize = 15, fontStyle = FontStyle.Italic };
        GUILayout.Label("Shader by:", TextStyle);
        GUILayout.Space(2);
        GUILayout.Label("Doppelgänger#8376", TextStyle);
        GUILayout.Space(6);
        GUILayout.Label("Thanks for help with editor:", TextStyle);
        GUILayout.Space(2);
        GUILayout.Label("Bkp#8336", TextStyle);
        EditorGUILayout.EndVertical();
    }
}
#endif