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


namespace X2Engine
{
    [ImageEffectAllowedInSceneView]
    [ExecuteInEditMode]
    [DisallowMultipleComponent]
    public class AtomsphereImage : MonoBehaviour
    {

        public enum DebugOption
        {
            None,
            LightOnly,
            Transmittance
        };
        public DebugOption _DebugOption = DebugOption.None;
        public bool optimizedPrecompute = true;

        public Shader m_AtmosphericCameraShader;
        private Material m_AtmosphericCameraMaterial;
        private Camera cam;
        private bool shouldRender = true;

        static readonly int sp_MainTex = Shader.PropertyToID("_MainTex");

        protected bool CheckSupport()
        {
            // support ImageEffects and support depth?
            if (!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Depth))
            {
                return false;
            }
            else
                return true;
        }

        void CheckMaterial()
        {
            if (m_AtmosphericCameraMaterial == null || m_AtmosphericCameraMaterial.shader != m_AtmosphericCameraShader)
            {
                m_AtmosphericCameraMaterial = new Material(m_AtmosphericCameraShader);
                m_AtmosphericCameraMaterial.hideFlags = HideFlags.DontSave;
            }
        }

        void OnEnable()
        {
            CheckMaterial();
            cam = CameraCache.main;
         
#if SCENE_EDITOR && UNITY_EDITOR
            cam.depthTextureMode |= DepthTextureMode.Depth; // needed this?
#else
            cam.depthTextureMode = DepthTextureMode.None; // needed this?
#endif

            optimizedPrecompute = true;
        }

        void Start()
        {
            shouldRender = CheckSupport();
        }

        [ImageEffectOpaque]
        void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (!shouldRender)
            {
                Graphics.Blit(source, destination);
                return;
            }

            switch (_DebugOption)
            {
                case DebugOption.LightOnly:
                    m_AtmosphericCameraMaterial.DisableKeyword("_DEBUG_TRANSMITTANCE");
                    m_AtmosphericCameraMaterial.EnableKeyword("_DEBUG_LIGHTONLY");
                    break;
                case DebugOption.Transmittance:
                    m_AtmosphericCameraMaterial.DisableKeyword("_DEBUG_LIGHTONLY");
                    m_AtmosphericCameraMaterial.EnableKeyword("_DEBUG_TRANSMITTANCE");
                    break;
                default:
                    m_AtmosphericCameraMaterial.DisableKeyword("_DEBUG_LIGHTONLY");
                    m_AtmosphericCameraMaterial.DisableKeyword("_DEBUG_TRANSMITTANCE");
                    break;
            }

            FrustumCornersGraphicsBlit(source, destination, m_AtmosphericCameraMaterial, 0);

        }

        static void FrustumCornersGraphicsBlit(RenderTexture source, RenderTexture dest, Material fxMaterial, int passNr)
        {
            RenderTexture.active = dest;

            fxMaterial.SetTexture(sp_MainTex, source);

            GL.PushMatrix();
            GL.LoadOrtho();

            fxMaterial.SetPass(passNr);

            GL.Begin(GL.QUADS);

            GL.MultiTexCoord2(0, 0.0f, 0.0f);
            GL.Vertex3(0.0f, 0.0f, 3.0f); // BL

            GL.MultiTexCoord2(0, 1.0f, 0.0f);
            GL.Vertex3(1.0f, 0.0f, 2.0f); // BR

            GL.MultiTexCoord2(0, 1.0f, 1.0f);
            GL.Vertex3(1.0f, 1.0f, 1.0f); // TR

            GL.MultiTexCoord2(0, 0.0f, 1.0f);
            GL.Vertex3(0.0f, 1.0f, 0.0f); // TL

            GL.End();
            GL.PopMatrix();

        }
    }

}
