package com.bitbusters.android.speproject;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
//import static com.google.android.gms.internal.zznu.is;

public class ImageDownloader extends AsyncTask<String, Void, Image> {
    private static final String DEBUG_TAG = "IMAGE_DOWNLOADER";
    private ImageView iv;
//    private OnTaskCompleted listener;

    public ImageDownloader(ImageView iv) {
        this.iv = iv;
    }

    @Override
    protected Image doInBackground(String...params) {
        Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
        Bitmap bmp = Bitmap.createBitmap(10, 10, conf);
        Image image = new Image("", bmp,0, 0, "",PhotoTag.NA);
        // param 0 is the id of the image
        try {

            Uri.Builder builder = new Uri.Builder();
            builder.scheme("http")
                    .encodedAuthority("172.23.115.174:3000")
                    .appendPath("getImage")
                    .appendPath(params[0]);
            String myUrl = builder.build().toString();
            Log.d(DEBUG_TAG, myUrl);
            URL url = new URL(myUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            int response = conn.getResponseCode();
            Log.d(DEBUG_TAG, "Url is: " + url);
            Log.d(DEBUG_TAG, "The response is: " + response);
            InputStream inputStream = null;
            inputStream = conn.getInputStream();
            //len limits the input string returned. should be changed from 5000 when tested.
            int len = 5000;
            // Convert the InputStream into a string
//            String SamplingPoints = readIt(is, len);
            InputStreamToImage inputStreamToImage = new InputStreamToImage();
            image = inputStreamToImage.readImageStream(inputStream);
//            Log.d(DEBUG_TAG, "The result is: " + SamplingPoints);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(Image result) {
        System.out.println(result.getComment());

        iv.setImageBitmap(result.getImage());
    }

    public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
        Reader reader = null;
        reader = new InputStreamReader(stream, "UTF-8");
        char[] buffer = new char[len];
        reader.read(buffer);
        return new String(buffer);
    }

}

@@ -1211,21 +1211,20 @@
 y(%221
-72.23.115.174
+39.59.184.70
 :300

Update server ip address