using Godot;
using System.Collections.Generic;

namespace Rokojori
{  
  public static class CurveExtensions
  {
    public static Curve WithPoints( this Curve curve, params Vector2[] points )
    {
      curve.ClearPoints();

      points.ForEach( p => curve.AddPoint( p ) );      
      
      return curve;
    }

    public static Curve WithValues( this Curve curve, params float[] values )
    {
      curve.ClearPoints();

      for ( int i = 0; i < values.Length; i++ )
      {
        float x = i / (float) ( values.Length - 1f );
        curve.AddPoint( new Vector2( x, values[ i ] ) );
      }
      
      return curve;
    }

    public static Curve WithDomainRange( this Curve curve, float min, float max )
    {
      curve.MinDomain = min;
      curve.MaxDomain = max;

      return curve;
    }

    public static Curve WithValueRange( this Curve curve, float min, float max )
    {
      curve.MinValue = min;
      curve.MaxValue = max;

      return curve;
    }

    public static Curve WithLinearTangents( this Curve curve )
    {
      var points = curve.PointCount;

      for ( int i = 0; i < points; i++ )
      {
        if ( i > 0 )
        { 
          curve.SetPointLeftTangent( i, curve.GetPointPosition( i ).Y - curve.GetPointPosition( i - 1 ).Y );
        }

        if ( i < ( points - 1 ) )
        {
          curve.SetPointRightTangent( i, curve.GetPointPosition( i + 1 ).Y - curve.GetPointPosition( i ).Y );
        }
      }

      for ( int i = 0; i < points; i++ )
      {
        if ( i > 0 )
        { 
          curve.SetPointLeftMode( i, Curve.TangentMode.Linear );
        }

        if ( i < ( points - 1 ) )
        {
          curve.SetPointRightMode( i, Curve.TangentMode.Linear );
        }
      }

      return curve;
    }
  }
}