using UnityEditor;
using UnityEngine;
using System.Reflection;
using System.Collections.Generic;

namespace Mochie
{
    public class CorruptionEditor : ShaderGUI
    {
        static Dictionary<Material, Toggles> foldouts = new Dictionary<Material, Toggles>();
        Toggles toggles = new Toggles(new string[] {
            "Background",
            "Corruption",
        }, 1);

        string versionLabel = "v1.1";
        string headerText = "CORRUPTION";

        MaterialProperty _Color = null;
        MaterialProperty _StarBrightness = null;
        MaterialProperty _Rotation = null;
        MaterialProperty _RotationSpeed = null;
        MaterialProperty _StarTex = null;
        MaterialProperty _StarTexScale = null;
        MaterialProperty _StarIterations = null;
        MaterialProperty _TextureBlendMode = null;
        MaterialProperty _CubemapToggle = null;
        MaterialProperty _CubemapTex = null;
        MaterialProperty _CubemapRotation = null;
        
        MaterialProperty _RimColor = null;
        MaterialProperty _RimBrightness = null;
        MaterialProperty _RimWidth = null;
        MaterialProperty _Radius = null;
        MaterialProperty _SimplexScale = null;
        MaterialProperty _NoiseIterations = null;
        MaterialProperty _NoiseMode = null;
        MaterialProperty _NoiseSpeed = null;

        private static readonly int TaNoiseTex = Shader.PropertyToID("_TANoiseTex");
        
        BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
        MaterialEditor m_me;

        public override void OnGUI(MaterialEditor me, MaterialProperty[] props)
        {
            m_me = me;
            Material mat = (Material)me.target;

            // Find properties
            foreach (var property in GetType().GetFields(bindingFlags)){
                if (property.FieldType == typeof(MaterialProperty)){
                    property.SetValue(this, FindProperty(property.Name, props));
                }
            }

            if (!foldouts.ContainsKey(mat))
                foldouts.Add(mat, toggles);

            var tanoisePath = AssetDatabase.GUIDToAssetPath("c29b465ac9f3944508dfdf1555925ed5");
            var tanoiseTex = AssetDatabase.LoadAssetAtPath<Texture2D>(tanoisePath);
            mat.SetTexture(TaNoiseTex, tanoiseTex);
            
            MGUI.DoHeader(headerText);

            me.RenderQueueField();
            MGUI.Space4();

            if (Foldouts.DoFoldout(foldouts, mat, "Background", Foldouts.Style.Standard)) {
                MGUI.PropertyGroupParent(()=>{
                    MGUI.PropertyGroup(()=>{
                        if (_CubemapToggle.floatValue == 0){
                            me.TexturePropertySingleLine(new GUIContent("Color"), _StarTex, _CubemapToggle, _Color);
                            MGUI.TexPropLabel("Cubemap", 165, true);
                            me.ShaderProperty(_StarIterations, Tips.starSamplesText);
                            me.ShaderProperty(_TextureBlendMode, "Sample Blending");
                            MGUI.Vector2Field(_StarTexScale, "Scale");
                            me.ShaderProperty(_StarBrightness, "Brightness");
                            me.ShaderProperty(_RotationSpeed, "Rotation Speed");
                            me.ShaderProperty(_Rotation, "Rotation Offset");
                        }
                        else {
                            me.TexturePropertySingleLine(new GUIContent("Color"), _CubemapTex, _CubemapToggle, _Color);
                            MGUI.TexPropLabel("Cubemap", 165, true);
                            MGUI.Space2();
                            MGUI.Vector3Field(_CubemapRotation, "Rotation Speed", false);
                            me.ShaderProperty(_StarBrightness, "Brightness");
                        }
                    });
                });
            }

            if (Foldouts.DoFoldout(foldouts, mat, "Corruption", Foldouts.Style.Standard)) {
                MGUI.PropertyGroupParent(()=>{
                    MGUI.PropertyGroup(()=>{
                        me.ShaderProperty(_NoiseMode, Tips.noiseTypeText);
                        me.ShaderProperty(_NoiseIterations, Tips.noiseIterationsText);
                        me.ShaderProperty(_Radius, "Radius");
                        me.ShaderProperty(_SimplexScale, "Scale");
                        me.ShaderProperty(_NoiseSpeed, "Speed");
                    });
                    MGUI.PropertyGroup(()=>{
                        me.ShaderProperty(_RimColor, "Rim Color");
                        me.ShaderProperty(_RimBrightness, "Rim Brightness");
                        me.ShaderProperty(_RimWidth, "Rim Width");
                    });
                });
            }

            MGUI.SetKeyword(mat, "_SIMPLEX_ON", _NoiseMode.floatValue == 1);

            MGUI.DoFooter(versionLabel);
        }
    }
}