package com.msi.manning.weather.data;

import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class YWeatherXmlPullParser {
	
    private static final String YLOC = "yweather:location";
    private static final String YWIND = "yweather:wind";
    private static final String YATMO = "yweather:atmosphere";
    private static final String YASTRO = "yweather:astronomy";
    private static final String YCOND = "yweather:condition";
    private static final String YFCAST = "yweather:forecast";
	
	static private XmlPullParser xpp;
	
	public static WeatherRecord parseWeather(InputStream xml) throws Exception {
		int forecastCount = 0;
		WeatherRecord r = new WeatherRecord();
		
		xpp = XmlPullParserFactory.newInstance().newPullParser();
	    xpp.setInput(xml, "UTF-8");
	    
	    int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
            case XmlPullParser.START_TAG:
                String tagname = xpp.getName();
                if (tagname.equalsIgnoreCase(YLOC)) {
                	r.setCity(xpp.getAttributeValue(null, "city"));
                	r.setRegion(xpp.getAttributeValue(null, "region"));
                	r.setCountry(xpp.getAttributeValue(null, "country"));
                }
                if (tagname.equalsIgnoreCase(YWIND)) {
                	r.setWindChill(xpp.getAttributeValue(null, "chill"));
                	int windDirectionDegrees = Integer.parseInt(xpp.getAttributeValue(null, "direction"));
                	r.setWindDirection(YWeatherHandler.convertDirection(windDirectionDegrees));
                	r.setWindSpeed(Integer.parseInt(xpp.getAttributeValue(null, "speed")));
                }
                if (tagname.equals(YATMO)) {
                    r.setHumidity(Integer.parseInt(xpp.getAttributeValue(null, "humidity")));
                    r.setVisibility(Integer.parseInt(xpp.getAttributeValue(null, "visibility")));
                    r.setPressure(Double.parseDouble(xpp.getAttributeValue(null, "pressure")));
                    String pressureState = xpp.getAttributeValue(null, "rising");
                    if (pressureState.equals("0")) {
                        r.setPressureState(WeatherRecord.PRESSURE_STEADY);
                    } else if (pressureState.equals("1")) {
                        r.setPressureState(WeatherRecord.PRESSURE_FALLING);
                    } else if (pressureState.equals("2")) {
                        r.setPressureState(WeatherRecord.PRESSURE_RISING);
                    }
                }
                if (tagname.equals(YASTRO)) {
                    r.setSunrise(xpp.getAttributeValue(null, "sunrise"));
                    r.setSunset(xpp.getAttributeValue(null, "sunset"));
                }

                if (tagname.equals(YCOND)) {
                    r.setTemp(Integer.parseInt(xpp.getAttributeValue(null, "temp")));
                    int code = Integer.parseInt(xpp.getAttributeValue(null, "code"));
                    WeatherCondition cond = WeatherCondition.getWeatherCondition(code);
                    r.setCondition(cond);
                    r.setDate(xpp.getAttributeValue(null, "date"));
                }

                if (tagname.equals(YFCAST)) {
                    if (forecastCount < 2) {
                        WeatherForecast forecast = new WeatherForecast();
                        forecast.setDate(xpp.getAttributeValue(null, "date"));
                        forecast.setDay(xpp.getAttributeValue(null, "day"));
                        forecast.setHigh(Integer.parseInt(xpp.getAttributeValue(null, "high")));
                        forecast.setLow(Integer.parseInt(xpp.getAttributeValue(null, "low")));
                        int code = Integer.parseInt(xpp.getAttributeValue(null, "code"));
                        WeatherCondition cond = WeatherCondition.getWeatherCondition(code);
                        forecast.setCondition(cond);
                        r.getForecasts()[forecastCount] = forecast;
                    }
                    forecastCount++;
                }
                
                break;



            default:
                break;
            }
            eventType = xpp.next();
        }
	    
	    return r;
	}
	
}
