using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
using Godot;

namespace Rokojori
{  
  [Tool]
  [GlobalClass]
  public partial class TextureWipeEffect:WipeEffect  
  { 
    [ExportGroup("Texture")]
    [Export]
    public Texture2D texture;

    [Export]
    public Gradient tint;
    
    [ExportGroup("Texture Transform")]
    [Export]
    public Curve rotation = MathX.Curve( 0, 0 );    

    [Export]
    public Curve scale = MathX.Curve( 1, 1 );

    [Export]
    public Curve translationX = MathX.Curve( 0, 0 );

    [Export]
    public Curve translationY = MathX.Curve( 0, 0 );

    [ExportGroup("Texture Transform Centers")]
    [Export]
    public Vector2 rotationCenter = new Vector2( 0.5f, 0.5f ); 

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


    public static readonly Texture2DPropertyName _wipeTextureProperty = Texture2DPropertyName.Create( "wipeTexture" );
    public static readonly Vector2PropertyName _outputViewSizeProperty = Vector2PropertyName.Create( "outputViewSize" );
    public static readonly Vector2PropertyName _wipeTextureSizeProperty = Vector2PropertyName.Create( "wipeTextureSize" );
    public static readonly Vector2PropertyName _uvRotationCenterProperty = Vector2PropertyName.Create( "uvRotationCenter" );
    public static readonly Vector2PropertyName _uvScaleCenterPropery = Vector2PropertyName.Create( "uvScaleCenter" );


    public static readonly ColorPropertyName _wipeTextureTintProperty = ColorPropertyName.Create( "wipeTextureTint" );
    public static readonly FloatPropertyName _uvRotationProperty = FloatPropertyName.Create( "uvRotation" );   
    public static readonly FloatPropertyName _uvScaleProperty = FloatPropertyName.Create( "uvScale" );    
    public static readonly Vector2PropertyName _uvTranslationProperty = Vector2PropertyName.Create( "uvTranslation" );
    


    public override void Assign( ColorRect colorRect )
    {
      if ( colorRect == null )
      {
        return;
      }

      var material = GetMaterial( colorRect );
      
      if ( texture != null )
      {
        _wipeTextureProperty.Set( material, texture );
        _wipeTextureSizeProperty.Set( material, texture.GetSize() );

        _uvRotationCenterProperty.Set( material, rotationCenter );
        _uvScaleCenterPropery.Set( material, scaleCenter );
      }

      _outputViewSizeProperty.Set( material, colorRect.GetViewportRect().Size );

    }

    public override void SetWipeState( float state, ColorRect target )
    {
      if ( target == null )
      {
        return;
      }
      
      var material = GetMaterial( target );
      
      _wipeTextureTintProperty.Set( material, tint.Sample( state ) );
      _uvRotationProperty.Set( material, rotation.Sample( state ) );
      _uvScaleProperty.Set( material, rotation.Sample( state ) );
      _uvTranslationProperty.Set( material, new Vector2( translationX.Sample( state ), translationY.Sample( state ) ) );

    }
    
  }
}