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

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

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

    private MaterialProperty _Color;
    private MaterialProperty _glow;
    private MaterialProperty _scale;
    private MaterialProperty _dspow;
    private MaterialProperty _groffs;
    private MaterialProperty _grsh;
    private MaterialProperty _noiscount;
    private MaterialProperty _rt;
    private MaterialProperty GSBG;

    private MaterialProperty _ap;
    private MaterialProperty _nspeed;
    private MaterialProperty _wspeed;
    private MaterialProperty _wpow;
    private MaterialProperty _refraction;
    private MaterialProperty _ZWrite;

    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");

        Header("Main Settings");

        editor.ShaderProperty(_Color, MakeLabel(_Color));
        editor.ShaderProperty(_glow, MakeLabel(_glow));
        editor.ShaderProperty(_ap, MakeLabel(_ap));
        editor.ShaderProperty(_dspow, MakeLabel(_dspow)); 
        editor.ShaderProperty(_refraction, MakeLabel(_refraction));
        editor.ShaderProperty(_rt, MakeLabel(_rt));
        editor.ShaderProperty(GSBG, MakeLabel(GSBG));
        editor.ShaderProperty(_ZWrite, MakeLabel(_ZWrite));

        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical("GroupBox");

        Header("Noise Settings");

        editor.ShaderProperty(_scale, MakeLabel(_scale));
        editor.ShaderProperty(_groffs, MakeLabel(_groffs));
        editor.ShaderProperty(_grsh, MakeLabel(_grsh));
        editor.ShaderProperty(_noiscount, MakeLabel(_noiscount));
        editor.ShaderProperty(_nspeed, MakeLabel(_nspeed));
        editor.ShaderProperty(_wspeed, MakeLabel(_wspeed));
        editor.ShaderProperty(_wpow, MakeLabel(_wpow));

        EditorGUILayout.EndVertical();

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

        DrawCredits();
    }

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

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

        EditorGUILayout.BeginVertical("GroupBox");
        var rect = GUILayoutUtility.GetRect(0, int.MaxValue, 35, 35);
        EditorGUI.DrawRect(rect, new Color(0f, 0f, 0f));
        EditorGUI.LabelField(rect, text2, rateTxt);
        EditorGUI.LabelField(rect, text1, title);

        if (GUI.Button(rect, "", 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 DrawCredits()
    {
        EditorGUILayout.BeginVertical("GroupBox");

        var 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();
    }

    private void Header(string name)
    {
        var Style = new GUIStyle { font = VrchatFont, fontSize = 18, fontStyle = FontStyle.Italic, alignment = TextAnchor.MiddleLeft };
        GUILayout.Label(name, Style);
        GUILayout.Space(5);
    }
}
#endif