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

// -*-Mode: Java-*- 
// HelloWorld.java -- 
// Author: Christine Alvarado <calvarad@ai.mit.edu> 
// Maintainer: Christine Alvarado <calvarad@ai.mit.edu> 
// Version: 
// Created: <Sat Mar 11 14:21:32 2000> 
// Time-stamp: <2000-03-13 18:11:41 calvarad> 
// Keywords: 
package rec.sys;


import java.awt.*;
import java.awt.event.*;
import rec.util.*;
import java.util.*;
import rec.core.widgets.*;

public class RetryDialog extends ListDialog
{
  private Surface m_Surface;
  protected Collection m_Reps;
  protected Squiggle m_BaseSquiggle;
  protected WSelection m_Selection;
  protected Hashtable m_NameMap;
  protected boolean m_FirstSel;
  protected Hashtable m_WidgetMap;
  
  public RetryDialog( Frame parent, String name, Surface s,
		      Collection reps, Squiggle base, WSelection retry_sel,
		      boolean first_sel )
  {
    super( parent, name );

    m_Surface = s;
    m_Reps = reps;
    m_BaseSquiggle = base;
    m_Selection = retry_sel;
    m_FirstSel = first_sel;
    
    m_NameMap = new Hashtable();
    m_WidgetMap = new Hashtable();

    initializeList();
  }

  private void initializeList()
  {

    String labeltext = new String( "You rejected the following interpretations for the " );
    // build the string to make the label
    Vector labels = new Vector();
    if ( m_Selection != null ) {
      labeltext = labeltext.concat( "selected element: " );
    }
    else {
      labeltext = labeltext.concat( "last stroke drawn: " );
    }

    labels.add( new Label( labeltext ) );
    
    Iterator it = m_Reps.iterator();
    while ( it.hasNext() ) {
      Widget w = (Widget)it.next();
      String name = w.getEnglishName();
      if ( name == null ) {
	name = w.toString();
      }
      labels.add( new Label( name ) );
    }

    labels.add( new Label( "Please choose a new interpretation from the list below" ) );

    addLabels( labels );


    // impeach the reps to set things back to normal
    Iterator reps_it = m_Reps.iterator();
    while ( reps_it.hasNext() ) {
      ((Widget)reps_it.next()).impeach();
    }
    
    // now add all the choices to the list
    // make a list of the possibilities.
    Collection allChildren = m_BaseSquiggle.getAllChildrenAndMe();
    
    // now rank that list on strength left over from last time
    java.util.Comparator comp = new WidgetStrengthComparator();
    Object[] array = allChildren.toArray();
    Arrays.sort( array, comp );

    java.util.List sorted_children = Arrays.asList( array );

    Iterator child_it = sorted_children.iterator();
    // first build groups of the same types
    while ( child_it.hasNext() ) {
      Widget w = (Widget)child_it.next();
      String name = w.getEnglishName();
      if ( (!m_Reps.contains( w )) && ( ! (w instanceof WGroup ) ) &&
	   w.alive() && ( name != null ) ) {
	if ( m_WidgetMap.containsKey( name ) ) {
	  Collection c = (Collection)m_WidgetMap.get( name );
	  c.add( w );
	}
	else {
	 Collection c = new HashSet();
	 c.add( w );
	 m_WidgetMap.put( name, c );
	}
      }
    }

    // now go throuh the hashtable and build the list
    int i = 0;
    child_it = m_WidgetMap.keySet().iterator();
    while ( child_it.hasNext() ) {
      String name = (String)child_it.next();
      String name_plu = new String( name );
      Collection c = (Collection)m_WidgetMap.get( name );
      if ( c.size() > 1 ) {
	name_plu = name.concat( "s" );
      }
      
      Integer index = new Integer( i );
      m_NameMap.put( index, name );
      addListElement( name_plu );
      i++;
    }
    addListElement( "None of the above.  I wish to redraw this object." );

    
  }

  protected void okCommand()
  {
    // first make sure there is something selected
    int selected_index = m_List.getSelectedIndex();

    while ( selected_index < 0 ) {
      MessageBox m = new MessageBox( this, "",
				     "Please choose an interpretation" );
      m.show();
      return;
    }

    if ( !m_NameMap.containsKey( new Integer( selected_index ) ) ) {
      // the user wants to redraw the item, so just delete the current
      // thing drawn.
      // NOTE: this is not undoable, but what is anymore...?
      Iterator reps_it = m_Reps.iterator();
      while ( reps_it.hasNext() ) {
	((Widget)reps_it.next()).elect( m_Surface );
      }


      if ( m_Selection != null ) {
	m_Selection.removeSelection( m_Surface );
	Iterator it = m_Reps.iterator();
	while ( it.hasNext() ) {
	  DoTransition dt = new DieTransition( m_Surface, (Widget)it.next() );
	  dt.doDo();
	}
	DoTransition dt = new DieTransition( m_Surface, m_Selection );
	dt.doDo();
	m_Surface.expunge();
      }
      else {
	DoTransition dt = new DieTransition( m_Surface, m_BaseSquiggle );
	dt.doDo();
	m_Surface.resetWidgetPools();
      }
      
    }

    else {
      String name = (String)m_NameMap.get( new Integer( selected_index ) );

      Collection wids = (Collection)m_WidgetMap.get( name );

      Iterator it = wids.iterator();
      while ( it.hasNext() ) {
	Widget w = (Widget)it.next();
	w.setUserStrength( 1 );
      }
      
      WGroup sel_grp = null;
      if ( m_Selection != null ) {
	sel_grp = m_Selection.unhookSelection( m_Surface );
      }

      WGroup newReps =
	new WGroup( new Vector( m_Surface.retryBase( m_BaseSquiggle,
						     m_Reps ) ));
      m_Surface.resetSelection( newReps, sel_grp );
      
    }

      
    dispose();


  }

  protected void cancelCommand()
  {
    // now elect the reps again in case the user hit cancel
    Iterator reps_it = m_Reps.iterator();
    while ( reps_it.hasNext() ) {
      ((Widget)reps_it.next()).elect( m_Surface );
    }

    // do nothing, just return
    m_Surface.retryCancel( m_FirstSel );
    dispose();
  }
  
}
