import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.*;

/**
*
*
*
 */

public class PricePlans extends MIDlet implements CommandListener {
	private static final Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
        private static final Command CMD_NEXT = new Command("Next", Command.SCREEN, 2);
	private static final Command CMD_BACK = new Command("Back", Command.BACK, 1);
        private static final Command CMD_OK = new Command("OK", Command.SCREEN, 2);

	private DataBase base;
	private Display display;
    	private Form welcomeScreen;
    	private List mainMenu;
	private List listOperators;
	private List listPlans;
	private Form formPlan;
	private Form formScalar;
	private Form formHelp;
	
	private int nSelOperator;
	private double[] weights;
	
	private Image imageLogo;
    	private Image[] imageArray = null;
	
	private TextField field1, field2, field3, field4, field5, field6, field7, field8, field9;

    	public PricePlans() 
	{
        	display = Display.getDisplay(this);
	    	try 
		{
			// load the duke image to place in the image array
			imageLogo = Image.createImage("/Logo.png");
			Image imagePlans = Image.createImage("/Plans.png");
			Image imagePareto = Image.createImage("/Pareto.png");
			Image imageScalar = Image.createImage("/Scalar.png");
			Image imageHelp = Image.createImage("/Help.png");
			// these are the images and strings for the choices.
			imageArray = new Image[] { imagePlans, imagePareto, imageScalar, imageHelp };
	    	} catch (java.io.IOException err) {
			// ignore the image loading failure the application can recover.
	    	}

            String[] stringArray = { "Show All Plans", "Use Pareto Optimization", "Use Scalar Optimization", "Help"};
            mainMenu = new List("Menu:", Choice.IMPLICIT, stringArray, imageArray);
            mainMenu.addCommand(CMD_EXIT);
            mainMenu.setCommandListener(this);
    	}

    	protected void startApp() 
	{
        	if (display.getCurrent() == null) 
		{
       			// startApp called for the first time
            		showWelcomeScreen();
        	}
    	}

	public void showOperatorList()
	{
	    int nCount = base.count;
	    String[] stringArray = new String[nCount];
            for(int i=0; i<nCount; i++)
	    {
            	stringArray[i] = base.operators[i].name;
	    }
	    listOperators = new List("Select Operator:", Choice.IMPLICIT, stringArray, null);
            listOperators.addCommand(CMD_BACK);
    	    listOperators.setCommandListener(this);
	    display.setCurrent(listOperators);		
	}

	public void showPlanList(int nIndex)
	{
	    int nCount = base.operators[nIndex].planCount;
	    String[] stringArray = new String[nCount];
            for(int i=0; i<nCount; i++)
	    {
            	stringArray[i] = base.operators[nIndex].plans[i].name;
	    }
	    String sTitle = base.operators[nIndex].name + " Plans:";
	    listPlans = new List(sTitle, Choice.IMPLICIT, stringArray, null);
            listPlans.addCommand(CMD_BACK);
    	    listPlans.setCommandListener(this);
	    display.setCurrent(listPlans);		
	}

	public void showPlan(int nIndex)
	{
	    formPlan = new Form(base.operators[nSelOperator].plans[nIndex].name+":");
            StringItem item = new StringItem("Monthly charge: ", base.operators[nSelOperator].plans[nIndex].param[0] + " LTL");
            formPlan.append(item);
	    formPlan.append("\nDaytime Calls");
            item = new StringItem("To main network: ", base.operators[nSelOperator].plans[nIndex].param[1] + " LTL");
            formPlan.append(item);
            item = new StringItem("To other networks: ", base.operators[nSelOperator].plans[nIndex].param[3] + " LTL");
            formPlan.append(item);
            item = new StringItem("To TEO network: ", base.operators[nSelOperator].plans[nIndex].param[5] + " LTL");
            formPlan.append(item);
	    formPlan.append("\nNighttime Calls");	   
            item = new StringItem("To main network: ", base.operators[nSelOperator].plans[nIndex].param[2] + " LTL");
            formPlan.append(item);
            item = new StringItem("To other networks: ", base.operators[nSelOperator].plans[nIndex].param[4] + " LTL");
            formPlan.append(item);
            item = new StringItem("To TEO network: ", base.operators[nSelOperator].plans[nIndex].param[6] + " LTL");
            formPlan.append(item); 
	    formPlan.append("\nSMS\n");
            item = new StringItem("To Lithuanian networks: ", base.operators[nSelOperator].plans[nIndex].param[7] + " LTL");
            formPlan.append(item);
            item = new StringItem("To foreign networks: ", base.operators[nSelOperator].plans[nIndex].param[8] + " LTL");
            formPlan.append(item);	 
            formPlan.addCommand(CMD_BACK);
    	    formPlan.setCommandListener(this);
	    display.setCurrent(formPlan);		
	}

