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

namespace Rokojori
{ 
  [Tool]
  [GlobalClass]
  public partial class UVScaleOffset:UVModifier
  {   
    [Export]
    public Vector2Property uvScale = Vector2Property.Create( "uv_scale", Vector2.One );

    [Export]
    public Vector2Property uvOffset = Vector2Property.Create( "uv_offset", Vector2.Zero );

    [Export]
    public Vector2Property uvScrolling = Vector2Property.Create( "uv_scrolling", Vector2.Zero );

    [Export]
    public string target = "UV";


    public override List<ShaderCode> GetCode( ShaderGenerationContext context )
    {
      if ( ShaderPhase.Variables == context.phase )
      {        
        var lines = new List<string>();


        Lists.From( uvScale, uvOffset, uvScrolling ).FilterNulls().ForEach(
          ( p )=>
          {
            lines.Add( Uniform( p ) + "\n" );
          }
        );    


        return AsUniformGroup( "UV", lines.Join( "" ) );
           
      }

      if ( ShaderPhase.Vertex == context.phase )
      {        
        var expression = "  " + target + " = " + target; 

        if ( uvScale != null )
        {
          expression += " * " + uvScale.name;
        }

        if ( uvOffset != null )
        {
          expression += " + " + uvOffset.name;
        }

        if ( uvScrolling != null )
        {
          expression += " + TIME * " + uvScrolling.name;
        }

        expression += ";";

        return ToCode( expression );
           
      }

      return null;
    }
  }
}