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 CargarNoticiesCategoria extends AsyncTask<String, Void, String>{

	@Override
	protected String doInBackground(String... params) {
		try{
			Log.d("aki", "dintre llegir noticies web Ctegoria");
			
			String categoria = params[0];
			Log.d("aki", categoria);
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost("http://tfg-sharenews.rhcloud.com/llegirNoticiesCategoriaBasic/");
	
		    
		    MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
		    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
	
		    JSONObject json = new JSONObject();
			
			json.put("categoriaBuscar", categoria );
		    
			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");

	        JSONObject jsonRebut = new JSONObject(result);
	
	        Log.d("aki", "despres  creacio objecte json");
	        instream.close();
	       
	        if (jsonRebut.getString("resultat").equals("OK")){
	        	Log.d("aki", "dintre ifff");
	        	String nombreComentarisString = jsonRebut.getString("numeroNoticies"); 
	        	int nombreComentaris = Integer.parseInt(nombreComentarisString);
	        	Log.d("aki", "nombre Noticies" +nombreComentaris);
	        	
	        	for(int i=0;i<nombreComentaris;i++){

	        		String id = jsonRebut.getString("id"+i);
			        Log.d("aki", id);
			        
			        String titol = jsonRebut.getString("titol"+i);
			        Log.d("aki", titol);
			        
			        String contingut = jsonRebut.getString("contingut"+i);
			        Log.d("aki", contingut);
			        
			        String autor = jsonRebut.getString("autor"+i);
			        Log.d("aki", autor);
			        
			        String tags = jsonRebut.getString("tags"+i);
			        Log.d("aki", tags);
			        
			        String categories = jsonRebut.getString("categories"+i);
			        Log.d("aki", categories);
			        		        
			        String latitude = jsonRebut.getString("latitude"+i);
			        Log.d("aki", latitude);
			        
			        String longitude = jsonRebut.getString("longitude"+i);
			        Log.d("aki", longitude);
			        
			        String localitzacio = jsonRebut.getString("localitzacio"+i);
			        Log.d("aki", localitzacio);
			        
			        String data = jsonRebut.getString("data"+i);
			        Log.d("aki", data);
			        
			        String fotos = jsonRebut.getString("fotos"+i);
			        Log.d("aki", fotos);
			        
			        String videos = jsonRebut.getString("videos"+i);
			        Log.d("aki", videos);
			        
			        Llista_AdapterNoticies.entradas.add(new Noticia(id,titol,contingut,autor,latitude,longitude,tags,categories,fotos,videos));
			        
			        
			   
	        	}
	        	//LlistaComentaris.actualitzarLlistaComentaris();
		        
	        }
	        
	        
		}catch(Exception e){
	   	 	Log.d("aki", "peeeeeeeeeeeeeettttttttttttaaaaaaaaaa");
		}
		
		return null;
	}
	
	@Override
	protected void onPostExecute(String result) {
	  
		 Log.d("aki", "post exectute avans");
	     LlistaNoticies.llista_adapter_noticies.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();
	}

}