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

public class PostProcessV1Behavior : MonoBehaviour
{
    public Shader BriShader;
    private Material briMaterial;
    public Material BriMaterial
    {
        get
        {
            briMaterial = checkShaderAndCreateMaterial(BriShader, briMaterial);
            return briMaterial;
        }
    }

    private Material checkShaderAndCreateMaterial(Shader s, Material m)
    {
        if (null == s)
            return null;
        if (null != m && m.shader == s)
            return m;

        m = new Material(s);
        m.hideFlags = HideFlags.DontSave;
        return m;
    }

    [Range(0.0f, 3.0f)]
    public float Brightness = 2.0f;
    [Range(0.0f, 3.0f)]
    public float Saturation = 2.0f;
    [Range(0.0f, 3.0f)]
    public float Contrast = 2.0f;


    Camera mainCamera;
    [HideInInspector]
    public RenderTexture targetRT;
    RenderTexture tmpCamRT;
    RenderTexture tmpCamDepthRT;
    Coroutine cleanupRTCoroutine;

    private void Start()
    {
        


    }
    private void OnEnable()
    {
        if (null == mainCamera)
            mainCamera = Camera.main;
        // mainCamera.forceIntoRenderTexture = true;
    }
    private void OnDisable()
    {
        ReleaseCamRT();
    }
    private void OnPreCull()
    {
        
    }

    private void OnPreRender()
    {
        SetupCamRT();
    }

    private void OnPostRender()
    {
        // for (int i = 0; i < 100; i++)
        {
            if (BriMaterial != null)
            {
                BriMaterial.SetFloat("_Brightness", Brightness);
                BriMaterial.SetFloat("_Saturation", Saturation);
                BriMaterial.SetFloat("_Contrast", Contrast);

                Graphics.Blit(tmpCamRT, null as RenderTexture, BriMaterial);
            }
            else
                Graphics.Blit(tmpCamRT, null as RenderTexture);

            mainCamera.targetTexture = null;
        }
          

        if (cleanupRTCoroutine != null)
            StopCoroutine(cleanupRTCoroutine);
        cleanupRTCoroutine = StartCoroutine(CheckReleaseCamRT());
    }


    public void SetupCamRT()
    {
        var width = targetRT != null ? targetRT.width : Screen.width;
        var height = targetRT != null ? targetRT.height : Screen.height;
        if (tmpCamRT == null || tmpCamRT.width != width || tmpCamRT.height != height)
        {
            if (tmpCamRT != null)
            {
                RenderTexture.ReleaseTemporary(tmpCamRT);
                tmpCamRT = null;
            }
            if (tmpCamDepthRT != null)
            {
                RenderTexture.ReleaseTemporary(tmpCamDepthRT);
                tmpCamDepthRT = null;
            }
            // var needDepth = IsDepthOfFieldEnabled();
            if (tmpCamRT == null)
            {
                tmpCamRT = RenderTexture.GetTemporary(width, height, 0);
                tmpCamRT.name = "PostFxCamRT";
                tmpCamRT.hideFlags = HideFlags.DontSave;
                tmpCamDepthRT = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.Depth);
                tmpCamDepthRT.name = "PostFxCamDepthRT";
                tmpCamDepthRT.hideFlags = HideFlags.DontSave;
            }
        }
        tmpCamRT.DiscardContents();
        tmpCamDepthRT.DiscardContents();
        mainCamera.SetTargetBuffers(tmpCamRT.colorBuffer, tmpCamDepthRT.depthBuffer);
    }

    void ReleaseCamRT()
    {
        if (tmpCamRT != null)
        {
            RenderTexture.ReleaseTemporary(tmpCamRT);
        }
        tmpCamRT = null;
        if (tmpCamDepthRT != null)
        {
            RenderTexture.ReleaseTemporary(tmpCamDepthRT);
        }
        tmpCamDepthRT = null;
    }

    IEnumerator CheckReleaseCamRT()
    {
        yield return new WaitForEndOfFrame();
        StopCoroutine(cleanupRTCoroutine);
        cleanupRTCoroutine = null;
        ReleaseCamRT();
    }


}
