import java.awt.*;
import java.applet.*;

public class Input extends Applet{
       
	protected TextField input_field; 
	protected Button butt_input;
        protected Label info_input;       
        protected InputItem[] all_input_items = new InputItem[30];
        protected int how_many_items;
        protected Panel Left;
        protected Panel Right;
        protected Button butt_parse;     
        protected Choice choose_text;

        public void init(){ 
        setBackground( new Color(230, 230, 230)); //light gray
	       
           butt_parse = new Button("Parse");     
           butt_parse.setBackground(Color.red);
           butt_input=new Button("Check Sentence");

	    input_field=new TextField(45);     
            info_input = new Label("        Choose or Enter your sentence              ", Label.LEFT);

	    choose_text=new Choice();                 
            choose_text.addItem("");
            choose_text.addItem("");
            choose_text.addItem("tourists visit their friends in Egypt with pleasure");
	    choose_text.addItem("fish sleep in an ocean");
	    choose_text.addItem("men fish in a big ocean");
            choose_text.select(""); 
         

               //ORGANIZE LAYOUT       
               Right=new Panel();	
               
              Panel butt_panel=new Panel();
                             
           GridBagLayout gbl = new GridBagLayout();
	   GridBagConstraints gbc = new GridBagConstraints();       
           setLayout(gbl); 
        
        gbc.gridx = 0;
	gbc.gridy = 0;
        gbc.anchor=GridBagConstraints.NORTH;  
        gbl.setConstraints(info_input,gbc);
	add(info_input);
	
        gbc.gridx = 0;
	gbc.gridy = 1;	
        gbc.anchor=GridBagConstraints.NORTH;  
        gbl.setConstraints(input_field,gbc);
	add(input_field);
       
        gbc.gridx = 0;
	gbc.gridy = 2;	
        gbc.anchor=GridBagConstraints.NORTH;  
        gbl.setConstraints(choose_text,gbc);
	add(choose_text);
         
        gbc.gridx = 1;
	gbc.gridy = 0;	
        gbc.anchor=GridBagConstraints.NORTH;  
        gbl.setConstraints(butt_panel,gbc);
	add(butt_panel);
               butt_panel.add(butt_input);
               butt_panel.add(butt_parse);              

        gbc.gridx = 1;
  	gbc.gridy = 1;	
        gbc.weightx = 2;
        gbc.weighty = 3;
        gbc.gridheight=GridBagConstraints.REMAINDER;
	gbc.fill=GridBagConstraints.BOTH;  
        gbc.anchor=GridBagConstraints.NORTH;  
        gbl.setConstraints(Right,gbc);
	add(Right);
      }
      
   //POSSIBLE ACTIONS 
    
    public boolean action(Event evt, Object whatAction){                       
       if( (evt.target == input_field) ||
	   (evt.target == butt_input)){    
            clean_old_stuff();
	    if(inputOK()){              
               if(process_input()){
                  print_input(Right, all_input_items, how_many_items);
            //print the same input_table on the second frame                  
		  print_input(MainFrame().myParsing.panel_for_input,
                              all_input_items, how_many_items);
                  info_input.setText("        Choose or Enter your sentence            ");
                  return true;
               }else return false;
            }else{
               info_input.setText("      No sentence to process!      ");
               return true;
            }
       }else if(evt.target == butt_parse){
             if(all_input_items[0]==null){
                 info_input.setText("             Check sentence first!                ");
                 return true;
             }else{  
                 MainFrame().myParsing.set_init_values();         
                 //change the frame (show the second frame in CardLayout 
                 MainFrame().myCardLayout.last(MainFrame()); 
                 return true;  
             }
       }else if(evt.target == choose_text){
              input_field.setText(choose_text.getSelectedItem());
              return true;                
       }else return false; 
   }

//are there some words in input?

