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

[CustomEditor(typeof(FastPostProcess))]
public class FastPostProcessInspector : Editor
{

    SerializedProperty m_mode;

    SerializedProperty m_camera;
    SerializedProperty m_isGlobal;

    SerializedProperty m_name;
    SerializedProperty m_NumberOfPasses;
    
    SerializedProperty m_Blur;
    SerializedProperty m_BlurAmount;
    SerializedProperty m_BlurMask;

    SerializedProperty m_Bloom;
    SerializedProperty m_BloomColor;
    SerializedProperty m_BloomAmount;
    SerializedProperty m_BloomDiffuse;
    SerializedProperty m_BloomThreshold;

    SerializedProperty m_Fxaa;
    SerializedProperty m_FxaaThresholdMin;
    SerializedProperty m_FxaaThreshold;
    SerializedProperty m_FxaaSharpness;

    SerializedProperty m_MotionBlur;

    SerializedProperty m_LUT;
    SerializedProperty m_LutDimension;
    SerializedProperty m_LutAmount;
    SerializedProperty m_SourceLut;

    SerializedProperty m_LiftGammaGain;
    SerializedProperty m_LiftGammaGain_Lift;
    SerializedProperty m_LiftGammaGain_Gamma;
    SerializedProperty m_LiftGammaGain_Gain;

    SerializedProperty m_Tonemap;

    SerializedProperty m_ImageFiltering;
    SerializedProperty m_Color;
    SerializedProperty m_Contrast;
    SerializedProperty m_Brightness;
    SerializedProperty m_Saturation;
    SerializedProperty m_Exposure;
    SerializedProperty m_Gamma;
    SerializedProperty m_Sharpness;

    SerializedProperty m_ChromaticAberration;
    SerializedProperty m_Offset;
    SerializedProperty m_FishEyeDistortion;
    SerializedProperty m_GlitchAmount;

    SerializedProperty m_Distortion;
    SerializedProperty m_LensDistortion;

    SerializedProperty m_Vignette;
    SerializedProperty m_VignetteColor;
    SerializedProperty m_VignetteAmount;
    SerializedProperty m_VignetteSoftness;

    SerializedProperty m_bSunShaft;
    SerializedProperty m_SunShaft_Material;
    SerializedProperty m_SunShaft_SunDir;
    SerializedProperty m_SunShaft_Color;

    public void showProperty(string propertyName)
    {
        var property = serializedObject.FindProperty(propertyName);
        if (property != null)
        {
            EditorGUILayout.PropertyField(serializedObject.FindProperty(propertyName));
        }
    }

