package net.mkonrad.synes.ctrl;

import javax.media.j3d.Node;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import net.mkonrad.synes.data.Beat;
import net.mkonrad.synes.data.BeatGraph;
import net.mkonrad.synes.gui.elements.AbstractGraphLine;
import net.mkonrad.synes.util.Common3DTools;
import net.mkonrad.synes.util.DbgTools;

public class SeqLinePickingCtrl extends AbstractLinePickingCtrl
{

  public SeqLinePickingCtrl(CentralPickingCtrl pickCtrl)
  {
    super(pickCtrl);
  }

  public void removeGraph()
  {
    if (_selectedGraphLine == null)
      return;
    
    if (_coreCtrl.isOSCMode())
    {
      _coreCtrl.getSCLang().sendOSCData("/change", "removeGraph;" + _selectedGraphLine.getGraph().getId());
    }
    
    _selectedGraphLine.getBG().detach();
    _coreCtrl.getCurrLayer().removeGraph(_selectedGraphLine.getGraph().getId());
    
    clearSelection();
  }

  public boolean clicked(Node pickedObj, Point3f where, int mouseState)
  {
    if (_pickCtrl.getState() == CentralPickingCtrl.STATE_GRAPH_CREATE)
    {
      return false;
    }
    else
    {
      makeSelection((AbstractGraphLine)pickedObj, where);
      
      if (_pickCtrl.getMouseEvent().isControlDown() && _coreCtrl.isOSCMode())
      {
        _pickCtrl.setState(CentralPickingCtrl.STATE_GRAPH_PAUSED);
        _pickCtrl.pauseBeatGraph((BeatGraph)_selectedGraphLine.getGraph());
      }
    }
    
    return true;
  }

  public boolean dragged(Point3f intercept, int mouseState)
  {
    moveGraphLine(new Vector3f(intercept));
    
    return true;
  }
  
  public boolean released(Point3f intercept, int mouseState)
  {
    if (_selectedGraphLine != null)
    {
      if (_coreCtrl.isOSCMode())
      {
        if (_pickCtrl.getState() == CentralPickingCtrl.STATE_GRAPH_PAUSED)
        {
          _pickCtrl.resumeBeatGraph((BeatGraph)_selectedGraphLine.getGraph());
        }
        else if (_pickCtrl.getState() == CentralPickingCtrl.STATE_GRAPH_MOVE)
        {
          _coreCtrl.getSCLang().sendOSCDataOneByOne("/change",
              ((BeatGraph)_selectedGraphLine.getGraph()).generateOSCData(0)); //"0" means make an update
        }
      }
      
      clearSelection();
    }
    
    return true;
  }
}
