package com.example.neo4jtest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.http.AndroidHttpClient;
import android.util.Log;

public class RestAndCypher {
	public static final String SERVER_ROOT_URI = "http://10.0.2.2:7474/";
	AndroidHttpClient httpclient;
	InputStream instream;

	public void executeCypher() {
		HttpResponse response;
		HttpEntity httpEntity;
		BufferedReader reader;
		StringBuilder sb = null;
		JSONObject midj = new JSONObject();
		JSONObject jObject = new JSONObject();
	 	String query = "CREATE (n) RETURN id(n)";
		httpclient = AndroidHttpClient.newInstance("Nuxeo Android Client", null);
	    try {
	    	midj.put("statement", "CREATE (n) RETURN n");
			JSONArray ja = new JSONArray();
			ja.put((Object)jObject);
			jObject.put("statements", (Object)ja);
		} catch (JSONException e1) {
			Log.e("json",e1.toString());
		}
		HttpPost httppost = createPostForJSONObject(jObject,(SERVER_ROOT_URI + "transaction/commit"));
		//new HttpPost((SERVER_ROOT_URI + "db/data/cypher"),createPostForJSONObject);    
		try {
			//httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			// Execute HTTP Post Request
			response = httpclient.execute(httppost);
			Log.e("responseeee",response.toString());
			httpEntity = response.getEntity();
			instream = httpEntity.getContent();
			String result = convertStreamToString(instream);
			Log.e("result2", response.getStatusLine().toString());
			// now you have the string representation of the HTML request
		} catch (Exception e) {
			Log.e("errorr",e.toString());
		}
		finally
		{
			try {
				instream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			httpclient.close();
		}
	}

	private HttpPost createPostForJSONObject(JSONObject params, String url) {
		HttpPost post = new HttpPost(url);
	    post.setEntity(createStringEntity(params));
	    return post;
	}

	private HttpEntity createStringEntity(JSONObject params) {
		StringEntity se = null;
		    try {
		    	Log.e("json params",params.toString());
		    se = new StringEntity(params.toString(), "UTF8");
		    se.setContentType("application/json; charset=UTF-8");
		} catch (Exception e) {
		    Log.e("Failed to create StringEntity", "oh no");
		}
		return se;
	}

	public void connect() {
		String url = SERVER_ROOT_URI;// + "cypher";
		httpclient = AndroidHttpClient.newInstance("");
		// Prepare a request object
		HttpGet request = new HttpGet(url);
		// Execute the request
		HttpResponse response;
		try {
			response = httpclient.execute(request);
			// Examine the response status
			Log.e("response", response.getStatusLine().toString());
			// Get hold of the response entity
			HttpEntity entity = response.getEntity();
			// If the response does not enclose an entity, there is no need
			// to worry about connection release
			if (entity != null) {

				// A Simple JSON Response Read
				instream = entity.getContent();
				String result = convertStreamToString(instream);
				Log.e("result", response.getStatusLine().toString());
				// now you have the string representation of the HTML request
			}
		} catch (Exception e) {
		}
		finally
		{
			try {
				instream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			httpclient.close();
		}
	}

	private String convertStreamToString(InputStream instream) {
		/*
		 * To convert the InputStream to String we use the
		 * BufferedReader.readLine() method. We iterate until the BufferedReader
		 * return null which means there's no more data to read. Each line will
		 * appended to a StringBuilder and returned as String.
		 */
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				instream));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				instream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
}