using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

[ExecuteInEditMode]
public class FastPostProcess : MonoBehaviour
{
    public enum EMode
    { 
        EM_Global,
        EM_Camera,
    }

    public EMode m_mode;

    public Camera m_camera;

    public bool m_isGlobal = true;

    static List<FastPostProcess> ms_fastPostProcessList = new List<FastPostProcess>();

    public static FastPostProcess GetSettings(Camera cam)
    {
        foreach (var ps in ms_fastPostProcessList)
        {
            return ps;
        }

        return null;
    }

    [System.Serializable]
    public class PostProcessSettings
    {
        public void CopyTo(FastPostProcess src)
        {
            NumberOfPasses = src.NumberOfPasses;

            Blur = src.Blur;
            BlurAmount = src.BlurAmount;
            BlurMask = src.BlurMask;

            Bloom = src.Bloom;
            BloomColor = src.BloomColor;
            BloomAmount = src.BloomAmount;
            BloomDiffuse = src.BloomDiffuse;
            BloomThreshold = src.BloomThreshold;

            Fxaa = src.Fxaa;
            FxaaThresholdMin = src.FxaaThresholdMin;
            FxaaThreshold = src.FxaaThreshold;
            FxaaSharpness = src.FxaaSharpness;

            LUT = src.LUT;
            LutDimension = src.LutDimension;
            LutAmount = src.LutAmount;
            SourceLut = src.SourceLut;

            LiftGammaGainSwitch = src.LiftGammaGainSwitch;
            LiftGammaGain_Lift = src.LiftGammaGain_Lift;
            LiftGammaGain_Gamma = src.LiftGammaGain_Gamma;
            LiftGammaGain_Gain = src.LiftGammaGain_Gain;

            ImageFiltering = src.ImageFiltering;
            Color = src.Color;
            Contrast = src.Contrast;
            Brightness = src.Brightness;
            Saturation = src.Saturation;
            Exposure = src.Exposure;
            Gamma = src.Gamma;
            Sharpness = src.Sharpness;

            ChromaticAberration = src.ChromaticAberration;
            Offset = src.Offset;
            FishEyeDistortion = src.FishEyeDistortion;
            GlitchAmount = src.GlitchAmount;

            Distortion = src.Distortion;
            LensDistortion = src.LensDistortion;

            Vignette = src.Vignette;
            VignetteColor = src.VignetteColor;
            VignetteAmount = src.VignetteAmount;
            VignetteSoftness = src.VignetteSoftness;

            bSunshaft = src.bSunshaft;
            SunShaft_Material = src.SunShaft_Material;
            SunShaft_SunDir = src.SunShaft_SunDir;
            SunShaft_Color = src.SunShaft_Color;
            SunShat_Timeline = src.SunShat_Timeline;

        }

        public Material material = null;

        public bool hasSet = false;

        public string name;

        [Range(1, 10)]
        public int NumberOfPasses = 3;
        public bool Blur = false;
        [Range(0, 1)]
        public float BlurAmount = 1f;
        public Texture2D BlurMask = null;
        public bool Bloom = false;
        public Color BloomColor = Color.white;
        [Range(0, 5)]
        public float BloomAmount = 1f;
        [Range(0, 3)]
        public float BloomDiffuse = 1f;
        [Range(0, 1)]
        public float BloomScatter = 1f;
        [Range(0, 2)]
        public float BloomThreshold = 0f;
        [Range(0, 1)]
        public float BloomSoftKnee = 0.5f;
        public float BloomClamp = 2f;

        public bool Fxaa = false;
        [Range(0, 0.1f)]
        public float FxaaThresholdMin = 0.06f;
        [Range(0, 0.25f)]
        public float FxaaThreshold = 0.125f;
        [Range(0, 8f)]
        public float FxaaSharpness = 8;

        public bool MotionBlur = false;
        [Range(0, 1f)]
        public float MotionBlurIntensity = 1.0f;
        [Range(0, 0.2f)]
        public float MotionBlurClamp = 0.2f;
        public bool ViewMask = false;
        public Vector2 MaskCentre = new Vector2(0.5f, 0.5f);
        public Vector2 MaskScale = new Vector2(1f, 1f);
        public float MaskGradient = 1.0f;

        public bool LUT = false;
        [Range(2, 3)]
        public int LutDimension = 2;
        [Range(0, 1)]
        public float LutAmount = 0.0f;
        public Texture2D SourceLut = null;


        public bool LiftGammaGainSwitch = false;
        public Vector4 LiftGammaGain_Lift = new Vector4(1, 1, 1, 1);
        public Vector4 LiftGammaGain_Gamma = new Vector4(1, 1, 1, 1);
        public Vector4 LiftGammaGain_Gain = new Vector4(1, 1, 1, 1);

