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

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

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

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

    [Export]
    public Vector2 ellipseCenter = new Vector2( 0.5f, 0.5f );

    [Export]
    public Vector2 ellipseSize = new Vector2( 0.5f, 0.5f );

    [Export]
    public float ellipseScale = 1.0f;

    [Export]
    public bool keepRatio = true;

    [Export]
    public float minDistance = 0.1f;

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

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

    [Export( PropertyHint.Range, "-2,2") ]
    public float ringsType = 0.5f;


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


    [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_EllipseDistortion distortion;    

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

      copy = new CEG_Copy( graph );
      distortion = new RG_EllipseDistortion( 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(        
        new Vector4( 
          ellipseCenter.X, ellipseCenter.Y, ellipseSize.X * ellipseScale, ellipseSize.Y * ellipseScale 
        ),
        distortionAmount, 
        blendAmount,
        minDistance,
        1.0f + redShift,
        1.0f + greenShift,
        1.0f + blueShift,
        (float) smearingSteps,
        smearing,
        rings,
        keepRatio ? 1.0f : 0.0f,
        ringsIntensity,
        Mathf.Pow( 10f, -ringsDistribution ),
        ringsType
      );
    }

   
  }
}