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

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

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

    private MaterialProperty _fs = null;
    private MaterialProperty _fe = null;
    private MaterialProperty RM = null;
    private MaterialProperty _Color = null;
    private MaterialProperty _pColor = null;
    private MaterialProperty _Rainbow = null;
    private MaterialProperty _Direction = null;
    private MaterialProperty _Lines = null;
    private MaterialProperty _Speed = null;
    private MaterialProperty _tOffset = null;
    private MaterialProperty _Scale = null;
    private MaterialProperty _rot = null;
    private MaterialProperty _offs = 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");

        Header("Renderer Settings");

        EditorGUILayout.BeginVertical(GUI.skin.box);
        editor.ShaderProperty(_fs, MakeLabel(_fs));
        editor.ShaderProperty(_fe, MakeLabel(_fe));
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical(GUI.skin.box);
        editor.ShaderProperty(RM, MakeLabel(RM));
        editor.ShaderProperty(_Direction, MakeLabel(_Direction));
        EditorGUILayout.EndVertical();

        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical("GroupBox");

        Header("Optical Stream Settings");

        EditorGUILayout.BeginVertical(GUI.skin.box);
        editor.ShaderProperty(_Rainbow, MakeLabel(_Rainbow));
        editor.ShaderProperty(_Color, MakeLabel(_Color));
        editor.ShaderProperty(_pColor, MakeLabel(_pColor));
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical(GUI.skin.box);
        editor.ShaderProperty(_Speed, MakeLabel(_Speed));
        editor.ShaderProperty(_tOffset, MakeLabel(_tOffset));
        editor.ShaderProperty(_Lines, MakeLabel(_Lines));
        editor.ShaderProperty(_Scale, MakeLabel(_Scale));
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical(GUI.skin.box);
        editor.VectorProperty(GUILayoutUtility.GetRect(0, int.MaxValue, 16, 16), _rot, _rot.displayName);
        editor.VectorProperty(GUILayoutUtility.GetRect(0, int.MaxValue, 16, 16), _offs, _offs.displayName);
        EditorGUILayout.EndVertical();

        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 };
        var ButtonTextStyle = new GUIStyle { font = VrchatFont, fontSize = 15, fontStyle = FontStyle.Italic, alignment = TextAnchor.MiddleLeft };
        ButtonTextStyle.normal.textColor = Color.white;
        GUILayout.Label("Original shader by:", TextStyle);
        GUILayout.Space(2);
        GUILayout.Label("notargs", TextStyle);
        GUILayout.Space(2);
        var rect = GUILayoutUtility.GetRect(0, int.MaxValue, 15, 17);
        EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f));
        if (GUI.Button(rect, "Open Original Shader", ButtonTextStyle))
        {
            Application.OpenURL("https://www.shadertoy.com/view/4lGcz1");
        }
        GUILayout.Space(6);
        GUILayout.Label("Edited 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