package o2mc.io.dimmldependency.viewMapper;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import o2mc.io.dimmldependency.Datastreams.DeviceName;

/**
 * Created by nickyromeijn on 18/02/16.
 */
public class DatastreamsViewmapperPost extends AsyncTask<DataStore, Void, Void> {

    private String environmentID = "nromeijn";
    private String url = "https://baltar-dev.dimml.io/flow/code.js?dimml.concept=//"+environmentID+"@datastreams.viewmapper/Global";

    Activity activity;

    public DatastreamsViewmapperPost(Activity a){
        activity=a;
    }

    @Override
    protected Void doInBackground(DataStore... params) {
        JSONObject root = new JSONObject();
        JSONObject param = params[0].getMap();

        try {
            root.put("info", new JSONObject()

                    .put("id", activity.getPackageName())

                    .put("resolution", new JSONObject()

                            .put("width", activity.getWindow().getWindowManager().getDefaultDisplay().getWidth())

                            .put("height", activity.getWindow().getWindowManager().getDefaultDisplay().getHeight())
                    )

                    .put("operatingSystem", "Android "+Build.VERSION.RELEASE)

                    .put("device", DeviceName.getDeviceName())

                    .put("screen", params[0].getBase64())
            );

            root.put("activities", param);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String data = root.toString();

        Log.e("data",data);

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        List<NameValuePair> postParams = new ArrayList<NameValuePair>(1);
        postParams.add(new BasicNameValuePair("data", data));

        try {
            httppost.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8"));
            HttpResponse response = httpclient.execute(httppost);

            Log.e("POSTING",url);
            Log.e("STATUS ","Status line : "+ response.getStatusLine().toString());
//            Log.e("RESULT", getHTTPResult(response));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

    private String getHTTPResult(HttpResponse response) throws IOException {
        String result = "";
        InputStream stream = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(stream));

        String line;
        while ((line = in.readLine()) != null) {
            result = result + line;
        }

        return result;
    }


}
