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.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
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.apache.http.util.EntityUtils;





import org.json.JSONObject;

import android.app.Activity;
import android.util.Log;

public class AfegirVideoWeb extends Activity implements Runnable{
	
	String videoPath;

	
	public AfegirVideoWeb(String vidStr){
		Log.d("aki","thread constr");
		videoPath=vidStr;
	}

	@Override
	public void run() {
		try{
		Log.d("aki","incii try");
		HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost("http://tfg-sharenews.rhcloud.com/upload_file_android2/");
	    Log.d("aki","avans pasar imatge a la classe imatge");
	 
	    //File filebodyVideo = new FileBody(new File(videoPath));
	    //String title = new StringBody("Filename: " + videoPath);
	    //StringBody description = new StringBody("This is a description of the video");

	    MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
	    Log.d("aki","multiparentity builder");
	    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
	    Log.d("aki","avansFile");
	    File file = new File(videoPath);
	    FileBody fb = new FileBody(file);

	    builder.addPart("file", fb);  
	    Log.d("aki","despres file");
	    builder.addTextBody("cosa",  "cosa");
	   
	    httppost.setEntity(builder.build());
	    
	    
	    
	    HttpResponse response=null;
		try {
			response = httpclient.execute(httppost);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    HttpEntity entity = response.getEntity();
	    Log.d("aki",entity.toString());
	    
	    InputStream instream=null;
		
		instream = entity.getContent();
		
		Log.d("aki",instream.toString());

		String result = convertStreamToString(instream);
	    instream.close();
	    Log.d("aki",result);
	    Log.d("aki","jsonnnnnnnnnnn");
	   
	    
	    JSONObject jsonRebut= new JSONObject(result);
	    Log.d("aki","despres convert");
	
	
	    String value2 = jsonRebut.getString("result");
	    Log.d("aki", value2);
	    String value = jsonRebut.getString("message");
	
	    Log.d("aki", value);
	
	    
	  
	    
	   
		instream.close();
	    // DEBUG
	    

	    // DEBUG
	    /*
	    System.out.println( response.getStatusLine( ) );
	    if (response != null) {
	      try {
			System.out.println( EntityUtils.toString( (HttpEntity) response ) );
		} catch (ParseException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    } // end if

	    if (response != null) {
	    	try {
				((HttpEntity) response).consumeContent( );
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    } // end if
		*/
	    httpclient.getConnectionManager( ).shutdown( );
		}catch(Exception e){
	    	 Log.d("aki", "peeeeeeeeeeeeeettttttttttttaaaaaaaaaa");
	    }
	} // end of uploadVideo( )
		
	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();
	}
}