package com.example.TreballFiGrau;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class AfegirFotoWeb implements Runnable{
	String imgStringBase64;
	Imatge img;
	
	public AfegirFotoWeb(String imgStr,Imatge im){
		imgStringBase64=imgStr;
		img = im;
	}
	

	@Override
	public void run() {
		try{
		JSONObject json = new JSONObject();;
		
		json.put("imatge",imgStringBase64);

		Log.d("aki",imgStringBase64);
		
				
		InputStream inputStream = null;
	    String result = "";
	               
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://tfg-sharenews.rhcloud.com/upload_file_android/");

        String jsonString = json.toString();
      
		
        StringEntity se = new StringEntity(jsonString);

        httpPost.setEntity(se);
       
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type","application/json");
        
        
        Log.d("aki", httpPost.getEntity().toString());
       
       
        Log.d("aki", "despres getentity");
       
		
        HttpResponse response = httpClient.execute((HttpUriRequest)httpPost);

        Log.d("aki", "despres conexio post de afegir Foto");
  
        Log.d("aki", "despres inputstream");
        
              
        HttpEntity entity = response.getEntity();
        InputStream instream=null;
		
		instream = entity.getContent();

        result = convertStreamToString(instream);
        instream.close();
        Log.d("aki","despres convert");
        //Log.d("aki",result);
        
        JSONObject jsonRebut= new JSONObject(result);
        Log.d("aki","despres convert");
	
    
        String value2 = jsonRebut.getString("result");
        Log.d("aki", value2);
        String value = jsonRebut.getString("message");
	

	
       
	
        Log.d("aki", value);
    
        
      
        
       
		instream.close();
	
        
		Log.d("aki","avans pasar imatge a la classe imatge");
		Bitmap aixo = pillarFoto(jsonRebut);
		img.imatge=aixo;
		Log.d("aki","despres pasar imatge a la classe imatge");
		img.b=true;
		}catch(Exception e){
	    	 Log.d("aki", "peeeeeeeeeeeeeettttttttttttaaaaaaaaaa");
	    }
	}
	
	public Bitmap pillarFoto(JSONObject jsonRebut) throws JSONException{
    	Log.d("aki","dintre pillar foto");
   
        
        String value2 = jsonRebut.getString("message");
    
        //Log.d("aki", value2);
        
        
        byte[] decodedString = Base64.decode(value2, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        
        return decodedByte;
        //ImageView imageView = (ImageView) findViewById(R.id.imgView);
        //view.setImageBitmap(decodedByte);
	}
	public String convertStreamToString(InputStream is) {

	    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	    StringBuilder sb = new StringBuilder();

	    String line = null;
	    try {
	        while ((line = reader.readLine()) != null) {
	            sb.append(line + "\n");
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    } finally {
	        try {
	            is.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	    return sb.toString();
	}
}

