// package thinlet.steuer;
// import thinlet.*;
/*
Underweight = <18.5 
Normal weight = 18.5-24.9 
Overweight = 25-29.9 
Obesity = BMI of 30 or greater 
*/

import java.util.*; // hashtable
import java.text.*; // decimalformat
import java.awt.*; // color
// import java.io.*;

public class Bmi extends Thinlet {
	public Bmi() throws Exception {
    		add(parse("bmi.xml"));
     		setColors(0xece9d8, 0x000000, 0xf5f4f0,0x919b9a, 0xb0b0b0, 
				0xededed, 0xb9b9b9, 0xff899a, 0xc5c5dd);
		//gray
		//     setColors(0xe6e6e6, 0x000000, 0xffffff,
		//     0x909090, 0xb0b0b0, 0xededed, 0xb9b9b9, 0x89899a, 0xc5c5dd);
		//yellow
		//	setColors(0xeeeecc, 0x000000, 0xffffff,
		//    0x999966, 0xb0b096, 0xededcb, 0xcccc99, 0xcc6600, 0xffcc66);
		//blue
		//	setColors(0x6375d6, 0xffffff, 0x7f8fdd,
		//	0xd6dff5, 0x9caae5, 0x666666, 0x003399, 0xff3333, 0x666666);
		calc();
 	}

	DecimalFormat df = new DecimalFormat("#0.0"); 
	int weight,height;
	double bmi;

	public void calc() {
 		weight = getInteger(find("sl_weight"),"value");
		height = getInteger(find("sl_height"),"value");
		bmi = 10000.0*weight/(height*height);
		setString(find("bmi_label"), "text","Body mass index: "+df.format(bmi));
		setString(find("lheight"), "text",
			String.valueOf(height)+" cm = "+df.format(height/2.54)+" inches");
		setString(find("lweight"), "text",
			String.valueOf(weight)+" kg = "+df.format(weight/0.4536)+" pounds");

		if (bmi < 16.0) { 
			setColor(find("bmi_label"), "foreground", new Color(255, 0, 0)); 
			setString(find("bottom_label"), "text","Underweight - Untergewicht");
		} else if (bmi < 18.4) 	{ 
			setColor(find("bmi_label"), "foreground", 
				new Color((int)(255-255*(bmi-16.0)/2.4), (int)(255*(bmi-16.0)/2.4), 0)); 
			setString(find("bottom_label"), "text","Underweight - Untergewicht");
		} else if (bmi < 24.9) { 
			setColor(find("bmi_label"), "foreground", new Color(0 , 255, 0)); 
			setString(find("bottom_label"), "text","Normal weight - Normalgewicht");
		} else if (bmi < 30.0) { 
			setColor(find("bmi_label"), "foreground", 
				new Color((int)(255*(bmi-24.9)/5.1), (int)(255-255*(bmi-24.9)/5.1), 0)); 
			setString(find("bottom_label"), "text","Overweight - Uebergewicht");
		} else  { 
			setColor(find("bmi_label"), "foreground", new Color(255, 0, 0)); 
			setString(find("bottom_label"), "text","Obesity - Adipositas");
		}
	};

	Hashtable name2component = new Hashtable();
	public Object find(String name) {
		Object component = name2component.get(name);
		if (component == null) {
			component = super.find(name);
			name2component.put(name, component);
		}
		return component;
	}
  
	  public static void main(String[] args) throws Exception {
    		new FrameLauncher("bmi Version 1.0 vom 17.8.2004", new Bmi(), 700,450);
  	}
}

