/*
 *
 * Copyright (c) 2003 Tienshiao Ma
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal 
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE.
 */

package wwpc;

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

/**
 *
 * @author  Tienshiao Ma
 */
public class wwpc extends MIDlet implements CommandListener, ItemStateListener {
    
    private Command exitCommand; // The exit command
    private Command clearCommand;
    private Display display;    // The display for this MIDlet
    private Form main;
    
    private TextField tfFiber;
    private TextField tfFat;
    private TextField tfCalories;
    private StringItem siPoints;
    
    public wwpc() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 2);
        clearCommand = new Command("Clear", Command.SCREEN, 2);        
        
        tfFiber = new TextField("Dietary Fiber Grams:", "", 10, TextField.NUMERIC);
        tfCalories = new TextField("Calories:", "", 10, TextField.NUMERIC);        
        tfFat = new TextField("Total Fat Grams:", "", 10, TextField.NUMERIC);
        siPoints = new StringItem("Points:", "");
        
        main = new Form("Point Calculator");
        
        main.append(tfFiber);
        main.append(tfCalories);        
        main.append(tfFat);
        
        main.append(siPoints);
        
        main.addCommand(exitCommand);
        main.addCommand(clearCommand);
        main.setCommandListener(this);
        main.setItemStateListener(this);        
    }
        
    public void startApp() {
        display.setCurrent(main);
    }
    
    public void pauseApp() {
    }
    
    /**
     * Destroy must cleanup everything not handled by the garbage collector.
     * In this case there is nothing to cleanup.
     */
    public void destroyApp(boolean unconditional) {
    }
    
    /*
     * Respond to commands, including exit
     * On the exit command, cleanup and notify that the MIDlet has been destroyed.
     */
    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        } else if (c == clearCommand) {
            tfFiber.setString("");;
            tfFat.setString("");
            tfCalories.setString("");
        }
    }
    
    public void itemStateChanged(Item item) {
        if (item != siPoints) {
            int fiber = 0;
            try {
                fiber = Integer.parseInt(tfFiber.getString());
            } catch (NumberFormatException nfe) {
                siPoints.setText("");
                display.setCurrent(main);
                return;
            }
            if (fiber > 4) {
                fiber = 4;
            }
            int fat = 0;
            try {
                fat = Integer.parseInt(tfFat.getString());
            } catch (NumberFormatException nfe) {
                siPoints.setText("");
                display.setCurrent(main);
                return;
            }
            int calories = 0;
            try {
                calories = Integer.parseInt(tfCalories.getString());
            } catch (NumberFormatException nfe) {
                siPoints.setText("");
                display.setCurrent(main);
                return;
            }
        
            int points = (12*5*calories) + (50*5*fat) - (50*12*fiber);
            points = (100*points)/(50*12*5);
            if (points < 0) {
                siPoints.setText("");
                display.setCurrent(main);
            } else {
                siPoints.setText(Integer.toString(points / 100) + "." + Integer.toString(points % 100));
                display.setCurrent(main);
            }            
        }
    }
}
