/*
 * Copyright (c) 2008 Keith A. Jaska
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package geriatricassistant.utils;

import geriatricassistant.GeriatricAssistantApplication;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 * The Class WSUtilities. This class provides wrapper utilities for the Yahoo!
 * Maps API and the Tumblr API.
 * 
 * @author Keith A. Jaska
 */
public class WSUtilities {

	/**
	 * Generate a Yahoo Map image based on the latitude and longitude.
	 * 
	 * @param latitude
	 *            the latitude
	 * @param longitude
	 *            the longitude
	 * 
	 * @return the URL string of the generated image.
	 */
	public static String generateYahooMap(double latitude, double longitude) {
		String imageURL = "";
		URL url;
		try {
			String yahooAppId = GeriatricAssistantApplication.getProperty(
					"yahooAppId", null);
			String yahooMapServiceURL = GeriatricAssistantApplication
					.getProperty("yahooMapServiceURL", null);
			url = new URL(yahooMapServiceURL + "?appid=" + yahooAppId
					+ "&latitude=" + latitude + "&longitude=" + longitude
					+ "&zoom=1");
			URLConnection conn = url.openConnection();

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

			try {

				DocumentBuilder db = dbf.newDocumentBuilder();

				Document doc = db.parse(conn.getInputStream());
				Element element = doc.getDocumentElement();
				imageURL = element.getFirstChild().getNodeValue();

			} catch (ParserConfigurationException pce) {
				pce.printStackTrace();
			} catch (SAXException se) {
				se.printStackTrace();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return imageURL;

	}

	/**
	 * Post image to tumblr.
	 * 
	 * @param imageSource
	 *            the URL string for the Map image.
	 * @param latitude
	 *            the latitude
	 * @param longitude
	 *            the longitude
	 * @param date
	 *            the date
	 * 
	 * @return the int which represents the reponse code of the tumblr post.
	 */
	public static int postImageToTumblr(String imageSource, double latitude,
			double longitude, Date date) {
		URL url;
		String tumblrPostURL = GeriatricAssistantApplication.getProperty(
				"tumblrPostURL", null);
		String tumblrEmail = GeriatricAssistantApplication.getProperty(
				"tumblrEmail", null);
		String tumblrPassword = GeriatricAssistantApplication.getProperty(
				"tumblrPassword", null);
		String userInfo = GeriatricAssistantApplication.getProperty("userInfo",
				null);
		try {
			url = new URL(tumblrPostURL);
			String data = "email="
					+ tumblrEmail
					+ "&password="
					+ tumblrPassword
					+ "&type=photo&source="
					+ imageSource
					+ "&caption="
					+ userInfo
					+ " appears to have fallen at the following location: Latitude: "
					+ latitude + " Longitude: " + longitude + "! - "
					+ date.toString() + "&generator=Geriatric Assistant";
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setAllowUserInteraction(false);
			conn.setDoOutput(true);
			conn.setRequestProperty("Content-type",
					"application/x-www-form-urlencoded");
			conn.setRequestProperty("Content-length", Integer.toString(data
					.length()));
			OutputStream ost = conn.getOutputStream();

			ost.write(data.getBytes());
			ost.close();

			return conn.getResponseCode();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;

	}

}