using Godot;

namespace Rokojori
{  
  [Tool]
  [GlobalClass]
  public partial class TextureFlareType:FlareType
  {
    public override Shader GetShader( FlareLayer.Data data )
    {
      var bm = data.GetBlendMode();

      if ( FlareBlendMode.BlendModeType.Add == bm )
      {
        return TextureTypeFlareAddShader.shader.Get();
      }

      if ( FlareBlendMode.BlendModeType.Mix == bm )
      {
        return TextureTypeFlareMixShader.shader.Get();
      }

      if ( FlareBlendMode.BlendModeType.Subtract == bm )
      {
        return TextureTypeFlareSubtractShader.shader.Get();
      }

      if ( FlareBlendMode.BlendModeType.Multiply == bm )
      {
        return TextureTypeFlareMultiplyShader.shader.Get();
      }

      return null;
      
    }

    public override ShaderMaterial CreateMaterial( FlareLayer.Data data )
    {
      var bm = data.GetBlendMode();

      if ( FlareBlendMode.BlendModeType.Add == bm )
      {
        return new TextureTypeFlareAddMaterial();
      }

      if ( FlareBlendMode.BlendModeType.Mix == bm )
      {
        return new TextureTypeFlareMixMaterial();
      }

      if ( FlareBlendMode.BlendModeType.Subtract == bm )
      {
        return new TextureTypeFlareSubtractMaterial();
      }

      if ( FlareBlendMode.BlendModeType.Multiply == bm )
      {
        return new TextureTypeFlareMultiplyMaterial();
      }

      return null;
    }

    [ExportGroup( "Texture")]
    [Export]
    public Texture2D texture;
    [Export]
    public float textureUVScale = 1f;
    [Export]
    public bool texureRepeat = false;

    [ExportGroup( "Mask")]
    [Export]
    public Texture2D mask;
    [Export]
    public float maskUVScale = 1f;
    [Export]
    public bool maskRepeat = false;

    [ExportGroup( "Noise","noise")]
    [Export( PropertyHint.Range, "0,1" ) ]
    public float noiseAmount = 0;

    [Export( PropertyHint.Range, "0.1,100" ) ]
    public float noiseScale = 0;

    public override void ApplyShape( FlareLayer.Data data )
    {
      var material = data.material;
      
      TextureTypeFlareAddShader.texture.Set( material, texture );
      TextureTypeFlareAddShader.textureUvScale.Set( material, textureUVScale );
      TextureTypeFlareAddShader.textureRepeat.Set( material, texureRepeat );

      TextureTypeFlareAddShader.textureMask.Set( material, mask );
      TextureTypeFlareAddShader.textureMaskUvScale.Set( material, maskUVScale );
      TextureTypeFlareAddShader.textureMaskRepeat.Set( material, maskRepeat );

      BaseFlareShader.shapeNoiseAmount.Set( material, noiseAmount );
      BaseFlareShader.shapeNoiseScale.Set( material, noiseScale );

    }
  }
}
