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 CargarNoticiesWeb extends AsyncTask<String, Void, String>{
	public String numeroId;
	
	
	@Override
	protected String doInBackground(String... params) {
		try{
			Log.d("aki", "dintre llegir noticies web");
			numeroId = params[0];
			Log.d("aki", numeroId);
			HttpClient httpclient = new DefaultHttpClient();
		    //HttpPost httppost = new HttpPost("http://tfg-sharenews.rhcloud.com/llegirNoticiesBasic/");
			HttpPost httppost = new HttpPost("http://tfg-sharenews.rhcloud.com/llegirNoticiesBasic/");
		    Log.d("aki", "despres creat post");
		    
		    MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
		    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		    Log.d("aki", "avsn jsonnn");
		    JSONObject json = new JSONObject();
			
			json.put("id", numeroId );
		    
			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")){
	        	
		        String id = jsonRebut.getString("id");
		        int numeroAnterior = Integer.parseInt(id);
		        int seguentNoticia = numeroAnterior+1;
		        Llista_AdapterNoticies.seguentNoticia = String.valueOf(seguentNoticia);
		        Log.d("aki", "numero que he apsat al adapter"+seguentNoticia);
		        
		        
		        Log.d("aki", id);
		        String titol = jsonRebut.getString("titol");
		        Log.d("aki", titol);
		        
		        String contingut = jsonRebut.getString("contingut");
		        Log.d("aki", contingut);
		        
		        String autor = jsonRebut.getString("autor");
		        Log.d("aki", autor);
		        
		        String tags = jsonRebut.getString("tags");
		        Log.d("aki", tags);
		        
		        String categories = jsonRebut.getString("categories");
		        Log.d("aki", categories);
		        		        
		        String latitude = jsonRebut.getString("latitude");
		        Log.d("aki", latitude);
		        
		        String longitude = jsonRebut.getString("longitude");
		        Log.d("aki", longitude);
		        
		        String localitzacio = jsonRebut.getString("localitzacio");
		        Log.d("aki", localitzacio);
		        
		        String data = jsonRebut.getString("data");
		        Log.d("aki", data);
		        
		        String fotos = jsonRebut.getString("fotos");
		        Log.d("aki", fotos);
		        
		        String videos = jsonRebut.getString("videos");
		        Log.d("aki", videos);
		        
		        Llista_AdapterNoticies.entradas.add(new Noticia(id,titol,contingut,autor,latitude,longitude,tags,categories,fotos,videos));
		        
		        Llista_AdapterNoticies.nombreNoticies = Llista_AdapterNoticies.entradas.size()-1;
		        
	        }
	        
	        
		}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();
	}

}
