package net.mkonrad.synes.data;

import javax.vecmath.Point3f;

import net.mkonrad.synes.util.Common3DTools;
import net.mkonrad.synes.util.MusicTools;

public abstract class SynthInstrument extends AbstractInstrument
{
  protected String[] _modArgs;
  
  public SynthInstrument()
  {
    super();
    _synthArgs = new String[] { "x", "y", "amp" };
    _modArgs = new String[] {  "x", "y", "amp", "modDist", "modAmp" };
  }
  
  public boolean needsKeyboard()
  {
    return true;
  }
  
  public String[] getModArgs()
  {
    return _modArgs;
  }

  public float getValueForArg(String argName, SynthObj synthData)
  {
    if (argName.equals("x"))
    {
      return getXArg(synthData);
    }
    else if (argName.equals("y"))
    {
      return getYArg(synthData);
    }
    else if (argName.equals("amp"))
    {
      return synthData.getAmp();
    }
    
    return 0.0f;
  }
  
  public float[] getAllModValues(SynthObj synth, Modulator mod)
  {
    float[] values = new float[_modArgs.length];
    
    for (int i = 0; i < _modArgs.length; i++)
    {
      values[i] = getModValueForArg(_modArgs[i], synth, mod);
    }
    
    return values;
  }
  
  public float getModValueForArg(String argName, SynthObj synth, Modulator mod)
  {
    if (argName.equals("modDist"))
    {
      //return getModDistArg(synth, mod);
      return synth.getDistance();
    }
    else if (argName.equals("modAmp"))
    {
      return mod.getAmp();
    }
    else
    {
      return getValueForArg(argName, synth);
    }
  }
  
  protected float getModDistArg(SynthObj synth, Modulator mod)
  {
    return Common3DTools.distance2D(new Point3f(synth.getInstrumentShape().getAbsPos()),
        new Point3f(mod.getInstrumentShape().getAbsPos()));
  }
  
  protected float getXArg(SynthObj synth)
  {
    return MusicTools.getPanFromXCoord(synth.getInstrumentShape().getAbsPos().x);
  }
  
  protected float getYArg(SynthObj synth)
  {
    return MusicTools.getMidiFromYCoord(synth.getInstrumentShape().getAbsPos().y, false);
  }
}
