using UnityEngine;
using UnityEditor;
using System;

public class SimulatorInspector : ShaderGUI {

    MaterialProperty dataTex;
    MaterialProperty falloff;
    MaterialProperty distOff;
    MaterialProperty attraction;
    MaterialProperty maxAttraction;
    MaterialProperty maxSpeed;
    MaterialProperty SpeedLoss;
    MaterialProperty noisePos;
    MaterialProperty attractorTex;

    public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] properties){
        Material material = materialEditor.target as Material;
        dataTex = FindProperty("_DataTex", properties);
        attraction = FindProperty("_Attraction", properties);
        falloff = FindProperty("_Falloff", properties);
        distOff = FindProperty("_DistOff", properties);
        maxAttraction = FindProperty("_MaxAttraction", properties);
        maxSpeed = FindProperty("_MaxSpeed", properties);
        SpeedLoss = FindProperty("_SpeedLoss", properties);
        noisePos = FindProperty("_NoisePos", properties);
        attractorTex = FindProperty("_AttractorTex", properties);

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

        materialEditor.ShaderProperty(dataTex, "Data Texture");
        materialEditor.ShaderProperty(attraction, "Attraction");
        materialEditor.ShaderProperty(falloff, "Attraction Falloff");
        materialEditor.ShaderProperty(distOff, "Distance Offset");
        materialEditor.ShaderProperty(maxAttraction, "Max Attraction");
        materialEditor.ShaderProperty(maxSpeed, "Max Speed");
        materialEditor.ShaderProperty(SpeedLoss, "Speed Loss");

        /* ---------- POSITION NOISE ---------- */
        bool noiseActive = material.GetFloat("_NoisePos") > 0;
        EditorGUI.BeginChangeCheck();
        noiseActive = EditorGUILayout.Toggle("Noisy Position", noiseActive);
        if (EditorGUI.EndChangeCheck()){
            if (noiseActive){
                material.EnableKeyword("NOISY_POS");
                material.SetFloat("_NoisePos", 0.00001f);
            }else{
                material.DisableKeyword("NOISY_POS");
                material.SetFloat("_NoisePos", 0.0f);
            }
        }
        if(noiseActive) materialEditor.ShaderProperty(noisePos, "Noise Strength");

        bool shapedAttractActive = material.GetFloat("_ShapedAttract") == 1.0f;
        EditorGUI.BeginChangeCheck();
        shapedAttractActive = EditorGUILayout.Toggle("Shaped Attraction", shapedAttractActive);
        if (EditorGUI.EndChangeCheck()){
            if (shapedAttractActive){
                material.SetFloat("_ShapedAttract", 1.0f);
                material.EnableKeyword("SHAPED_ATTRACT");
            }else{
                material.SetFloat("_ShapedAttract", 0.0f);
                material.DisableKeyword("SHAPED_ATTRACT");
            }
        }
        if(shapedAttractActive) materialEditor.ShaderProperty(attractorTex, "Attraction Shape Texture");
    }
}
