package com.example.TreballFiGrau;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;
import android.widget.EditText;

public class AfegirComentariWeb implements Runnable {
	private String autor;
	private String contingut;
	private String idNoticia;
	
	public AfegirComentariWeb(String au,String co,String id) {
	    autor = au;
	    contingut = co;
	    idNoticia = id;
	}
	
	public void run() {
		try{
			
		
			HttpClient httpclient = new DefaultHttpClient();
		    HttpPost httppost = new HttpPost("http://tfg-sharenews.rhcloud.com/afegirComentari/");

		    MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 

		    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		    
		    
		    
			JSONObject json = new JSONObject();
			json.put("contingut", contingut);
			json.put("autor",autor);
			json.put("idNoticia", idNoticia);

			String jsonString = json.toString();
		
			builder.addTextBody("infoNoticia", jsonString);
			   
		    httppost.setEntity(builder.build());
		   

		    String result = "";
		               
		    Log.d("aki", "avans conexio post de afegir comentari");
	        HttpResponse response = httpclient.execute((HttpUriRequest)httppost);
	        Log.d("aki", "despres conexio post de afegir comentari");
	  
   
	        HttpEntity entity = response.getEntity();
	        InputStream instream = entity.getContent();
	        result = convertStreamToString(instream);
	        Log.d("aki","despres convert");
	     
	        
	        JSONObject jsonRebut = new JSONObject(result);
	
	        String value2 = jsonRebut.getString("result");
	
	        Log.d("aki", value2);
	        
	        instream.close();
	 
			}catch(Exception e){
		    	 Log.d("aki", "peeeeeeeeeeeeeettttttttttttaaaaaaaaaa");
		    }
		
		
	}
	
	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();
	}
}
