/***
 * File neve: TerminalFelulet.java
 * Szerző: Szatmáry Botond
 * Utolsó módosítás: 1998. jun. 15.
 * Leírás: Grafikus felület ami használja TerminaltFogad osztály metódusait.
 * Ez az osztály teszi lehetőve, hogy könnyen lehessen a gyűrűre egy személy
 * adatait másolni, róla olvasni. 
 ***/

import java.smartcard.*;
import java.awt.*;

public class TerminalFelulet extends Frame implements Runnable
{
  CheckboxGroup adapterSelection;
  Checkbox serialNative, serialPure, parallelNative;
   
  String titleHasiButton = "Terminal (iButton inserted)";
  String titleNoiButton = "Terminal (No iButton inserted)";

  TerminaltFogad terminal; 

  graf g;
  
  Thread driver;
   
  boolean haveiButton;
  
  public TerminalFelulet(String[] args, graf g)
  {
    super("Terminal");
    this.g=g;

    setLayout(null);
    setVisible(false);
    setSize(getInsets().left + getInsets().right + 350,getInsets().top + getInsets().bottom + 275);
    setBackground(new Color(12632256));
    setTitle("Terminál");
    setResizable(false);

    label0 = new java.awt.Label("Ring ID");
    label0.setBounds(getInsets().left + 10,getInsets().top + 35,80,18);
    label0.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label0);
    idField = new java.awt.TextField();
    idField.setEditable(false);
    idField.setBounds(getInsets().left + 95,getInsets().top + 35,200,32);
    idField.setFont(new Font("Dialog", Font.PLAIN, 12));
    add(idField);
    label1 = new java.awt.Label("Üdvözöllek");
    label1.setBounds(getInsets().left + 10,getInsets().top + 70,80,18);
    label1.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label1);
    nameField = new java.awt.TextField();
    nameField.setBounds(getInsets().left + 95,getInsets().top + 70,200,32);
    nameField.setFont(new Font("Dialog", Font.PLAIN, 12));
    add(nameField);
    label2 = new java.awt.Label("Csoport:");
    label2.setBounds(getInsets().left + 10,getInsets().top + 105,80,18);
    label2.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label2);
    groupField = new java.awt.TextField();
    groupField.setBounds(getInsets().left + 95,getInsets().top + 105,200,32);
    groupField.setFont(new Font("Dialog", Font.PLAIN, 12));
    add(groupField);
    label3 = new java.awt.Label("Utad:");
    label3.setBounds(getInsets().left + 10,getInsets().top + 140,80,18);
    label3.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label3);
    roadField = new java.awt.TextField();
    roadField.setBounds(getInsets().left + 95,getInsets().top + 140,200,32);
    roadField.setFont(new Font("Dialog", Font.PLAIN, 12));
    add(roadField);
    label4 = new java.awt.Label("Terminál:");
    label4.setBounds(getInsets().left + 10,getInsets().top + 175,80,18);
    label4.setFont(new Font("Dialog", Font.BOLD, 14));
    add(label4);
    terminalField = new java.awt.TextField();
    terminalField.setBounds(getInsets().left + 95,getInsets().top + 175,200,32);
    terminalField.setFont(new Font("Dialog", Font.PLAIN, 12));
    add(terminalField);

    okButton = new java.awt.Button();
    okButton.setActionCommand("button");
    okButton.setLabel("OK");
    okButton.setBounds(getInsets().left + 10,getInsets().top + 225,90,32);
    okButton.setFont(new Font("Dialog", Font.BOLD, 18));
    okButton.setBackground(new Color(12632256));
    add(okButton);
    clearButton = new java.awt.Button();
    clearButton.setActionCommand("button");
    clearButton.setLabel("Clear All");
    clearButton.setBounds(getInsets().left + 110,getInsets().top + 225,90,32);
    clearButton.setFont(new Font("Dialog", Font.BOLD, 18));
    clearButton.setBackground(new Color(12632256));
    add(clearButton);
    cancelButton = new java.awt.Button();
    cancelButton.setActionCommand("button");
    cancelButton.setLabel("Cancel");
    cancelButton.setBounds(getInsets().left + 210,getInsets().top + 225,90,32);
    cancelButton.setFont(new Font("Dialog", Font.BOLD, 18));
    cancelButton.setBackground(new Color(12632256));
    add(cancelButton);

    SymAction lSymAction = new SymAction();
    cancelButton.addActionListener(lSymAction);
    clearButton.addActionListener(lSymAction);
    okButton.addActionListener(lSymAction);
    
    if ((args.length > 0) && (args[0].length() > 0))
      terminal = new TerminaltFogad(args[0]);
    else
      terminal = new TerminaltFogad();
    
    driver = new Thread(this);
    driver.start();
  }

  java.awt.Label label0;
  java.awt.TextField idField;
  java.awt.Label label1;
  java.awt.TextField nameField;
  java.awt.Label label2;
  java.awt.TextField groupField;
  java.awt.Label label3;
  java.awt.TextField roadField;
  java.awt.Label label4;
  java.awt.TextField terminalField;
  java.awt.Button okButton;
  java.awt.Button clearButton;
  java.awt.Button cancelButton;

  public void run()
  {
    while (true)
      {
	try
	  {
            Thread.sleep(250);
	  }
	catch (InterruptedException e)
	  {
	  }
	
	// Ellenorzi az eltuno iButton-t
	if (haveiButton && !terminal.isiButtonInserted())
	  {
	    haveiButton = false;
            clearFields();
            setTitle(titleNoiButton);
	  }
	else
	  {
	    // Ellenorzi a megjeleno iButton-t
            if (!haveiButton && terminal.isiButtonInserted())
	      {
		ErrorDialog status = new ErrorDialog(this, "Selecting BusinessCard applet", "Status", false);
		status.okButton.setVisible(false);
		status.setVisible(true);
		
		try
		  {
		    haveiButton = true;
		    setTitle(titleHasiButton);
		    if (terminal.selectTerminal())
		      {
			status.setMessage("Olvasom a terminalrol az adatokat");
			terminal.read(); 
			status.setVisible(false);
			idField.setText(terminal.getiButtonId());
			nameField.setText(terminal.getName());
			groupField.setText(terminal.getGroup());
			roadField.setText(terminal.getRoad());
		      } 
		    else
		      {
			idField.setText(terminal.getiButtonId());
			status.setVisible(false);
			System.out.println("Nem lehet a Terminal appletet kivalasztani");
			ErrorDialog ed = new ErrorDialog(this, "Nem lehet a Terminal appletet kivalasztani","Error", true);
			ed.setVisible(true);
		      }
		  }
		catch (SmartCardException e)
		  {
		    status.setVisible(false);
		    System.out.println("Exception: " + e.getMessage());
		    e.printStackTrace();
		    ErrorDialog ed = new ErrorDialog(this, "Nem lehet olvasni","Smartcard Exception", true);
		    ed.setVisible(true);
		  }
		catch (TerminaltFogadException e)
		  {
		    status.setVisible(false);
		    System.out.println("Exception: " + e.getMessage());
		    e.printStackTrace();
		    ErrorDialog ed = new ErrorDialog(this, "Unhandled exception in JiB","BusinessCard Exception", true);
		    ed.setVisible(true);
		  }
	      }
	  }
      }
  }
  
  public void start()
  {
    if (driver == null)
      {
	driver = new Thread(this);
	driver.start();
      }
  }
  
  public void stop()
  {
    if (driver != null)
      {
	driver.stop();
	driver = null;
      }
  }
  
  class SymAction implements java.awt.event.ActionListener
  {
    public void actionPerformed(java.awt.event.ActionEvent event)
    {
      Object object = event.getSource();
      if (object == clearButton)
	clearButton_Action(event);
      else if  (object  == okButton)
	okButton_Action(event);
      else if  (object  == cancelButton)
	cancelButton_Action(event);
    }
  }
  
  void clearFields()
  {
    idField.setText("");
    nameField.setText("");
    groupField.setText("");
    roadField.setText("");
    terminalField.setText("");
  }
  
  void clearButton_Action(java.awt.event.ActionEvent event)
  {
    clearFields();
  }

  void cancelButton_Action(java.awt.event.ActionEvent event)
  {
  }
  
  void okButton_Action(java.awt.event.ActionEvent event)
  {
    String name = nameField.getText();
    String group = groupField.getText();
    String term = terminalField.getText();
    String road = roadField.getText();
    
    // Ki van-e toltve a mezo
    if ( term.length() == 0 || name.length()==0 || group.length()==0 )
      {
	ErrorDialog ed = new ErrorDialog(this, "Kerlek menjél valahova!", "Error", true);
	ed.setVisible(true);
      }
    else
      {
	if ( road.length()==0 )
	  {
	    if ( g.mehet(g.getGyoker().azonosito,term,group) )
	      {
		ErrorDialog ed = new ErrorDialog(this, "Átmehetsz a kapun", "Üzenet", true);
		ed.setVisible(true);
		road=road+term;
		terminalField.setText("");
	      }
	    else
	      {
		ErrorDialog ed = new ErrorDialog(this, "Nem mehetsz át a kapun", "Error", true);
		ed.setVisible(true);
		terminalField.setText("");
		return;
	      }
	  }
	else
	  {
	    int ind=road.lastIndexOf(",");
	    ind=ind==-1?0:ind+1;
	    String honnan=road.substring(ind,road.length());
	    if ( g.mehet(honnan,term,group) )
	      {
		ErrorDialog ed = new ErrorDialog(this, "Átmehetsz a kapun", "Üzenet", true);
		ed.setVisible(true);
		road+=","+term;
		terminalField.setText("");
	      }
	    else
	      {
		ErrorDialog ed = new ErrorDialog(this, "Nem mehetsz át a kapun", "Error", true);
		ed.setVisible(true);
		terminalField.setText("");
		return;
	      }
	  }
	roadField.setText(road);
	try
	  {
	    // Beallitja az adatokat
            terminal.setName(name);
	    terminal.setGroup(group);
            terminal.setRoad(road);

	    // Elmenti az adatokat a Java iButton-ra
            terminal.store();
	  }
         catch (SmartCardException e)
	   {
	     System.out.println("Exception: " + e.getMessage());
	     e.printStackTrace();
	     ErrorDialog ed = new ErrorDialog(this, "Nem lehet menteni","Smartcard Exception", true);
	     ed.setVisible(true);
	   }
         catch (TerminaltFogadException e)
	   {
	     System.out.println("Exception: " + e.getMessage());
	     e.printStackTrace();
	     ErrorDialog ed = new ErrorDialog(this, "Unhandled exception in JiB","BusinessCard Exception", true);
	     ed.setVisible(true);
	   }
      }
  }
}