    private void OnEnable()
    {
        m_mode = serializedObject.FindProperty("m_mode");

        m_camera = serializedObject.FindProperty("m_camera");
        m_isGlobal = serializedObject.FindProperty("m_isGlobal");

        m_name = serializedObject.FindProperty("m_name");
        m_NumberOfPasses = serializedObject.FindProperty("settings.NumberOfPasses");

        m_Blur = serializedObject.FindProperty("settings.Blur");
        m_BlurAmount = serializedObject.FindProperty("settings.BlurAmount");
        m_BlurMask = serializedObject.FindProperty("settings.BlurMask");

        m_Bloom = serializedObject.FindProperty("settings.Bloom");
        m_BloomColor = serializedObject.FindProperty("settings.BloomColor");
        m_BloomAmount = serializedObject.FindProperty("settings.BloomAmount");
        m_BloomDiffuse = serializedObject.FindProperty("settings.BloomDiffuse");
        m_BloomThreshold = serializedObject.FindProperty("settings.BloomThreshold");

        m_Fxaa = serializedObject.FindProperty("settings.Fxaa");
        m_FxaaThresholdMin = serializedObject.FindProperty("settings.FxaaThresholdMin");
        m_FxaaThreshold = serializedObject.FindProperty("settings.FxaaThreshold");
        m_FxaaSharpness = serializedObject.FindProperty("settings.FxaaSharpness");

        m_MotionBlur = serializedObject.FindProperty("settings.MotionBlur");

        m_LUT = serializedObject.FindProperty("settings.LUT");
        m_LutDimension = serializedObject.FindProperty("settings.LutDimension");
        m_LutAmount = serializedObject.FindProperty("settings.LutAmount");
        m_SourceLut = serializedObject.FindProperty("settings.SourceLut");

        m_LiftGammaGain = serializedObject.FindProperty("settings.LiftGammaGainSwitch");
        m_LiftGammaGain_Lift = serializedObject.FindProperty("settings.LiftGammaGain_Lift");
        m_LiftGammaGain_Gamma = serializedObject.FindProperty("settings.LiftGammaGain_Gamma");
        m_LiftGammaGain_Gain = serializedObject.FindProperty("settings.LiftGammaGain_Gain");

        m_Tonemap = serializedObject.FindProperty("settings.Tonemap");

        m_ImageFiltering = serializedObject.FindProperty("settings.ImageFiltering");
        m_Color = serializedObject.FindProperty("settings.Color");
        m_Contrast = serializedObject.FindProperty("settings.Contrast");
        m_Brightness = serializedObject.FindProperty("settings.Brightness");
        m_Saturation = serializedObject.FindProperty("settings.Saturation");
        m_Exposure = serializedObject.FindProperty("settings.Exposure");
        m_Gamma = serializedObject.FindProperty("settings.Gamma");
        m_Sharpness = serializedObject.FindProperty("settings.Sharpness");

        m_ChromaticAberration = serializedObject.FindProperty("settings.ChromaticAberration");
        m_Offset = serializedObject.FindProperty("settings.Offset");
        m_FishEyeDistortion = serializedObject.FindProperty("settings.FishEyeDistortion");
        m_GlitchAmount = serializedObject.FindProperty("settings.GlitchAmount");

        m_Distortion = serializedObject.FindProperty("settings.Distortion");
        m_LensDistortion = serializedObject.FindProperty("settings.LensDistortion");

        m_Vignette = serializedObject.FindProperty("settings.Vignette");
        m_VignetteColor = serializedObject.FindProperty("settings.VignetteColor");
        m_VignetteAmount = serializedObject.FindProperty("settings.VignetteAmount");
        m_VignetteSoftness = serializedObject.FindProperty("settings.VignetteSoftness");

        m_bSunShaft = serializedObject.FindProperty("settings.bSunshaft");
        m_SunShaft_Material = serializedObject.FindProperty("settings.SunShaft_Material");
        m_SunShaft_SunDir = serializedObject.FindProperty("settings.SunShaft_SunDir");
        m_SunShaft_Color = serializedObject.FindProperty("settings.SunShaft_Color");

        lutfilesname = null;
        m_TrackballUIDrawer = new TrackballUIDrawer();

        blur_toggle = m_Blur.boolValue;
        bloom_toggle = m_Bloom.boolValue;
        fxaa_toggle = m_Fxaa.boolValue;
        lut_toggle = m_LUT.boolValue;
        lgg_toggle = m_LiftGammaGain.boolValue;
        imagefiltering_toggle = m_ImageFiltering.boolValue;
        chromatic_toggle = m_ChromaticAberration.boolValue;
        distortion_toggle = m_Distortion.boolValue;
        vignete_toggle = m_Vignette.boolValue;
    }




    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        using(new EditorGUI.DisabledScope(true))
        {
            EditorGUILayout.PropertyField(m_name, new GUIContent("name: "));
            m_name.stringValue = target.name + "_" + target.GetInstanceID();
        }
        EditorGUILayout.PropertyField(m_isGlobal);
       
        GUILayout.Space(5);
        EditorGUILayout.PropertyField(m_mode);
        if (m_mode.enumValueIndex == (int)FastPostProcess.EMode.EM_Camera)
        {
            EditorGUILayout.PropertyField(m_camera);
        }

        GUILayout.BeginVertical();

        GUILayout.Space(5);
        EditorGUILayout.PropertyField(m_NumberOfPasses);
        

        OnInspector_Blur();
        OnInspector_Bloom();
        OnInspector_Fxaa();
        OnInspector_MotionBlur();
        OnInspector_LUT();
        OnInspector_LiftGammaGain();
        OnInspector_Tonemap();
        OnInspector_ImageFiltering();
        OnInspector_ChromaticAberration();
        OnInspector_Distortion();
        OnInspector_Vignette();
        OnInspector_Sunshaft();


        GUILayout.EndVertical();

