package it.unipr.mobdev.youtube.remote;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class RESTRequest {

	protected String base_url;
	protected Map<String,String> parameters;
	protected Map<String,String> headers;
	private String method = "GET";

	public RESTRequest(){
		this.base_url = null;
		this.parameters = null;
		this.headers = null;
	}

	public RESTRequest(String base_url){
		this.base_url = base_url;
		this.parameters = new HashMap<String, String>();
		this.headers = new HashMap<String, String>();
	}

	public void isMethodPOST(boolean value)
	{
		if(value == true)
			this.method = "POST";
		else
			this.method = "GET";
	}

	public void isMethodGET(boolean value)
	{
		if(value == true)
			this.method = "GET";
		else
			this.method = "POST";
	}

	public String getBase_url() {
		return base_url;
	}

	public void setBase_url(String base_url) {
		this.base_url = base_url;
	}

	public Map<String, String> getParameters() {
		return parameters;
	}

	public void setParameters(Map<String, String> parameters) {
		this.parameters = parameters;
	}

	public final void addParameter(String key, String value){
		this.parameters.put(key, value);
	}

	public Map<String, String> getHeaders() {
		return this.headers;
	}

	public void setHeaders(Map<String, String> headers) {
		this.headers = headers;
	}

	public final void addHTTPHeader(String header, String value){
		this.headers.put(header, value);
	}

	public boolean hasHTTPHeaders(){
		return this.headers.size() > 0;
	}

	public Iterator<String> headerIterator(){
		return this.headers.keySet().iterator();
	}

	public String getHeaderValue(String header){
		return this.headers.get(header);
	}

	public String requestURL(){
		//base url + for con i parametri hashmap
		StringBuilder sb = new StringBuilder();
		sb.append(this.base_url);
		sb.append("?");
		for(String key: this.parameters.keySet() ){
			String value = this.parameters.get(key);
			sb.append(key + "=" + value + "&");
		}
		sb.deleteCharAt(sb.length()-1);
		return new String(sb);
	}

	protected void signRequest(){}

	public HttpURLConnection getURLConnection() throws IOException{
		URL url = null;

		if(this.method.equals("GET"))
		{
			url = new URL(this.requestURL());
		}
		else
		{
			url = new URL(this.base_url);
		}

		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod(this.method);

		// set the headers for the request if any...
		if (this.hasHTTPHeaders()) {
			Iterator<String> it = this.headerIterator();
			while (it.hasNext()) {			
				String header = it.next();
				String value = this.getHeaderValue(header);
				connection.setRequestProperty(header, value);
			}
		}

		// uncomment this if you want to write output to this url
		// connection.setDoOutput(true);
		// give it 15 seconds to respond
		connection.setReadTimeout(60 * 1000);
		return connection;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((this.requestURL() == null) ? 0 : this.requestURL().hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RESTRequest other = (RESTRequest) obj;
		if (this.requestURL() == null) {
			if (other.requestURL() != null)
				return false;
		} else if (!this.requestURL().equals(other.requestURL()))
			return false;
		return true;
	}

	public String getPOSTBody() {
		//base url + for con i parametri hashmap
		StringBuilder sb = new StringBuilder();
		for(String key: this.parameters.keySet() ){
			String value = this.parameters.get(key);
			sb.append(key + "=" + value + "&");
		}
		sb.deleteCharAt(sb.length()-1);
		return new String(sb);
	}

}
