package com.example.TreballFiGrau;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;

import com.squareup.picasso.Picasso;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class Login extends Activity{
	private String usuari;
	private String contrasenya;
	ViewGroup rootView;
	EditText usuariText;
	EditText contrasenyaText; 
	TextView registrar; 

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("aki", "inici");
		setContentView(R.layout.login);

		
		usuariText = (EditText) findViewById(R.id.entradaUsuari);
		contrasenyaText = (EditText) findViewById(R.id.entradaContrasenya);
		registrar = (TextView) findViewById(R.id.linkRegistrar);
		
		comprovarUserContrasenya();
		
		
		registrar.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
			public void onClick(View v) {
            	Log.d("aki", "dintre link");
            	Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://tfg-sharenews.rhcloud.com/media/img/2014/05/30/grey_knight_term_150_129.gif/"));
            	//startActivity(browserIntent);
            	Log.d("aki", "avans");
            	
            	//    AIXO ES LO DE MOSTARR UNA IMATGE DINTERNET, FUNCIONA
            	//ImageView imageView1= (ImageView) findViewById(R.id.imageView1);
            	//Picasso.with(getBaseContext()).load("http://tfg-sharenews.rhcloud.com/media/img/2014/05/30/grey_knight_term_150_129.gif/").into(imageView1);
            	//-----------------------
            	
            	new CargarNoticiesWeb().execute("1");
            	Log.d("aki", "despres ");
   
            }
        });
			
		
		
		
		
		
		
		Log.d("aki", "avans cridar funcio");
		//comprovarUserContasenya();
	
	}
	private Drawable grabImageFromUrl(String url) throws Exception {
        return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
    }
	
	public Drawable drawable_from_url(String url, String src_name) throws 
	   java.net.MalformedURLException, java.io.IOException 
	{
		url="http://tfg-sharenews.rhcloud.com/media/img/2014/05/30/grey_knight_term_150_129.gif";
		src_name="cosa";		
	   return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name);
	}
	public void comprovarUserContrasenya(){
		Log.d("aki", "1111111o");
		Button button = (Button) findViewById(R.id.botoComprovarLogin);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Log.d("aki", "2222222222");
            	usuari=usuariText.getText().toString();
        		contrasenya=contrasenyaText.getText().toString();
        		
        		Log.d("aki", usuari);
        		Log.d("aki", contrasenya);
        		
            	Log.d("aki", "dintre boto comprovarUserContasenya");
            	try {
					String cosa = new ConectarServerLogin().execute(usuari,contrasenya).get();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	Log.d("aki", "encara nooo");
            	Log.d("aki", "encara nooo");
            	
            	finish();

            }
        });
	}
}