        public bool Tonemap = false;
        [Range(0, 3)]
        public float TonemapExposure = 1.0f;
        public bool TonemapUnity = false;

        public bool ImageFiltering = false;
        public Color Color = Color.white;
        [Range(0, 1)]
        public float Contrast = 0f;
        [Range(-1, 1)]
        public float Brightness = 0f;
        [Range(-1, 1)]
        public float Saturation = 0f;
        [Range(-1, 1)]
        public float Exposure = 0f;
        [Range(-1, 1)]
        public float Gamma = 0f;
        [Range(0, 1)]
        public float Sharpness = 0f;

        public bool ChromaticAberration = false;
        public float Offset = 0;
        [Range(-1, 1)]
        public float FishEyeDistortion = 0;
        [Range(0, 1)]
        public float GlitchAmount = 0;

        public bool Distortion = false;
        [Range(0, 1)]
        public float LensDistortion = 0;

        public bool Vignette = false;
        public Color VignetteColor = Color.black;
        [Range(0, 1)]
        public float VignetteAmount = 0f;
        [Range(0.001f, 1)]
        public float VignetteSoftness = 0.001f;

        public bool bSunshaft = false;
        public Material SunShaft_Material = null;
        public Vector3 SunShaft_SunDir = Vector3.up;    // addComponent<SunshaftInterface>();
        public Color SunShaft_Color = Color.white;
        public int SunShat_Timeline = 0;
    }

    public PostProcessSettings settings = new PostProcessSettings();

    public string m_name;

    [Range(1, 5)]
    public int NumberOfPasses = 3;
    public bool Blur = false;
    [Range(0, 1)]
    public float BlurAmount = 1f;
    public Texture2D BlurMask = null;
    public bool Bloom = false;
    public Color BloomColor = Color.white;
    [Range(0, 5)]
    public float BloomAmount = 1f;
    [Range(0, 3)]
    public float BloomDiffuse = 1f;
    [Range(0, 1)]
    public float BloomThreshold = 0f;

    public bool Fxaa = false;
    [Range(0, 0.1f)]
    public float FxaaThresholdMin = 0.06f;
    [Range(0, 0.25f)]
    public float FxaaThreshold = 0.125f;
    [Range(0, 8f)]
    public float FxaaSharpness = 8;

    public bool LUT = false;
    [Range(2, 3)]
    public int LutDimension = 2;
    [Range(0, 1)]
    public float LutAmount = 0.0f;
    public Texture2D SourceLut = null;

    public bool LiftGammaGainSwitch = false;
    public Vector4 LiftGammaGain_Lift = new Vector4(1, 1, 1, 1);
    public Vector4 LiftGammaGain_Gamma = new Vector4(1, 1, 1, 1);
    public Vector4 LiftGammaGain_Gain = new Vector4(1, 1, 1, 1);


    public bool ImageFiltering = false;
    public Color Color = Color.white;
    [Range(0, 1)]
    public float Contrast = 0f;
    [Range(-1, 1)]
    public float Brightness = 0f;
    [Range(-1, 1)]
    public float Saturation = 0f;
    [Range(-1, 1)]
    public float Exposure = 0f;
    [Range(-1, 1)]
    public float Gamma = 0f;
    [Range(0, 1)]
    public float Sharpness = 0f;

    public bool ChromaticAberration = false;
    public float Offset = 0;
    [Range(-1, 1)]
    public float FishEyeDistortion = 0;
    [Range(0, 1)]
    public float GlitchAmount = 0;

    public bool Distortion = false;
    [Range(0, 1)]
    public float LensDistortion = 0;

    public bool Vignette = false;
    public Color VignetteColor = Color.black;
    [Range(0, 1)]
    public float VignetteAmount = 0f;
    [Range(0.001f, 1)]
    public float VignetteSoftness = 0.001f;

    public bool bSunshaft = false;
    public Material SunShaft_Material = null;
    [HideInInspector]
    public Vector3 SunShaft_SunDir = Vector3.up;    // addComponent<SunshaftInterface>();
    public Color SunShaft_Color = Color.white;
    public int SunShat_Timeline = 0;

    public bool isVaild(Camera cam)
    {
        if (m_mode != EMode.EM_Camera)
            return false;

        if (cam.cameraType == CameraType.SceneView && m_isGlobal)
            return true;

        return cam == m_camera;
    }

    private void OnEnable()
    {
        ms_fastPostProcessList.Add(this);
        if (settings.hasSet == false)
        {
            settings.CopyTo(this);
            settings.hasSet = true;
        }
    }

    private void OnDisable()
    {
        ms_fastPostProcessList.Remove(this);
    }

}
