// weather channel
// demonstrates handling live updates
// Laura Lemay

import java.awt.*;
import java.net.*;
import java.util.*;
import java.io.IOException;
import marimba.gui.*;
import marimba.channel.*;
import marimba.io.*;

public class WeatherChannel extends ApplicationPlayerFrame {

/* data file */
String src = "data.txt";

/* conditions */
static final int SUNNY = 0;
static final int PARTLY_CLOUDY = 1;
static final int CLOUDY = 2;
static final int RAINY = 3;
static final int SNOWY = 4;

/* overide start to read in the initial data */
public void start() {
  super.start();
  setTitle("The Weather Channel");
  loadData();
}

/* notifyAvailable is called just before the data is
actually installed.  Its like the tuner is asking
permission.  installData() says OK, go ahead.
Here's we'll restrict the install to the data file
cause that's all we care about.  All other new data
will get installed when the channel restarts */
public void notifyAvailable(String dir) {
	System.out.println("arg:" + dir);
	context.installData(src);
}

/* notifyInstall is called after the data has been installed.
Here's where we process the new stuff. */
public void notifyInstall(String dir) {
	loadData();
}

/* read in the data.txt file, which has the current weather values
we'll display.  Format is
city condition temperature humidity
*/
public void loadData() {
	try {
		URL base = new URL(context.getBase(), src);
		String str;
		WeatherData data;
		FastInputStream in = new FastInputStream(base.openStream());

		while ((str = in.readLine()) != null) {
			str.trim();
			data = new WeatherData();
			if ((str.length() > 0) && !str.startsWith("#")) {
				StringTokenizer st = new StringTokenizer(str);
				data.city = st.nextToken();
				data.conditions = Integer.parseInt(st.nextToken());
				data.temp = st.nextToken();
				data.humidity = st.nextToken();

				fillWidgets(data);
			}
		}
		in.close();
	}
	catch (MalformedURLException e) { 
		e.printStackTrace();
	}
	catch (IOException e) {
		e.printStackTrace();
	}
}

/* fill in widget data.  This is so not efficient (kludge!  kludge!)
This app would be much better implemented with
each city as a custom bongo widget; that'd let them get 
reused here as well as dynamically built. */
public void fillWidgets(WeatherData data) {
	String cityimg ="";
	String citytemp = "";
	String cityhum	= "";

	if (data.city.equals("SF")) {
		cityimg = "SFimg";
		citytemp = "SFtemp";
		cityhum = "SFhum";
	} else if (data.city.equals("NY")) {
		cityimg = "NYimg";
		citytemp = "NYtemp";
		cityhum = "NYhum";
	} else if (data.city.equals("L")) {
		cityimg = "Limg";
		citytemp = "Ltemp";
		cityhum = "Lhum";
	}

	switch (data.conditions) {
		case SUNNY:
			util.setValue(cityimg, "images/sunny.gif");
			break;
		case PARTLY_CLOUDY:
			util.setValue(cityimg, "images/pcloud.gif");
			break;
		case CLOUDY:
			util.setValue(cityimg, "images/cloudy.gif");
			break;
		case RAINY:
			util.setValue(cityimg, "images/rainy.gif");
			break;
		case SNOWY:
			util.setValue(cityimg, "images/snow.gif");
			break;
	}

	util.setValue(citytemp, data.temp);
	util.setValue(cityhum, data.humidity);

}

}
