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

// -*-Mode: Java-*- 
// $Id: LeverBalanceRule.java,v 1.5 2005/02/17 21:40:59 moltmans Exp $
// LeverBalanceRule.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Tue May 23 14:42:43 2000> 
// Time-stamp: <2005-02-16 14:04:49 moltmans> 
package rec.annotate.rules;

import rec.annotate.facts.*;
import java.util.*;
import rec.util.*;
import rec.tms.*;
import rec.sys.*;
import rec.simulate.widgets.*;
import rec.geo.*;

/**
 * 
 **/
public class LeverBalanceRule extends AnnotateRule
{

  public LinkedList getTriggers() 
  {
    LinkedList triggers = new LinkedList();
    triggers.add( Type.LEVER );
    triggers.add( Type.FORCE );
    triggers.add( Type.FORCE );
    return triggers;
  }

  public boolean match( LinkedList params ) 
  {
    if( params.size() == 1 )
      return true;

    Lever lever = (Lever)params.get(0);
    Force force1 = (Force)params.get(1);
    if( !force1.getBody().equals( lever.getBody() ) ) {
      return false;
    }
    if( params.size() == 2 ) {
      return true;
    }

    Force force2 = (Force)params.get(2);
    if( force1.equals( force2 ) ||
        !force2.getBody().equals( lever.getBody() ) )
    {
      return false;
    }

//      DebugWindow.addLine( "Bal", "force1: " + force1 );
//      DebugWindow.addLine( "Bal", "force2: " + force2 );
    return opposingForces( lever, force1, force2 );
  }
  
  private boolean opposingForces( Lever lever, Force f1, Force f2 )
  {
    GPoint p1 = f1.getPoint();
    GPoint p2 = f2.getPoint();

//      DebugWindow.addLine( "Balance", "f1 = " + f1 );
//      DebugWindow.addLine( "Balance", "f2 = " + f2 );
//      DebugWindow.addLine( "Balance",
//  			 "Same half: " + lever.sameHalf( p1, p2 ) );
//      DebugWindow.addLine( "Balance",
//  			 "Same direction: " +
//  			 f1.isSameVerticalDirection( f2 ) );
    
    return ( lever.sameHalf( p1, p2 ) ^
	     f1.isSameVerticalDirection( f2 ) );
  }
  

  public void run( Surface s, Notebook book, LinkedList params )
  {
    Lever lever = (Lever)params.get(0);
    Force force1 = (Force)params.get(1);
    Force force2 = (Force)params.get(2);

    LinkedList list = new LinkedList();
    list.add( force1 );
    list.add( force2 );
    
    Balances balances = new Balances( lever, list );
    
    book.myassert( balances, "LeverBalanceRule", params );
  }
}

