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

// $Id: EnterAnnotationModeRecognizer.java,v 1.20 2003/03/06 00:40:49 moltmans Exp $
// EnterAnnotationModeRecognizer.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Sat Nov  6 19:17:56 1999> 
// Time-stamp: <2001-09-11 16:07:02 moltmans> 
package rec.annotate.recognizers;

import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;

import rec.annotate.*;
import rec.simulate.*;
import rec.simulate.widgets.*;
import rec.core.speech.*;
import rec.util.*;
import rec.sys.*;
import rec.speech.*;
import rec.annotate.gui.*;

public class EnterAnnotationModeRecognizer extends SimpleUtteranceRecognizer
{

  public EnterAnnotationModeRecognizer()
  {
    super( "enter_annotation_mode" );
  }

  public void performAction( Surface s, Object arg )
  {
    //    printSimObjects( s );
    addGrammarForSimObjects( s );
    ((SimulateSurface)s).findAttachments();
    s.getNotebook().assertDesign();
    s.setIsWidgetNameShown( true );
    s.enterEditMode( "Annotation" );
    
    makeDebugFrame( s );
  }
  
  private void makeDebugFrame( Surface s ) 
  {
    final Surface m_frameSurface;
  
    m_frameSurface = s;
    //    JFrame f = new JFrame( "Debugger" );
    JButton button = new JButton("show labels");
    button.addActionListener( new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  boolean val = !m_frameSurface.isWidgetNameShown();
	  m_frameSurface.setIsWidgetNameShown( val );
	  m_frameSurface.getScreen().repaint();
	}});

    JButton run = new JButton("run rules");
    run.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  m_frameSurface.getNotebook().updateNotes();
	}
      });
    
    DebugWindow.addButton("buttons", button);
    DebugWindow.addButton("buttons", run);
//      f.getContentPane().add( button );
//      f.getContentPane().setLayout( new FlowLayout() );
//      f.pack();
//      f.show();
  }

  public void performUndo( Surface s, Object arg )
  {
    s.enterEditMode( EditModeManager.DEFAULT_MODE );
    s.setIsWidgetNameShown( false );
  }

  private void printSimObjects( Surface s )
  {
    Iterator it = s.getSurfaceWidgets().iterator();
    Object o;

    while ( it.hasNext() ) {
      o = it.next();
      if ( o instanceof WSimObject ) {
	DebugWindow.addLine( "Parts", ((WSimObject)o).getEnglishName() );
      }
    }
    
  }

  /** Add english names of all components to the grammar of components **/
  private void addGrammarForSimObjects( Surface s )
  {
    HashSet names = new HashSet();
    Iterator it = s.getSurfaceWidgets().iterator();
    Object o;

    while ( it.hasNext() ) {
      o = it.next();
      if ( o instanceof WSimObject ) {
	String basename = ((WSimObject)o).getEnglishName();
	if( basename == null || basename.equals( "" ) )
	  continue;
	if( basename.toLowerCase().endsWith( "body" ) ) {
	  basename = "body";
	  //	  basename = "";
	}
	else if ( ! (o instanceof WSimSpring) ) {
	  ((WSimObject)o).setEnglishName("");
	  continue;
	}
	

	//	basename = "";
	int i = 1;
	String name = (basename + " " + i++).trim();
	while( names.contains( name ) ) {
	  name = (basename + " " + i++).trim();
	}
	names.add( name );
	((WSimObject)o).setEnglishName( name );
      }
    }

    RecGrammar components =
      RecGrammar.getRecGrammar( "rec.annotate.recognizers.TopLevel" );

    //    names.add("this");
    components.addAlternatives( "lex-N_component", new Vector( names ) );
  }


}