	public void showScalarForm()
	{
	    formScalar = new Form("Enter Weigths:");
            field1 = new TextField("Monthly charge:", "1", 4, TextField.DECIMAL);
	    formScalar.append(field1);
	    formScalar.append("Daytime Calls");
            field2 = new TextField("To main network:", "1", 4, TextField.DECIMAL);
	    formScalar.append(field2);
            field3 = new TextField("To other networks:", "1", 4, TextField.DECIMAL);
	    formScalar.append(field3);
            field4 = new TextField("To TEO network:", "1", 4, TextField.DECIMAL);
	    formScalar.append(field4);
	    formScalar.append("Nighttime Calls");
            field5 = new TextField("To main network:", "1", 4, TextField.DECIMAL);
	    formScalar.append(field5);
            field6 = new TextField("To other networks:", "1", 4, TextField.DECIMAL);
	    formScalar.append(field6);
            field7 = new TextField("To TEO network:", "1", 4, TextField.DECIMAL);
	    formScalar.append(field7);
	    formScalar.append("SMS");
            field8 = new TextField("To Lithuanian networks:", "1", 4, TextField.DECIMAL);
	    formScalar.append(field8);
            field9 = new TextField("To foreign networks:", "1", 4, TextField.DECIMAL);
	    formScalar.append(field9);
	    formScalar.addCommand(CMD_BACK);
	    formScalar.addCommand(CMD_OK);
	    formScalar.setCommandListener(this);
	    display.setCurrent(formScalar);		
	}

	public void showHelpForm()
	{
	    formHelp = new Form("Help:");
	    formHelp.append("MIDlet PricePlans v1.0");
            formHelp.append("Copyright (c) 2007 \nDarius Matijosaitis IT-4/1 VGTU \nUpdated by Gintaras Juozapavicius IT-4/2 VGTU\n");
            StringItem item = new StringItem("How to use: ", "");
            formHelp.append(item);
	    formHelp.append("1.'Show All Plans' - Shows all mobile price plans stored on" +
		" data base. ");
	    formHelp.append("2. 'Use Pareto Optimization' - Shows mobile price plan list" +
		" optimized with Pareto algorithm.");
	    formHelp.append("3. 'Use Scalar Optimization' - Shows mobile price plan list" +
		" optimized with scalar optimization algorithm.");


            formHelp.addCommand(CMD_BACK);
    	    formHelp.setCommandListener(this);
	    display.setCurrent(formHelp);
	}

	public void setWeights()
	{
		weights = new double[9];
		weights[0] = Double.parseDouble(field1.getString());
		weights[1] = Double.parseDouble(field2.getString());
		weights[2] = Double.parseDouble(field3.getString());
		weights[3] = Double.parseDouble(field4.getString());
		weights[4] = Double.parseDouble(field5.getString());
		weights[5] = Double.parseDouble(field6.getString());
		weights[6] = Double.parseDouble(field7.getString());
		weights[7] = Double.parseDouble(field8.getString());
		weights[8] = Double.parseDouble(field9.getString());	
	}

    	public void showWelcomeScreen() 
	{
        	if (welcomeScreen == null) 
		{
            		Item[] items =
                	new Item[] { new ImageItem(null, imageLogo, Item.LAYOUT_CENTER, "logo") };
            		welcomeScreen = new Form("PricePlans v1.0", items);
			welcomeScreen.addCommand(CMD_NEXT);
			welcomeScreen.addCommand(CMD_EXIT);
            		welcomeScreen.setCommandListener(this);
        	}

        	display.setCurrent(welcomeScreen);
    	}

	public void commandAction(Command c, Displayable d) 
	{
		if (d.equals(mainMenu)) 
		{
		    	if (c == List.SELECT_COMMAND) 
			{
		            switch (((List)d).getSelectedIndex()) {
		            case 0:
				base = new DataBase();
		                showOperatorList();

		                break;
		            case 1:
				base = new DataBase();
				base.OptimizePareto();
		                showOperatorList();

		                break;
		            case 2:
				base = new DataBase();
		                showScalarForm();

		                break;
		            case 3:
		                showHelpForm();

		                break;
		            }
		        }
		}
		if (d.equals(listOperators)) 
		{
		    	if (c == List.SELECT_COMMAND) 
			{
			    int nNr = ((List)d).getSelectedIndex();
			    showPlanList(nNr);
			    nSelOperator = nNr;
		        }

		   	if (c == CMD_BACK) 
			{
		    	    display.setCurrent(mainMenu);
			}
		}

		if (d.equals(listPlans)) 
		{
		    	if (c == List.SELECT_COMMAND) 
			{
			    int nNr = ((List)d).getSelectedIndex();
			    showPlan(nNr);
		        }

		   	if (c == CMD_BACK) 
			{
		    	    display.setCurrent(listOperators);
			}
		}

		if (d.equals(formPlan)) 
		{
		   	if (c == CMD_BACK) 
			{
		    	    display.setCurrent(listPlans);
			}
		}

		if (d.equals(formHelp)) 
		{
		   	if (c == CMD_BACK) 
			{
		    	    display.setCurrent(mainMenu);
			}
		}

		if (d.equals(formScalar)) 
		{
		   	if (c == CMD_BACK) 
			{
		    	    display.setCurrent(mainMenu);
			}
		   	if (c == CMD_OK) 
			{ 
			    setWeights();
		    	    base.OptimizeScalar(weights);
		            showOperatorList();
			}
		}

		if (c == CMD_NEXT) {
		    display.setCurrent(mainMenu);
		}

	
		if (c == CMD_EXIT) {
		    destroyApp(false);
		    notifyDestroyed();
		}
    	}

    protected void destroyApp(boolean unconditional) {
    }

    protected void pauseApp() {
    }
}
