﻿#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

public class BGGUI : ShaderGUI
{
    private Font VrchatFont = (Font)Resources.Load(@"BankGothic");
    private Texture2D bannerTex = Resources.Load<Texture2D>("bg");

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

    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;

        DrawBanner("Burning Glasses Shader", "Open Discord", "https://discord.gg/5PHerbf");

        FindProperties(properties);

        EditorGUILayout.BeginVertical("GroupBox");

        Header("Color 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(_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 static GUIContent MakeLabel(MaterialProperty property, string tooltip = null)
    {
        GUIContent staticLabel = new GUIContent();
        staticLabel.text = property.displayName;
        staticLabel.tooltip = tooltip;
        return staticLabel;
    }

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

        GUIStyle title = new GUIStyle(rateTxt);
        title.normal.textColor = new Color(1f, 1f, 1f);
        title.alignment = TextAnchor.MiddleCenter;
        title.fontSize = 18;

        EditorGUILayout.BeginVertical("GroupBox");
        var rect = GUILayoutUtility.GetRect(0, int.MaxValue, 35, 35);
        EditorGUI.DrawPreviewTexture(rect, bannerTex, null, ScaleMode.ScaleAndCrop);

        EditorGUI.LabelField(rect, text2, rateTxt);
        EditorGUI.LabelField(rect, text1, title);

        if (GUI.Button(rect, "", new GUIStyle()))
        {
            Application.OpenURL(URL);
        }

        EditorGUILayout.EndVertical();
    }

    private void FindProperties(MaterialProperty[] properties)
    {
        _Color = FindProperty("_Color", properties);
        _glow = FindProperty("_glow", properties);
        _scale = FindProperty("_scale", properties);
        _dspow = FindProperty("_dspow", properties);
        _groffs = FindProperty("_groffs", properties);
        _grsh = FindProperty("_grsh", properties);
        _noiscount = FindProperty("_noiscount", properties);
        _ap = FindProperty("_ap", properties);
        _nspeed = FindProperty("_nspeed", properties);
        _wspeed = FindProperty("_wspeed", properties);
        _wpow = FindProperty("_wpow", properties);
        _refraction = FindProperty("_refraction", properties);
        _ZWrite = FindProperty("_ZWrite", properties);
    }

    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