package com.msi.manning.weather.data;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class YWeatherXmlDOMParser {
	
    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();
		
		Document document = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        
        try {
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inputSource = new InputSource(xml);
            document = db.parse(inputSource);
        
            Element element = (Element) document.getElementsByTagName(YLOC).item(0);
            r.setCity(element.getAttribute("city"));
        	r.setRegion(element.getAttribute("region"));
        	r.setCountry(element.getAttribute("country"));
        	
        	element = (Element) document.getElementsByTagName(YWIND).item(0);
        	r.setWindChill(element.getAttribute("chill"));
        	int windDirectionDegrees = Integer.parseInt(element.getAttribute("direction"));
        	r.setWindDirection(YWeatherHandler.convertDirection(windDirectionDegrees));
        	r.setWindSpeed(Integer.parseInt(element.getAttribute("speed")));
        
        	element = (Element) document.getElementsByTagName(YATMO).item(0);
        	r.setHumidity(Integer.parseInt(element.getAttribute("humidity")));
            r.setVisibility(Integer.parseInt(element.getAttribute("visibility")));
            r.setPressure(Double.parseDouble(element.getAttribute("pressure")));
            String pressureState = element.getAttribute("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);
            }
        	
            element = (Element) document.getElementsByTagName(YASTRO).item(0);
            r.setSunrise(element.getAttribute("sunrise"));
            r.setSunset(element.getAttribute("sunset"));

            element = (Element) document.getElementsByTagName(YCOND).item(0);
            r.setTemp(Integer.parseInt(element.getAttribute("temp")));
            int code = Integer.parseInt(element.getAttribute("code"));
            WeatherCondition cond = WeatherCondition.getWeatherCondition(code);
            r.setCondition(cond);
            r.setDate(element.getAttribute("date"));
            
            NodeList nodeList = document.getElementsByTagName(YFCAST);
            WeatherForecast[] forecasts = new WeatherForecast[nodeList.getLength()];
            for(int i = 0; i < nodeList.getLength(); i++)
            {
            	element = (Element) nodeList.item(i);
                WeatherForecast forecast = new WeatherForecast();
                forecast.setDate(element.getAttribute("date"));
                forecast.setDay(element.getAttribute("day"));
                forecast.setHigh(Integer.parseInt(element.getAttribute("high")));
                forecast.setLow(Integer.parseInt(element.getAttribute("low")));
                code = Integer.parseInt(element.getAttribute("code"));
                cond = WeatherCondition.getWeatherCondition(code);
                forecast.setCondition(cond);
                //r.getForecasts()[i] = forecast;
                forecasts[i] = forecast;
            }
            r.setForecasts(forecasts);
        
        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }        
	    
	    return r;
	}
	
}
