package com.uservoice.uservoicesdk.babayaga;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;

import com.uservoice.uservoicesdk.Session;

public class BabayagaTask extends AsyncTask<String,String,Void> {
	
    private final String event;
	private final Map<String, Object> traits;
	private final Map<String, Object> eventProps;
	private final String uvts;

	public BabayagaTask(String event, String uvts, Map<String,Object> traits, Map<String,Object> eventProps){
		this.event = event;
		this.uvts = uvts;
		this.traits = traits;
		this.eventProps = eventProps;
    }
    
	@SuppressLint("DefaultLocale")
	private static String getTzOffset() {
		int offset = TimeZone.getDefault().getOffset(new Date().getTime());
		return String.format("%s%02d:%02d", offset > 0 ? "-" : "+", (int) Math.floor(Math.abs(offset) / 60.0), (int) Math.abs(offset) % 60);
	}
	
	@Override
	protected Void doInBackground(String... args) {
	    try {
			JSONObject data = new JSONObject();
			if (traits != null && !traits.isEmpty()) {
				JSONObject u = new JSONObject(traits);
				u.put("o", getTzOffset());
				data.put("u", u);
			}
			if (eventProps != null && !eventProps.isEmpty()) {
				data.put("e", eventProps);
			}
			String subdomainId = Session.getInstance().getClientConfig().getSubdomainId();
			StringBuilder url = new StringBuilder(String.format("https://%s/t/%s/%s/%s", Babayaga.DOMAIN, subdomainId, Babayaga.CHANNEL, event));
			if (uvts != null) {
				url.append("/");
				url.append(uvts);
			}
			url.append("/track.js?_=");
			url.append(new Date().getTime());
			url.append("&c=_");
			if (data.length() != 0) {
				url.append("&d=");
				try {
					url.append(URLEncoder.encode(Base64.encodeToString(data.toString().getBytes(), Base64.NO_WRAP), "UTF-8"));
				} catch (UnsupportedEncodingException e) {
					throw new RuntimeException(e);
				}
			}
			
	    	HttpRequestBase request = new HttpGet();
			request.setURI(new URI(url.toString()));
			HttpClient client = new DefaultHttpClient();
            HttpResponse response = client.execute(request);
            HttpEntity responseEntity = response.getEntity();
            StatusLine responseStatus = response.getStatusLine();
            int statusCode = responseStatus != null ? responseStatus.getStatusCode() : 0;
            if (statusCode != 200)
            	return null;
            String body = responseEntity != null ? EntityUtils.toString(responseEntity) : null;
            String payload = body.substring(2, body.length() - 2);
			JSONObject responseData = new JSONObject(payload);
            String uvts = responseData.getString("uvts");
            Babayaga.setUvts(uvts);
	    } catch (Exception e) {
	    	e.printStackTrace();
	    	Log.e("UV", String.format("%s: %s", e.getClass().getName(), e.getMessage()));
		}
	    return null;
	}
}

@@ -181,35 +181,8 @@
 Map;
-%0Aimport java.util.TimeZone;
 %0A%0Aim
@@ -558,48 +558,8 @@
 t;%0A%0A
-import android.annotation.SuppressLint;%0A
 impo
@@ -1120,289 +1120,8 @@
    %0A
-%09@SuppressLint(%22DefaultLocale%22)%0A%09private static String getTzOffset() %7B%0A%09%09int offset = TimeZone.getDefault().getOffset(new Date().getTime());%0A%09%09return String.format(%22%25s%2502d:%2502d%22, offset %3E 0 ? %22-%22 : %22+%22, (int) Math.floor(Math.abs(offset) / 60.0), (int) Math.abs(offset) %25 60);%0A%09%7D%0A%09%0A
 %09@Ov
@@ -1280,22 +1280,21 @@
 %09%09%09%09
-JSONObject u =
+data.put(%22u%22,
  new
@@ -1316,60 +1316,8 @@
 its)
-;%0A%09%09%09%09u.put(%22o%22, getTzOffset());%0A%09%09%09%09data.put(%22u%22, u
 );%0A%09

Remove tzoffset