using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.RenderGraphModule;

// Render Feature, das den Sichtkegel mit fovealem und peripherem Bereich darstellt und pro Kamera aktiviert werden kann.
public class GazeOverlayRendererFeature : ScriptableRendererFeature
{
    public sealed class GazeRenderPass : ScriptableRenderPass
    {
        internal Vector3 gazeOrigin;
        internal Vector3 gazeDirection;
        internal float foveaHalfRad = Mathf.Deg2Rad * 2.5f; // für 5° fovealer Bereich
        internal float periHalfRad = Mathf.Deg2Rad * 10f; // für 20° peripheren Bereich

        static readonly int gazeOriginID = Shader.PropertyToID("_GazeOrigin");
        static readonly int gazeDirectionID = Shader.PropertyToID("_GazeDirection");
        static readonly int foveaHalfRadID = Shader.PropertyToID("_FoveaAngle");
        static readonly int periHalfRadID = Shader.PropertyToID("_PeriAngle");

        readonly Material gazeMaterial;

        public GazeRenderPass(Material material) => gazeMaterial = material;

        public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
        {
            Debug.Log("RecordRenderGraph");
            var resourceData = frameData.Get<UniversalResourceData>();
            TextureHandle depth = resourceData.activeDepthTexture;
            TextureHandle color = resourceData.activeColorTexture;

            using var builder = renderGraph.AddRasterRenderPass<PassData>("GazeOverlay", out var passData);

            passData.depth = depth;
            passData.color = color;
            passData.gazeMaterial = gazeMaterial;
            passData.gazeOrigin = gazeOrigin;
            passData.gazeDirection = gazeDirection;
            passData.foveaHalfRad = foveaHalfRad;
            passData.periHalfRad = periHalfRad;

            builder.UseTexture(depth, AccessFlags.Read);
            builder.UseTexture(color, AccessFlags.ReadWrite);
            builder.SetRenderAttachment(color, 0);

            builder.SetRenderFunc((PassData data, RasterGraphContext ctx) =>
            {
                ctx.cmd.ClearRenderTarget(RTClearFlags.Color, new Color(1, 0, 1, 1), 1.0f, 0);
                //ctx.cmd.SetGlobalVector(gazeOriginID, data.gazeOrigin);
                //ctx.cmd.SetGlobalVector(gazeDirectionID, data.gazeDirection);
                //ctx.cmd.SetGlobalFloat(foveaHalfRadID, data.foveaHalfRad);
                //ctx.cmd.SetGlobalFloat(periHalfRadID, data.periHalfRad);

                //Blitter.BlitTexture(ctx.cmd, data.depth, new Vector4(1, 1, 0, 0),
                    //data.gazeMaterial, 0);
            });
        }

        class PassData
        {
            internal TextureHandle depth;
            internal TextureHandle color;
            internal Material gazeMaterial;
            internal Vector3 gazeOrigin, gazeDirection;
            internal float foveaHalfRad, periHalfRad;
        }
    }

    [SerializeField] Material gazeOverlayMaterial;
    GazeRenderPass pass;

    public GazeRenderPass GetPass() {  return pass; }

    public override void Create()
    {
        pass = new GazeRenderPass(gazeOverlayMaterial);
        Debug.Log("Feature created");
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(pass);
    }
}
