// (c) MIT 2003.  All rights reserved.

// -*-Mode: Java-*- 
// $Id: RotationEvent.java,v 1.6 2003/03/06 00:40:49 moltmans Exp $
// RotationEvent.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Thu Jun 01 23:13:01 2000> 
// Time-stamp: <2000-07-19 17:00:43 moltmans> 
package rec.annotate.facts;

import rec.annotate.rules.*;
import java.util.*;
import rec.annotate.widgets.*;
import rec.util.*;
import rec.sys.*;
import rec.tms.*;

/**
 * This class is for storing information about rotations
 **/
public class RotationEvent extends Event
{
  protected MotionEvent m_motion;
  protected Body m_body;
  protected PinJoint m_pivot;

  public RotationEvent( Surface s, MotionEvent motion, Body body,
			PinJoint pivot )
  {
    super( s, motion.getStartTime(), motion.getEndTime() );
    m_motion = motion;
    m_body = body;
    m_pivot = pivot;
  }
  
  public Body getSubject()
  {
    return getBody();
  }

  public boolean isBasedOn( Annotation a ) 
  {
    if( a == null )
      return false;
    return a.sameFact( m_motion );
  }
    
  public Body getBody()
  {
    return m_body;
  }

  public String getFactType() 
  {
    return Type.ROTATION_EVENT;
  }

  public boolean sameFact( Fact fact ) 
  {
    if( fact instanceof RotationEvent ) {
      RotationEvent f = (RotationEvent)fact;
      
      if( m_body.sameFact( f.m_body ) &&
	  m_pivot.sameFact( f.m_pivot ) &&
	  m_motion.sameFact( f.m_motion ) )
      {
	return true;
      }
      else {
//  	DebugWindow.addLine( "rotate",
//  			     "m_motion: " + m_motion + " " +
//  			     "f.motion: " + f.m_motion + " " +
//  			     "m_body:   " + m_body + " " +
//  			     "f.body:   " + f.m_body );
	return false;
      }
    }
    return false;
  }

  public Event combineWith( Event event )
  {
    if( event instanceof MotionEvent ) {
      MotionEvent motion = (MotionEvent)event;
      if( m_body.sameFact( motion.m_body ) )
      {
	return new RotationEvent( getSurface(), m_motion,
				  m_body, m_pivot );
      }
    }
    else if( event instanceof RotationEvent ) {
      RotationEvent motion = (RotationEvent)event;
      if( m_body.sameFact( motion.m_body ) &&
	  m_pivot.sameFact( motion.m_pivot ) )
      {
	return new RotationEvent( getSurface(), m_motion,
				  m_body, m_pivot );
      }
    }
    return null;
  }
      
    

  public String toString() 
  {
    return( "<Rotation: of " + m_body.getName() +
	    " around " + m_pivot.getName() + ">" );
  }

  public String toEnglish()
  {
    return m_body.toEnglish() + " rotates around " +
      m_pivot.toEnglish();
  }
  
}
