package com.example.TreballFiGrau;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
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.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;



public class LlistaNoticies extends Fragment{
	//public static ArrayList<Noticia> dades;
	public static Llista_AdapterNoticies llista_adapter_noticies;
	public ListView llista;
	public int ultimaNoticia=0;
	ViewGroup rootView;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
    	rootView = (ViewGroup) inflater.inflate(R.layout.llista_noticies, container, false);
    	
    	//dades = new ArrayList<Noticia>();
    	//llegirNoticiesServer();
		//construirDades();
    	Log.d("aki", "dintre fragment llista noticies");
    	llista_adapter_noticies = new Llista_AdapterNoticies(getActivity(), R.layout.noticia);
		llista = (ListView) rootView.findViewById(R.id.ListView_noticies);
        llista.setAdapter(llista_adapter_noticies);

        afegirListener();
        listenerBotons();
        return rootView;
    }
	/*
	public void llegirNoticiesServer(){
		Runnable r = new LlegirNoticiesWeb(ultimaNoticia);
    	new Thread(r).start();
	}
	*/
	
	public void listenerBotons(){
		Button button1 = (Button) rootView.findViewById(R.id.buttonEconomia);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Log.d("aki", "dintre boto 1");
            	Llista_AdapterNoticies.reiniciarLlista();
            	new CargarNoticiesCategoria().execute("Economia");
                //Intent intent = new Intent(MainActivity.this, GoogleMaps.class);
                //startActivity(intent);
			
            	//mPager.setCurrentItem(0);
            	
            }
        });
        Button button2 = (Button) rootView.findViewById(R.id.buttonEsports);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Log.d("aki", "dintre boto 2");
            	Llista_AdapterNoticies.reiniciarLlista();
            	new CargarNoticiesCategoria().execute("Esports");
            	//Intent intent = new Intent(MainActivity.this, EscriureNoticia.class);
            	//Log.d("aki", "despres intent");
            	//startActivity(intent);

            	Log.d("aki", "surt boto 2");

            }
        });
        Button button3 = (Button) rootView.findViewById(R.id.buttonSocietat);
        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Log.d("aki", "dintre boto 3");
            	Llista_AdapterNoticies.reiniciarLlista();
            	new CargarNoticiesCategoria().execute("Societat");
            	//Intent intent = new Intent(MainActivity.this, PujarFoto.class);
            	//startActivity(intent);
				//post();
            }
        });
        Button button4 = (Button) rootView.findViewById(R.id.buttonCultura);
        button4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Log.d("aki", "dintre boto 4");
            	Llista_AdapterNoticies.reiniciarLlista();
            	new CargarNoticiesCategoria().execute("Cultura");
            	//Intent intent = new Intent(MainActivity.this, Login.class);
            	//startActivity(intent);
				
            }
        });
        Button button5 = (Button) rootView.findViewById(R.id.buttonPolitica);
        button5.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Log.d("aki", "dintre boto 5");
            	Llista_AdapterNoticies.reiniciarLlista();
            	new CargarNoticiesCategoria().execute("Politica");
            	//Intent intent = new Intent(MainActivity.this, PujarVideo.class);
            	//startActivity(intent);
				
            }
        });
        Button button6 = (Button) rootView.findViewById(R.id.buttonTecnologia);
        button6.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Log.d("aki", "dintre boto 6");
            	Llista_AdapterNoticies.reiniciarLlista();
            	new CargarNoticiesCategoria().execute("Tecnologia");
            	//Intent intent = new Intent(MainActivity.this, MostrarFotosNoticia.class);
            	//startActivity(intent);
				
            }
        });
	}
	
	
	
	
	public void afegirListener(){
		llista.setOnItemClickListener(new OnItemClickListener() { 
			@Override
			public void onItemClick(AdapterView<?> pariente, View view, int posicion, long id) {
				Log.d("aki", "dintre boto listener k vull");
				Noticia noti = (Noticia) pariente.getItemAtPosition(posicion); 
				Log.d("aki", "dintre"+ noti.titol);
				Log.d("aki", ""+posicion);
				
				NoticiaSeleccionada.cargarInformacio(noti);
				Intent intent = new Intent(getActivity(), LlegirNoticia.class);
		        startActivity(intent);
		        
		        
                CharSequence texto = "Seleccionado: " + noti.titol;
                Toast toast = Toast.makeText(getActivity(), texto, Toast.LENGTH_LONG);
                toast.show();
			}
        });
	}
}