        serializedObject.ApplyModifiedProperties();
    }

    bool blur_toggle = false;
    public void OnInspector_Blur()
    {
        ExtendEditor.DrawSplitter();
        blur_toggle = ExtendEditor.DrawHeaderToggle("Blur", m_Blur, blur_toggle);
        if (!blur_toggle) return;
        using (new EditorGUI.DisabledScope(!m_Blur.boolValue))
        {
            EditorGUILayout.PropertyField(m_BlurAmount);
            EditorGUILayout.PropertyField(m_BlurMask);
        }

    }
    bool bloom_toggle = false;
    public void OnInspector_Bloom()
    {
        ExtendEditor.DrawSplitter();
        bloom_toggle = ExtendEditor.DrawHeaderToggle("Bloom", m_Bloom, bloom_toggle);
        if (!bloom_toggle) return;
        using (new EditorGUI.DisabledScope(!m_Bloom.boolValue))
        {
            EditorGUILayout.PropertyField(m_BloomColor);
            EditorGUILayout.PropertyField(m_BloomAmount);
            EditorGUILayout.PropertyField(m_BloomDiffuse);
            EditorGUILayout.PropertyField(m_BloomThreshold);
            showProperty("settings.BloomSoftKnee");
            showProperty("settings.BloomClamp");
            showProperty("settings.BloomScatter");
        }

    }

    bool fxaa_toggle = false;
    public void OnInspector_Fxaa()
    {
        ExtendEditor.DrawSplitter();
        fxaa_toggle = ExtendEditor.DrawHeaderToggle("Fxaa", m_Fxaa, fxaa_toggle);
        if (!fxaa_toggle) return;
        using (new EditorGUI.DisabledScope(!m_Fxaa.boolValue))
        {
            EditorGUILayout.PropertyField(m_FxaaThresholdMin);
            EditorGUILayout.PropertyField(m_FxaaThreshold);
            EditorGUILayout.PropertyField(m_FxaaSharpness);
        }
    }

    bool motionBlur_toggle = false;
    public void OnInspector_MotionBlur()
    {
        ExtendEditor.DrawSplitter();
        motionBlur_toggle = ExtendEditor.DrawHeaderToggle("Motion Blur", m_MotionBlur, motionBlur_toggle);
        if (!motionBlur_toggle) return;
        using (new EditorGUI.DisabledScope(!m_MotionBlur.boolValue))
        {
            showProperty("settings.MotionBlurIntensity");
            showProperty("settings.MotionBlurClamp");
            showProperty("settings.ViewMask");
            showProperty("settings.MaskCentre");
            showProperty("settings.MaskScale");
            showProperty("settings.MaskGradient");
        }
    }

    static string lut_path = "Assets/Plugins/MobilePostProcess/LUTS/";
    static string[] lutfilespath;
    static string[] lutfilesname;

    public void InitLutFiles()
    {
        if (lutfilesname == null)
        {
            var path = Application.dataPath.Replace("Assets", lut_path);
            lutfilespath = System.IO.Directory.GetFiles(path, "*.png", System.IO.SearchOption.AllDirectories);
            lutfilesname = new string[lutfilespath.Length];
            for (int i = 0; i < lutfilespath.Length; i++)
            {
                //var name = System.IO.Path.GetFileName(lutfilespath[i]);
                var name = lutfilespath[i].Replace(path, "").Replace(".png", "");

                lutfilesname[i] = name;
            }
        }
    }
    public Texture2D LoadLUTTexture(int index)
    {
        var path = lutfilespath[index].Replace(Application.dataPath, "Assets");

        return AssetDatabase.LoadAssetAtPath<Texture2D>(path);
    }
    public int FindLUTIndex(string name)
    {
        for (int i = 0; i < lutfilesname.Length; i++)
        {
            if (lutfilesname[i].EndsWith(name))
                return i;
        }
        return -1;
    }

    bool lut_toggle = false;
    public void OnInspector_LUT()
    {
        ExtendEditor.DrawSplitter();
        lut_toggle = ExtendEditor.DrawHeaderToggle("LUT", m_LUT, lut_toggle);
        if (!lut_toggle) return;
        using (new EditorGUI.DisabledScope(!m_LUT.boolValue))
        {
            EditorGUILayout.PropertyField(m_LutDimension);
            EditorGUILayout.PropertyField(m_LutAmount);

            EditorGUI.BeginChangeCheck();
            InitLutFiles();
            var index = m_SourceLut.objectReferenceValue ? FindLUTIndex(m_SourceLut.objectReferenceValue.name) : -1;
            index = EditorGUILayout.Popup("Source LUT File ", index, lutfilesname);
            if (EditorGUI.EndChangeCheck())
            {
                var tex = LoadLUTTexture(index);
                m_SourceLut.objectReferenceValue = tex;
            }

            EditorGUILayout.PropertyField(m_SourceLut);
            if (m_SourceLut.objectReferenceValue == null && m_LUT.boolValue)
            {
                GUILayout.Label("必须设置SourceLut！！！！", "ErrorLable");
            }
        }

    }


    static TrackballUIDrawer m_TrackballUIDrawer;//= new TrackballUIDrawer();
    static Vector3 GetLiftValue(Vector4 x) => new Vector3(x.x + x.w, x.y + x.w, x.z + x.w);

    bool lgg_toggle = false;
    public void OnInspector_LiftGammaGain()
    {
        ExtendEditor.DrawSplitter();
        lgg_toggle = ExtendEditor.DrawHeaderToggle("LiftGammaGain", m_LiftGammaGain, lgg_toggle);
        if (!lgg_toggle) return;
        using (new EditorGUI.DisabledScope(!m_LiftGammaGain.boolValue))
        {
            //Lift-Gamma-Gain
            using (new EditorGUILayout.HorizontalScope())
            {
                m_TrackballUIDrawer.OnGUI(m_LiftGammaGain_Lift, EditorGUIUtility.TrTextContent("Lift"), GetLiftValue);
                GUILayout.Space(4f);
                m_TrackballUIDrawer.OnGUI(m_LiftGammaGain_Gamma, EditorGUIUtility.TrTextContent("Gamma"), GetLiftValue);
                GUILayout.Space(4f);
                m_TrackballUIDrawer.OnGUI(m_LiftGammaGain_Gain, EditorGUIUtility.TrTextContent("Gain"), GetLiftValue);
            }
        }
    }

    bool tonemap_toggle = false;
    public void OnInspector_Tonemap()
    {
        ExtendEditor.DrawSplitter();
        tonemap_toggle = ExtendEditor.DrawHeaderToggle("Tonemapping", m_Tonemap, tonemap_toggle);
        if (!tonemap_toggle) return;
        using (new EditorGUI.DisabledScope(!m_Tonemap.boolValue))
        {
            showProperty("settings.TonemapExposure");
        }
    }

    bool imagefiltering_toggle = false;
    public void OnInspector_ImageFiltering()
    {
        ExtendEditor.DrawSplitter();
        imagefiltering_toggle = ExtendEditor.DrawHeaderToggle("Image Filtering", m_ImageFiltering, imagefiltering_toggle);
        if (!imagefiltering_toggle) return;
        using (new EditorGUI.DisabledScope(!m_ImageFiltering.boolValue))
        {
            EditorGUILayout.PropertyField(m_Color);
            EditorGUILayout.PropertyField(m_Contrast);
            EditorGUILayout.PropertyField(m_Brightness);
            EditorGUILayout.PropertyField(m_Saturation);
            EditorGUILayout.PropertyField(m_Exposure);
            EditorGUILayout.PropertyField(m_Gamma);
            EditorGUILayout.PropertyField(m_Sharpness);
        }
    }
    bool chromatic_toggle = false;
    public void OnInspector_ChromaticAberration()
    {
        ExtendEditor.DrawSplitter();
        chromatic_toggle = ExtendEditor.DrawHeaderToggle("Chromatic Aberration", m_ChromaticAberration, chromatic_toggle);
        if (!chromatic_toggle) return;
        using (new EditorGUI.DisabledScope(!m_ChromaticAberration.boolValue))
        {
            EditorGUILayout.PropertyField(m_Offset);
            EditorGUILayout.PropertyField(m_FishEyeDistortion);
            EditorGUILayout.PropertyField(m_GlitchAmount);
        }

    }

    bool distortion_toggle = false;
    public void OnInspector_Distortion()
    {
        ExtendEditor.DrawSplitter();
        distortion_toggle = ExtendEditor.DrawHeaderToggle("Distortion", m_Distortion, distortion_toggle);
        if (!distortion_toggle) return;
        using (new EditorGUI.DisabledScope(!m_Distortion.boolValue))
        {
            EditorGUILayout.PropertyField(m_LensDistortion);
        }

    }

    bool vignete_toggle = false;
    public void OnInspector_Vignette()
    {
        ExtendEditor.DrawSplitter();
        vignete_toggle = ExtendEditor.DrawHeaderToggle("Vignette", m_Vignette, vignete_toggle);
        if (!vignete_toggle) return;
        using (new EditorGUI.DisabledScope(!m_Vignette.boolValue))
        {
            EditorGUILayout.PropertyField(m_VignetteColor);
            EditorGUILayout.PropertyField(m_VignetteAmount);
            EditorGUILayout.PropertyField(m_VignetteSoftness);
        }
    }

    bool sunshaft_toggle = false;
    public void OnInspector_Sunshaft()
    {
        ExtendEditor.DrawSplitter();
        sunshaft_toggle = ExtendEditor.DrawHeaderToggle("Sunshaft", m_bSunShaft, sunshaft_toggle);
        if (!sunshaft_toggle) return;
        using (new EditorGUI.DisabledScope(!m_bSunShaft.boolValue))
        {
            EditorGUILayout.PropertyField(m_SunShaft_Material);
            EditorGUILayout.PropertyField(m_SunShaft_Color);
            EditorGUILayout.PropertyField(m_SunShaft_SunDir);
        }
    }
}
