using UnityEngine;
using UnityEditor;
using System;

public class SymbolVisualizerInspector : ShaderGUI {

    public enum ColorMode{
        Static,
        Pulse,
        Changing
    }

    MaterialProperty particleSize;
    MaterialProperty animPart;
    MaterialProperty animSpeed;
    MaterialProperty colorMode;
    MaterialProperty dstBlend;
    MaterialProperty color1;
    MaterialProperty color2;
    MaterialProperty color3;
    MaterialProperty changeSpeed;

    public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] properties){
        Material material = materialEditor.target as Material;
        particleSize = FindProperty("_Size", properties);
        animPart = FindProperty("_AnimPart", properties);
        animSpeed = FindProperty("_AnimSpeed", properties);
        dstBlend = FindProperty("_DstBlend", properties);
        colorMode = FindProperty("_ColorMode", properties);
        color1 = FindProperty("_Color1", properties);
        color2 = FindProperty("_Color2", properties);
        color3 = FindProperty("_Color3", properties);
        changeSpeed = FindProperty("_ChangeSpeed", properties);

        /* ---------- BASE SETUP ---------- */

        materialEditor.ShaderProperty(animSpeed, "Animation Speed");
        materialEditor.ShaderProperty(animPart, "Animation Length");
        materialEditor.ShaderProperty(particleSize, "Particle Size");

        /* ---------- GLOW ---------- */
        bool glowActive = material.GetFloat("_DstBlend") == (int)UnityEngine.Rendering.BlendMode.One;
        EditorGUI.BeginChangeCheck();
        glowActive = EditorGUILayout.Toggle("Glow", glowActive);
        if (EditorGUI.EndChangeCheck()){
            if (glowActive)
            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);
            else
            material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
        }

        /* ---------- COLOR MODE ---------- */
        ColorMode cMode = (ColorMode)colorMode.floatValue;

        EditorGUI.BeginChangeCheck();
        cMode = (ColorMode)EditorGUILayout.Popup("Coloring Mode", (int)cMode, Enum.GetNames(typeof(ColorMode)));
        if (EditorGUI.EndChangeCheck()){
            materialEditor.RegisterPropertyChangeUndo("Coloring Mode");
            colorMode.floatValue = (float)cMode;

            foreach (var obj in colorMode.targets){
                setupMaterialColorMode((Material)obj);
            }
        }
        switch(cMode){
            case ColorMode.Static:
                materialEditor.ShaderProperty(color1, "Color");
                break;
            case ColorMode.Pulse:
                materialEditor.ShaderProperty(color1, "Color 1");
                materialEditor.ShaderProperty(color2, "Color 2");
                materialEditor.ShaderProperty(changeSpeed, "Change Speed");
                break;
            case ColorMode.Changing:
                materialEditor.ShaderProperty(color1, "Color 1");
                materialEditor.ShaderProperty(color2, "Color 2");
                materialEditor.ShaderProperty(color3, "Color 3");
                materialEditor.ShaderProperty(changeSpeed, "Change Speed");
                break;
            default: break;//nothing
        }
    }

    public static void setupMaterialColorMode(Material material){
        ColorMode mode = (ColorMode)material.GetFloat("_ColorMode");
        if(mode == ColorMode.Static) material.EnableKeyword("CM_STATIC");
        else material.DisableKeyword("CM_STATIC");
        if(mode == ColorMode.Pulse) material.EnableKeyword("CM_PULSE");
        else material.DisableKeyword("CM_PULSE");
        if(mode == ColorMode.Changing) material.EnableKeyword("CM_CHANGING");
        else material.DisableKeyword("CM_CHANGING");
    }
}
