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

namespace Rokojori
{  
  [Tool]
  [GlobalClass]
  public partial class ScanLinesEffect:OverlayEffect
  {
    public ScanLinesEffect():base()
    {
      Initialize();
    } 

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

    [Export( PropertyHint.Range, "0,1") ]
    public Color topColor = Colors.White;

    [Export( PropertyHint.Range, "0,1") ]
    public Color bottomColor = new Color( 0.96f, 0.96f, 0.96f, 1.0f );

    [Export( PropertyHint.Range, "0,500") ]
    public int scanLineHeight = 2;

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

    [Export( PropertyHint.Range, "1,100") ]
    public int rgbSize = 2;

    [Export]
    public bool useRGBOffset = false;

    [Export( PropertyHint.Range, "0,500") ]
    public int rgbOffsetLineHeight = 2;

    [Export]
    public float jitterStrength = 1f;

    [Export( PropertyHint.Range, "1,120") ]
    public float jitterFPS = 60f;

    bool jitterFlag = false;




    RG_ScreenColorTexure screenColorTexture;
    RG_BufferTexture bufferTexture;    

    CEG_Copy copy;
    RG_ScanLines scanLines;    

    void Initialize()
    { 
      screenColorTexture = new RG_ScreenColorTexure( graph );
      bufferTexture = RG_BufferTexture.ScreenSize( graph );

      copy = new CEG_Copy( graph );
      scanLines = new RG_ScanLines( graph );
      graph.InitializeNodes();

      copy.SetTextureSlotInputs( screenColorTexture, bufferTexture );
      scanLines.SetTextureSlotInputs( copy.output, copy.input );

      

      graph.SetProcessOrder(
        screenColorTexture, 
        bufferTexture, 
        copy,
        scanLines 
      );
      
    }


    protected override void ForAllViews()
    {
      float duration = 1f / jitterFPS;
      float normalizedTime = ( TimeLine.osTime % duration ) / duration;
      float jitter = Mathf.Sin( normalizedTime * Mathf.Pi * 2f ) * jitterStrength; 

      // this.LogInfo( jitter );




      scanLines.constants.Set(        
        topColor,
        bottomColor,
        Vector4.Zero,
        Vector4.Zero,

        amount,
        (float) scanLineHeight,
        rgbTintAmount,
        (float) rgbOffsetLineHeight,

        (float) rgbSize,
        useRGBOffset ? 1f : 0f,
        jitter,
        0f
        
        
        

      );
    }

   
  }
}