package net.mkonrad.synes.data;

import java.util.Locale;

import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import net.mkonrad.synes.util.MusicTools;

public abstract class SeqInstrument extends AbstractInstrument
{ 
  protected String[] _extraSeqArgs = { "dur", "instrument" };
  
  public SeqInstrument()
  {
    _synthArgs = new String[] { "amp", "sustain", "pitch", "pan" };
    _modArgs =  new String[] { "pan", "pitch"};
  }
  
  public String[] getExtraSeqArgs()
  {
    return _extraSeqArgs;
  }
  
  public Object[] getExtraSeqValues(Beat beat)
  {
    return new Object[] { String.format(Locale.US, "%1$.2f", beat.getDur()), _name };
  }
  
  public float[] getAllModValues(Point3f pos)
  {
    float[] values = new float[_modArgs.length];
    
    for (int i = 0; i < _modArgs.length; i++)
    {
      values[i] = getModValueForArg(_modArgs[i], pos);
    }
    
    return values;
  }

  public float getValueForArg(String argName, SynthObj synthData)
  {
    Beat beat = (Beat) synthData;
    
    if (argName.equals("pan"))
    {
      return getPanArg(beat.getInstrumentShape().getAbsPos());
    }
    else if (argName.equals("pitch"))
    {
      return getPitchArg(beat.getInstrumentShape().getAbsPos());
    }
    else if (argName.equals("sustain"))
    {
      return beat.getSustain();
    }
    else if (argName.equals("amp"))
    {
      return beat.getAmp();
    }
    
    return 0.0f;
  }
  
  public float getModValueForArg(String argName, Point3f pos)
  {
    if (argName.equals("pan"))
    {
      return getPanArg(new Vector3f(pos));
    }
    else if (argName.equals("pitch"))
    {
      return getPitchArg(new Vector3f(pos));
    }
    
    return 0.0f;
  }
  
  protected float getPanArg(Vector3f pos)
  {
    return MusicTools.getPanFromXCoord(pos.x);
  }
  
  abstract protected float getPitchArg(Vector3f pos);
}