   protected boolean inputOK(){
           String sentence1 = input_field.getText();
           sentence1=sentence1.trim();
           //input field is preferred; check it first; 
           if(sentence1.length() != 0){
              return true;
           }else return false;
   }              

//read word by word and look in the lexicon for a part of speech

   protected boolean process_input(){
           String curr_word; 

           //ensure one blank at the end of input
           String curr_input=( (input_field.getText()).trim()+" ");
         
           int next_blank; 
           int position=0;  //word's place in input         
           String[] curr_speechparts = new String[10];  //list of possible parts of speech for each word         

           //take words from input one after another and process
	   
           while ( !curr_input.equals(" ") ) {
	       next_blank = curr_input.indexOf(" ");

	       //take next word from input
               curr_word = curr_input.substring(0, next_blank);
               
               curr_speechparts=look_in_lexicon(curr_word);
	       
               if(curr_speechparts[0].equals("not found")){
                  info_input.setText("\""+curr_word+"\""+" not found in lexicon!");
                  clean_old_stuff();
                  return false;
               }
               InputItem curr_item=new InputItem( (position+1), curr_word, curr_speechparts);
               all_input_items[position]=curr_item;
              
               position++;         
               curr_input = curr_input.substring(next_blank);
               curr_input = (curr_input.trim()+" ");
                 
           }    
           how_many_items=position;
           return true;
     }       

     protected String[] look_in_lexicon(String word){
             List curr_lexicon;
             String[] speechparts=new String[10];
             speechparts[0]="not found";             
             curr_lexicon=FirstFrame().myLexicon.my_lexicon;           
           
             int j=0; //number of the next speechpart's string in speechpart's array
             for(int i=0; i< curr_lexicon.countItems(); i++){
               
               if( ( ( FirstFrame().myLexicon.restore_LexItem 
                       (curr_lexicon.getItem(i))).get_word()).equals(word) ){
                 speechparts[j]= 
                   (FirstFrame().myLexicon.restore_LexItem(curr_lexicon.getItem(i))).get_speechpart();
                 j++; 
	       }
             }
             return speechparts;
     } 

//print_input - printing processed input in the table form inside applet
//will be used once again to repete input table on the parsing frame
//-> has wider usability    
  
     protected void print_input(Panel where_panel, InputItem[] all_input_items,
                                int how_many_items){
          
          where_panel.removeAll(); //clean the place
          where_panel.setLayout(new GridLayout(0,how_many_items));
          for(int i=0; (i<all_input_items.length) && 
                         (all_input_items[i]!=null); i++){
                Label curr_label= new Label("P:" + String.valueOf(all_input_items[i].get_position()));
                where_panel.add(curr_label);
                
          }           
          
          for(int i=0; (i<all_input_items.length) && 
                         (all_input_items[i]!=null); i++){
                Label curr_label= new Label(all_input_items[i].get_word());
                curr_label.setForeground(Color.red);
                where_panel.add(curr_label);
		curr_label.show();
                
          }                  
        
          String sp_parts="";         
          for(int i=0; (i<all_input_items.length) && 
                            (all_input_items[i]!=null); i++){
               for(int j=0; (j<all_input_items[i].get_speechparts().length) &&
                             (all_input_items[i].get_speechparts()[j] != null); 
                       j++){                 
                         sp_parts = sp_parts + all_input_items[i].get_speechparts()[j] + " ";     
               } 		
               Label curr_label= new Label(sp_parts);
               where_panel.add(curr_label);
               sp_parts="";
          }
	where_panel.validate();
       }

   public LexRulesInput FirstFrame(){
      return (LexRulesInput) getParent();   
   }

   public DemoParser MainFrame(){
      return (DemoParser) FirstFrame().getParent();   
   }

   public void clean_old_stuff(){
       Right.removeAll(); //clean the place       
       how_many_items=0; 
       for(int i=0; i<all_input_items.length; i++){
               all_input_items[i]=null;
       }
   } 
}
