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.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

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

public class ConectarServerLogin extends AsyncTask<String, Void, String> {
	String user;
	

    protected String doInBackground(String... valors) {
    	try{
    	JSONObject json = new JSONObject();
    	Log.d("aki",valors[0]);
    	Log.d("aki",valors[1]);
    
		json.put("user",valors[0]);
		json.put("password",valors[1]);

		//Log.d("aki",imgStringBase64);
		
				
		InputStream inputStream = null;
	    String result = "";
	               
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://tfg-sharenews.rhcloud.com/loginPageAndroid/");

        String jsonString = json.toString();
      
		
        StringEntity se = new StringEntity(jsonString);

        httpPost.setEntity(se);
       
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type","application/json");
        
        
        Log.d("aki", httpPost.getEntity().toString());
       
       
        Log.d("aki", "despres getentity");
       
		
        HttpResponse response = httpClient.execute((HttpUriRequest)httpPost);

        Log.d("aki", "despres conexio conectarLoginAndroid");
  

        
              
        HttpEntity entity = response.getEntity();

		
        InputStream	instream = entity.getContent();

        result = convertStreamToString(instream);
        Log.d("aki","despres convert");
        
        JSONObject jsonRebut= new JSONObject(result);

        String valor = jsonRebut.getString("result");
        Log.d("aki",valor);
        
        if(valor.equals("1")){
        	user = jsonRebut.getString("username");
        	 Log.d("aki",user);
        }

        
        
       
		instream.close();
		return valor;
		
    	}catch(Exception e){
	    	 Log.d("aki", "peeeeeeeeeeeeeettttttttttttaaaaaaaaaa");
	    }
		return null;  	      
    }
	
	protected void onProgressUpdate() {
		Log.d("aki", "onProgressUpdate");
    }
	
    protected void onPostExecute(String result) {
    	Log.d("aki", "onPostExecute");
    	Log.d("aki",result);
    	switch(result){
    		case "1":
    			Log.d("aki", "User is valid, active and authenticated");
    			User.conectat=true;
    			User.codierror=1;
    			User.nom=user;
    			Log.d("aki", "despres User is valid, active and authenticated");
    			break;
    		case "2":
    			Log.d("aki","The password is valid, but the account has been disabled!");
    			User.conectat=false;
    			User.codierror=2;
    			break;
    		case "3":
    			Log.d("aki","The username and password were incorrect.");
    			User.conectat=false;
    			User.codierror=3;
    			break;
    			
    		
    	}
       
    }


    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();
	}
}
