package com.example.TreballFiGrau;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class PerfilUsuari extends Fragment {
	ViewGroup rootView;
	TextView userText;
	public final static int LOGIN = 1;
	public final static int FOTOPERFIL = 2;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
    	rootView = (ViewGroup) inflater.inflate(R.layout.perfil_usuari, container, false);

    	
    	
    	userText = (TextView) rootView.findViewById(R.id.textUsuari);
    	
    	
    	trigers();
        return rootView;
    }
    
    public void trigers(){
    	
    	Button button1 = (Button) rootView.findViewById(R.id.buttonEscriureNoticia);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(User.conectat){
	            	Intent intent = new Intent(rootView.getContext(), EscriureNoticia.class);
	                startActivity(intent);	
            	}else{
            		CharSequence texto = "Connectat per Escriure Noticia!";
                	Toast toast;
            		toast = Toast.makeText(getActivity(), texto, Toast.LENGTH_LONG);
                    toast.setDuration(100000);
                    toast.show();
            	}

            }
        });
        
        Button button2 = (Button) rootView.findViewById(R.id.buttonCanviarFotoPerfil);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(User.conectat){
	            	Intent intent = new Intent( getActivity(), CanviarFotoPerfil.class);
	            	startActivityForResult(intent, FOTOPERFIL);
	            }else{
	        		CharSequence texto = "Connectat per canviar foto perfil";
	            	Toast toast;
	        		toast = Toast.makeText(getActivity(), texto, Toast.LENGTH_LONG);
	                toast.setDuration(100000);
	                toast.show();
	        	}
            	
            }
        });
        
        Button button3 = (Button) rootView.findViewById(R.id.buttonSessio);
        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(getActivity(), Login.class);
            	startActivityForResult(intent, LOGIN);	
       

            }
        });
        
        Button button4 = (Button) rootView.findViewById(R.id.buttonGoogleMaps);
        button4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Log.d("aki", "dintre google maps");
            	Intent intent = new Intent(rootView.getContext(), GoogleMaps.class);
                startActivity(intent);		


            }
        });
        Button button5 = (Button) rootView.findViewById(R.id.buttonMainNoticies);
        button5.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Llista_AdapterNoticies.iniciarNoticiesMain();
            	Llista_AdapterNoticies.categoria=false;

            }
        });
        Button button6 = (Button) rootView.findViewById(R.id.buttonDesconectar);
        button6.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
	            if(User.conectat){
	            	User.conectat=false;
	            	User.nom=null;
	            	userText.setText("Usuari: ");
	            	
	            }else{
	        		CharSequence texto = "Connectat per canviar foto perfil";
	            	Toast toast;
	        		toast = Toast.makeText(getActivity(), texto, Toast.LENGTH_LONG);
	                toast.setDuration(100000);
	                toast.show();
	        	}
            }
        });
        Button button7 = (Button) rootView.findViewById(R.id.buttonCercaTags);
        button7.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Log.d("aki", "dintre cercatags");
            	Intent intent = new Intent(rootView.getContext(), CercaTags.class);
            	Log.d("aki", "dintre cercatags");
                startActivity(intent);

            }
        });
    }
    
    @Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        switch (requestCode) {
        case FOTOPERFIL:
        	Log.d("aki", "estic on activity ressult foto perfil");
            ImageView image = (ImageView) rootView.findViewById(R.id.imageViewFotoPerfil);
        	image.setImageBitmap(BitmapFactory.decodeFile(User.fotoPerfil)); 
            break;
            
        case LOGIN:
        	Log.d("aki", "estic on activity ressult foto perfil"); 
        	CharSequence texto;
        	Toast toast;
        	switch(User.codierror){
    		case 1:
    			Log.d("aki", "User is valid, active and authenticated");
    			texto = "User is valid, active and authenticated";
    			User.conectat=true;
    			userText.setText(User.nom);
                toast = Toast.makeText(getActivity(), texto, Toast.LENGTH_LONG);
                toast.setDuration(100000);
                toast.show();
    			
    			break;
    		case 2:
    			Log.d("aki","The password is valid, but the account has been disabled!");
    			texto = "The password is valid, but the account has been disabled!";
                toast = Toast.makeText(getActivity(), texto, Toast.LENGTH_LONG);
                toast.setDuration(100000);
                toast.show();
    			break;
    		case 3:
    			Log.d("aki","The username and password were incorrect.");
    			texto = "The username and password were incorrect.";
                toast = Toast.makeText(getActivity(), texto, Toast.LENGTH_LONG);
                toast.setDuration(100000);
                toast.show();
    			break;
            
        	}
           
        
        }
    }
}

