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

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

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

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

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

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

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

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

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

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


    [Export( PropertyHint.Range, "1,10") ]
    public int smearingSteps = 5;

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

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

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

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


    RG_ScreenColorTexure screenColorTexture;
    RG_BufferTexture bufferTexture;    

    CEG_Copy copy;
    RG_EdgeDistortion distortion;    

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

      copy = new CEG_Copy( graph );
      distortion = new RG_EdgeDistortion( graph );
      graph.InitializeNodes();

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

      

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


    protected override void ForAllViews()
    {

      distortion.constants.Set(        
        distortionAmount, 
        blendAmount,
        horizontalEdge,
        verticalEdge,
        1.0f + redShift,
        1.0f + greenShift,
        1.0f + blueShift,
        (float) smearingSteps,
        smearing,
        Mathf.Pow( 10f, horizontalEdgePower ), 
        Mathf.Pow( 10f, verticalEdgePower ), 
        horizontalAmount,
        verticalAmount,
        0f, 
        0f,
        0f
      );
    }

   
  }
}