package client2;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;
import java.net.*;

import java.rmi.*;
import java.rmi.server.*;

import java.io.FileInputStream.*;
import java.io.RandomAccessFile.*;
import java.io.File;

import java.util.*;
import java.text.*;

import server.*;

class RMIClient2 extends JFrame
		 implements ActionListener {

   private JLabel creditCard, custID, apples, peaches, pears, total, cost, clicked;
   private JButton view, reset;
   private JPanel panel;
   private JTextArea creditNo, customerNo, applesNo, peachesNo, pearsNo, itotal, icost; 
   private static Send send;
   private String customer;
   private Set s = null;

//Internationalization variables
   private static Locale currentLocale;
   private static ResourceBundle messages;
   private static String language, country;
   private NumberFormat numFormat;

   private RMIClient2(){ //Begin Constructor

     setTitle(messages.getString("title"));

//Create labels
     creditCard = new JLabel(messages.getString("card"));
     custID = new JLabel(messages.getString("customer"));
     apples = new JLabel(messages.getString("apples"));
     peaches = new JLabel(messages.getString("peaches"));
     pears = new JLabel(messages.getString("pears"));
     total = new JLabel(messages.getString("items"));
     cost = new JLabel(messages.getString("cost"));

//Create text areas
     creditNo = new JTextArea();
     customerNo = new JTextArea();
     applesNo = new JTextArea();
     peachesNo = new JTextArea();
     pearsNo = new JTextArea();
     itotal = new JTextArea();
     icost = new JTextArea();

//Create buttons
     view = new JButton(messages.getString("view"));
     view.addActionListener(this);

     reset = new JButton(messages.getString("reset"));
     reset.addActionListener(this);

//Create panel for 2-column layout
//Set white background color
     panel = new JPanel();
     panel.setLayout(new GridLayout(0,2));
     panel.setBackground(Color.white);

//Add components to panel columns
//going left to right and top to bottom
     getContentPane().add(panel);
     panel.add(creditCard);
     panel.add(creditNo);

     panel.add(custID);
     panel.add(customerNo);

     panel.add(apples);
     panel.add(applesNo);

     panel.add(peaches);
     panel.add(peachesNo);

     panel.add(pears);
     panel.add(pearsNo);

     panel.add(total);
     panel.add(itotal);

     panel.add(cost);
     panel.add(icost);

     panel.add(view);
     panel.add(reset);

   } //End Constructor

//Create list of customer IDs
  private void customerList(String custID){
    if(s==null){
	s = new HashSet();
	s.add(custID);
    }else{
	s.add(custID);
    }
  }

//Print customer IDs
  private void print(){
    if(s!=null){
      Iterator it = s.iterator();
      while(it.hasNext()){
        try{
          String customer = (String)it.next();
          System.out.println(customer);
        }catch (java.util.NoSuchElementException e){
          System.out.println("No data available");
        }
      }
    }else{
      System.out.println("No customer IDs available");
    }
  }

  public void actionPerformed(ActionEvent event){
     Object source = event.getSource();
     String unit, i;
     double cost;
     Double price;
     int items;
     Integer itms;
     DataOrder order = new DataOrder();

//If View button pressed
//Get data from server and display it
     if(source == view){
        try{
          order  = send.getOrder();
	  creditNo.setText(order.cardnum);
	  customerNo.setText(order.custID);
//Get customer ID and add to list
          customerList(order.custID);
          applesNo.setText(order.apples);
          peachesNo.setText(order.peaches);
          pearsNo.setText(order.pears);

//Create number formatter
          numFormat = NumberFormat.getNumberInstance(currentLocale);

	  price = new Double(order.icost);
	  unit = numFormat.format(price);
	  icost.setText(unit);

	  itms = new Integer(order.itotal);
	  i = numFormat.format(order.itotal);
	  itotal.setText(i);
	} catch (java.rmi.RemoteException e) {
	  System.out.println("Cannot access data in server");	
	}
//Print
	print();
     }

//If Reset button pressed
//Clear all fields
     if(source == reset){
	creditNo.setText("");
	customerNo.setText("");
	applesNo.setText("");
	peachesNo.setText("");	
	pearsNo.setText("");
	itotal.setText("");
	icost.setText("");
     }
  }
  
  public static void main(String[] args){
    if(args.length != 3) {
      language = new String("en");
      country = new String ("US");
      System.out.println("English");
    }else{
      language = new String(args[1]);
      country = new String(args[2]);
      System.out.println(language + country);
    }

    currentLocale = new Locale(language, country);
    messages = ResourceBundle.getBundle("client2" + File.separatorChar + "MessagesBundle", currentLocale);

    WindowListener l = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };

    RMIClient2 frame = new RMIClient2();
    frame.addWindowListener(l);
    frame.pack();
    frame.setVisible(true);

    if(System.getSecurityManager() == null) {
      System.setSecurityManager(new RMISecurityManager());
    }

    try {
      String name = "//" + args[0] + "/Send";
      send = ((Send) Naming.lookup(name));
    } catch (java.rmi.NotBoundException e) {
      System.out.println(messages.getString("nolookup"));
    } catch(java.rmi.RemoteException e){
      System.out.println(messages.getString("nolookup"));
    } catch(java.net.MalformedURLException e) {
      System.out.println(messages.getString("nolookup"));
    }

  }
}
