using UnityEngine;
using UnityEditor;
using System;

public class VisualizerInspector : ShaderGUI {

    public enum ColorMode{
        Static,
        Pulse,
        Changing,
        Direction,
        Velocity,
        Texture
    }

    MaterialProperty dataTex;
    MaterialProperty particleSize;
    MaterialProperty colorMode;
    MaterialProperty dstBlend;
    MaterialProperty color1;
    MaterialProperty color2;
    MaterialProperty color3;
    MaterialProperty changeSpeed;
    MaterialProperty maxSpeed;
    MaterialProperty colorTex;

    public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] properties){
        Material material = materialEditor.target as Material;
        dataTex = FindProperty("_DataTex", properties);
        particleSize = FindProperty("_Size", 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);
        maxSpeed = FindProperty("_MaxSpeed", properties);
        colorTex = FindProperty("_ColorTex", properties);

        /* ---------- INPUT SETUP ---------- */

        materialEditor.ShaderProperty(dataTex, "Data Texture");

        bool worldPosActive = material.GetFloat("_WorldPos") == 1.0f;
        EditorGUI.BeginChangeCheck();
        worldPosActive = EditorGUILayout.Toggle("World Position", worldPosActive);
        if (EditorGUI.EndChangeCheck()){
            if (worldPosActive){
                material.SetFloat("_WorldPos", 1.0f);
                material.EnableKeyword("WORLD_POS");
            }else{
                material.SetFloat("_WorldPos", 0.0f);
                material.DisableKeyword("WORLD_POS");
            }
        }


        /* ---------- 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);
        }

        materialEditor.ShaderProperty(particleSize, "Particle Size");

        /* ---------- 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;
            case ColorMode.Direction:
                materialEditor.ShaderProperty(color1, "X Axis");
                materialEditor.ShaderProperty(color2, "Y Axis");
                materialEditor.ShaderProperty(color3, "Z Axis");
                break;
            case ColorMode.Velocity:
                materialEditor.ShaderProperty(color1, "Slow");
                materialEditor.ShaderProperty(color2, "Medium");
                materialEditor.ShaderProperty(color3, "Fast");
                materialEditor.ShaderProperty(maxSpeed, "Max Speed");
                break;
            case ColorMode.Texture:
                materialEditor.ShaderProperty(colorTex, "Colors");
                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");
        if(mode == ColorMode.Direction) material.EnableKeyword("CM_DIRECTION");
        else material.DisableKeyword("CM_DIRECTION");
        if(mode == ColorMode.Velocity) material.EnableKeyword("CM_VELOCITY");
        else material.DisableKeyword("CM_VELOCITY");
        if(mode == ColorMode.Texture) material.EnableKeyword("CM_TEXTURE");
        else material.DisableKeyword("CM_TEXTURE");
    }
}
