package edu.wpi.mails;
import java.awt.BorderLayout;

import javax.swing.*;
import java.util.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MailsBroker {
	
	private JFrame window;
	private JTextArea txtStatus;

	private String[] areas = { "Canada", "Mexico", "United Kingdom", "Brazil", "China",
			"Japan", "Australia", "Germany", "Russia", "India" };
	
	private HashMap<String,Integer> areasMap;
	
	public MailsBroker() {
		window = new JFrame("Mails Rules Simulator");
		window.setSize(600, 300);
		window.setLocation(0, 350);
		{
			txtStatus = new JTextArea();
			window.getContentPane().add(txtStatus, BorderLayout.CENTER);
			txtStatus.setEditable(false);
		}
		window.setVisible(true);
		window.setPreferredSize(new java.awt.Dimension(600, 300));
		txtStatus.setText("Mails Broker Started.");
		
		loadAreas();
	}
	
	private void loadAreas() {
		areasMap = new HashMap<String,Integer>();
		areasMap.put("Canada", 1);
		areasMap.put("Mexico", 1);
		areasMap.put("United Kingdom", 1);
		areasMap.put("Brazil", 1);
		areasMap.put("China", 1);
		areasMap.put("Japan", 1);
		areasMap.put("Australia", 1);
		areasMap.put("Germany", 1);
		areasMap.put("Russia", 1);
		areasMap.put("India", 1);
		areasMap.put("South Africa", 0);
		areasMap.put("Argentina", 0);
		areasMap.put("Ireland", 0);
		areasMap.put("Austria", 0);
		areasMap.put("Spain", 0);
		areasMap.put("Indonesia", 0);
		areasMap.put("Egypt", 0);
		areasMap.put("Bermuda", 0);
		
	}

	public ArrayList<String> getTopTenDestinations() {
		txtStatus.setText(txtStatus.getText() + "\nGetting top 10 destinations");		
		ArrayList<String> retval = new ArrayList<String> ( );
		Map.Entry<String,Integer> e;
		for (Iterator<Map.Entry<String,Integer>> i = areasMap.entrySet().iterator(); i.hasNext(); ) {
			e = i.next();
			if (e.getValue() == 1) {
				retval.add(e.getKey());
			}
		}
		txtStatus.setText(txtStatus.getText() + "\nReturning top 10 destinations");
		return retval;
	}

	public ArrayList<String> findDestinationMatches(String matchString) {
		txtStatus.setText(txtStatus.getText() + "\nFinding destinations matching '" + matchString + "'");		
		ArrayList<String> retval = new ArrayList<String> ( );
		String area;
		for (Iterator<String> i=areasMap.keySet().iterator(); i.hasNext();) {
			area = i.next();
			if (area.contains(matchString)) {
				retval.add(area);
			}
		}		
		txtStatus.setText(txtStatus.getText() + "\nReturning matching destinations");
		return retval;
	}

	
	public boolean validateDestination(String dest) {
		for (int i = 0; i<10; i++) {
			if (dest.equals(areas[i])) return true;
		}
		return false;
	}
	
	public ArrayList getPrimaryServices(int weight, String destination) {
		ArrayList retval = new ArrayList();
		txtStatus.setText(txtStatus.getText() + "\nDestination: " + destination + "\nWeight: " + weight);
		if (weight <= 1) retval.add("First Class");
		if (! destination.equals("China")) retval.add("Overnight");
		retval.add("2 Day");
		retval.add("Ground");
		txtStatus.setText(txtStatus.getText() + "\nReturned possible services to " + destination 
				+ " for packages weighing " + weight);
		return retval;
	}
	
	public boolean selectPrimaryService(String destination, int weight, String service) {
		boolean retval = true;
		if (destination == "China" && service == "Overnight") retval = false;
		if (weight > 1 && service == "First Class") retval = false;
		if (retval) txtStatus.setText(txtStatus.getText() + "\nService selection accepted"); 
			else txtStatus.setText(txtStatus.getText() + "\nSerivce selection rejected");
		return retval;
	}
	
	public ArrayList getAddOnServices(int weight, String destination, String primary) {
		ArrayList retval = new ArrayList();
		txtStatus.setText(txtStatus.getText() + "\nDestination: " + destination + "\nWeight: " + weight + "\nPrimary Svc: " + primary);
		if (! primary.equals("First Class")) retval.add("Insurance");
		if (weight > 1) retval.add("Signature Confirmation");
		if (! destination.equals("India")) retval.add("Return receipt");
		txtStatus.setText(txtStatus.getText() + "\nReturned possible services to " + destination 
				+ " for packages weighing " + weight + " using " + primary);
		return retval;
	}
	
	public boolean selectAddOnService(String destination, int weight, String primary, String addon) {
		boolean retval = true;
		if (primary.equals("First Class") && addon.equals("Insurance")) retval = false;
		if (destination.equals("India") && addon == "Return Receipt") retval = false;
		if (weight > 1 && addon.equals("Signature Confirmation")) retval = false;
		if (retval) txtStatus.setText(txtStatus.getText() + "\nService selection accepted"); 
			else txtStatus.setText(txtStatus.getText() + "\nSerivce selection rejected");
		return retval;
	}

	public String saveTransaction(String origin, String dest, int weight, String primary, ArrayList addons) {
		txtStatus.setText(txtStatus.getText() + "\nSending a " + weight + " kg package from " + origin + " to " + dest);
		txtStatus.setText(txtStatus.getText() + "\nSending using " + primary + " and " + addons);
		txtStatus.setText(txtStatus.getText() + "\nDone sending package");
		return "done";
	}
}
