package com.example.TreballFiGrau;

import java.io.BufferedReader;
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.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

public class CargarComentarisWebInici extends AsyncTask<String, Void, String>{

	@Override
	protected String doInBackground(String... params) {
		try{
			Log.d("aki", "dintre llegir comentaris threadddd");
			
			
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost("http://tfg-sharenews.rhcloud.com/llegirComentarisBasic/");
	
		    
		    MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
		    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
	
		    JSONObject json = new JSONObject();
			
			json.put("id", NoticiaSeleccionada.id );
			Log.d("aki", "id de noticia coentaris vull caragar"+NoticiaSeleccionada.id);
		    
			String jsonString = json.toString();

			builder.addTextBody("numeroIdNoticia", jsonString);
			   
		    httppost.setEntity(builder.build());
		    Log.d("aki", "avans execute");
		    HttpResponse response = httpclient.execute((HttpUriRequest)httppost);
		    Log.d("aki", "despres executee");
	        HttpEntity entity = response.getEntity();
	        InputStream instream = entity.getContent();
	        String result = convertStreamToString(instream);
	        Log.d("aki", "avans creacio objecte json");
	        Log.d("aki", "result"+result);
	        JSONObject jsonRebut = new JSONObject(result);
	        Log.d("aki", "avans jsonn");
	        Log.d("aki",""+ jsonRebut);
	        Log.d("aki", "despres  creacio objecte json");
	        instream.close();
	       
	        if (jsonRebut.getString("resultat").equals("OK")){
	        	Log.d("aki", "dintre ifff");
	        	String nombreComentarisString = jsonRebut.getString("numeroComentaris"); 
	        	int nombreComentaris = Integer.parseInt(nombreComentarisString);
	        	Log.d("aki", "nombre comentaris" +nombreComentaris);
	        	
	        	for(int i=0;i<nombreComentaris;i++){

			        String autor = jsonRebut.getString("autor"+i);
			        Log.d("aki", autor);
			        
			        String contingut = jsonRebut.getString("contingut"+i);
			        Log.d("aki", contingut);
			        
			        String data = jsonRebut.getString("data"+i);
			        Log.d("aki", data);

			        Llista_AdapterComentaris.entradasComentari.add(new Comentari("0",autor,contingut,data));
	        	}
	        	//LlistaComentaris.actualitzarLlistaComentaris();
		        
	        }
	        
	        
		}catch(Exception e){
	   	 	Log.d("aki", "peeeeeeeeeeeeeettttttttttttaaaaaaaaaa");
		}
		
		return null;
	}
	
	@Override
	protected void onPostExecute(String result) {
	  
		 Log.d("aki", "post exectute avans");
	     LlistaComentaris.llista_adapter_comentaris.notifyDataSetChanged();
	     Log.d("aki", "post exectute despres");
	    
	}
	
	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();
	}

}