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 AfegirNoticiaWeb implements Runnable {
	private String titol;
	private String contingut;
	
	public AfegirNoticiaWeb(String ti,String co) {
	    titol=ti;
	    contingut=co;
	}
	
	public void run() {
		try{
			
		
			HttpClient httpclient = new DefaultHttpClient();
		    HttpPost httppost = new HttpPost("http://tfg-sharenews.rhcloud.com/afegirNoticiaGeri/");

		    MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 

		    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		    
		    if (NoticiaCreada.videos != null){
		    	 Log.d("aki","dintre videos");
		    	for(int i=0;  i < NoticiaCreada.videos.size(); i++){
		    		 File file = new File(NoticiaCreada.videos.get(i));
		    		 FileBody fb = new FileBody(file);
		    		 builder.addPart("video"+i, fb);
		    	}
		    }
		    
		    if (NoticiaCreada.fotos != null){
		    	 Log.d("aki","dintre fotos");
		    	for(int i=0;  i < NoticiaCreada.fotos.size(); i++){
		    		 File file = new File(NoticiaCreada.fotos.get(i));
		    		 FileBody fb = new FileBody(file);
		    		 builder.addPart("foto"+i, fb);
		    	}
		    }
		    
		    
			JSONObject json = new JSONObject();
	
			json.put("titol", NoticiaCreada.titol);
			json.put("contingut", NoticiaCreada.contingut);
			Log.d("aki", "User.nom: "+User.nom);
			json.put("autor", User.nom);
			json.put("tags", NoticiaCreada.tags);
			
			json.put("localitzacio", "Barcelona");
			if(NoticiaCreada.categorias!=null){
				String categories="";
				for(int i=0;  i < NoticiaCreada.categorias.size(); i++){
					String cat = NoticiaCreada.categorias.get(i);
					categories= categories + cat + ",";
				}
				json.put("categories", categories);
			}
			
			
			if(NoticiaCreada.gps==true){
				if (NoticiaCreada.latitude!=0.0){
					float la =(float) NoticiaCreada.latitude;
					float lo =(float) NoticiaCreada.longitude;
					Log.d("aki", "dintre envian localitzacio del thread");
					json.put("latitude",la);
					json.put("longitude",lo);
				}
			}
			
			String jsonString = json.toString();
		
	

			builder.addTextBody("infoNoticia", jsonString);
			   
		    httppost.setEntity(builder.build());
		   
			InputStream inputStream = null;
		    String result = "";
		               
		    Log.d("aki", "avans conexio post de afegir noticia");
	        HttpResponse response = httpclient.execute((HttpUriRequest)httppost);
	        Log.d("aki", "despres conexio post de afegir noticia");
	  
	   
	        
	              
	        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();
	}
}
