package com.jcf.sportpager;

import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;;import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


/**
 * Created by jon on 30/07/2016.
 */
public class RestPost extends AsyncTask<String, String, String>
{
    private static final String TAG = "RestPost";

    public RestPost()
    {
        super();
        // setear las variables de contexto, si es necesario
    }

    @Override
    protected String doInBackground(String... params)
    {
        DataOutputStream printout;
        String ruta = params[0];
        //String query = params[1];

        try
        {
            String urlString = ruta;
            URL url = new URL(urlString);
            /*
            JSONObject jsonParam = new JSONObject();
            jsonParam.put("tokenId", query);
            String message =jsonParam.toString();
            */
            String message =params[1];
            Log.i(TAG, "Antes de url.openConnection()");
            HttpURLConnection conn  = (HttpURLConnection) url.openConnection();
            Log.i(TAG, "Después de url.openConnection()");
            conn.setReadTimeout( 10000 /*milliseconds*/ );
            conn.setConnectTimeout( 15000 /* milliseconds */ );
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setFixedLengthStreamingMode(message.getBytes().length);

            //make some HTTP header nicety
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");

            //open
            conn.connect();

            //setup send
            OutputStream os = new BufferedOutputStream(conn.getOutputStream());
            os.write(message.getBytes());
            //clean up
            os.flush();


            //JSONObject jsonObject = new JSONObject();
            InputStream inputStream;
            // get stream
            if (conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
                inputStream = conn.getInputStream();
            } else {
                inputStream = conn.getErrorStream();
            }
            // parse stream
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String temp, response = "";
            while ((temp = bufferedReader.readLine()) != null) {
                response += temp;
            }

            return response;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onCancelled()
    {
        super.onCancelled();
    }

    @Override
    protected void onCancelled(String s)
    {
        super.onCancelled(s);
    }

    @Override
    protected void onPostExecute(String s)
    {
        super.onPostExecute(s);
        Log.i(TAG, "onPostExecute: " + s);

    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(String... values)
    {
        super.onProgressUpdate(values);
    }
}
