
using Godot;
using Godot.Collections;
using System.Collections.Generic;

namespace Rokojori
{  
  [Tool]
  [GlobalClass]
  public partial class TemporalSmearEffect:SingleShaderCompositorEffect
  {
    public static readonly string shaderPath = 
      RokojoriPlugin.Path( "Runtime/Rendering/CompositorEffects/TemporalSmear/TemporalSmear.glsl" );

    [Export( PropertyHint.Range, "0,1")]
    public float amount = 1f;

    [Export( PropertyHint.Range, "0,1")]
    public float smearing = 0.5f;

    [Export( PropertyHint.Range, "0,1")]
    public float lumaAmount = 0.5f;

    [Export( PropertyHint.Range, "0,1")]
    public float lumaTreshold = 0.5f;

    [Export( PropertyHint.Range, "0.5,5")]
    public float lumaSaturate = 1.3f;

    [Export( PropertyHint.Range, "0,1")]
    public float blur = 0.5f;

    [Export( PropertyHint.Range, "1,30")]
    public float blurRange = 2f;

    protected override void OnConfigure()
    {
      EffectCallbackType = EffectCallbackTypeEnum.PostTransparent;
      _shaderPath = shaderPath;
      _groupSize = 8;
    }


    protected override void SetConstants()
    {     
      constants.Set(
        amount,
        smearing,
        lumaAmount,
        lumaTreshold,
        lumaSaturate,
        blur,
        blurRange
      );
    }

    RDTexture _bufferTexture;
    
    protected override void RenderView()
    {
      if ( _bufferTexture == null || _bufferTexture.size != context.internalSize )
      {
        if ( _bufferTexture != null )
        {
          rd.FreeRid( _bufferTexture.rid );
          _bufferTexture = null;
        }

        _bufferTexture = RDTexture.Create( this, context.internalSize );
      }

      context.AssignUniformSet_Texture( _bufferTexture );
      context.Assign_ScreenColorTexture();
      

      context.pushConstants = constants;

      context.Render();

    }
  }
}