/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pagination;

import db.entidades.Area;
import db.entidades.PlanoActuacao;
import db.entidades.Risco;
import db.entidades.Valor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author lluis
 */
public class Pagination {
    private int TOTAL_PAGINAS;
    //private HashMap<Integer, Risco> riscosHash = new HashMap();
    private Vector<Risco> riscosVector = new Vector();
    private Vector<Valor> valoresVector = new Vector();
    private int current_page = 1;
    
    public Pagination(PlanoActuacao p)
    {
        initialize(p);
    }
    
    public Vector<Pagina> getPaginas()
    {
        Vector<Pagina> paginas = new Vector();
        Pagina pagina = null;
        
        if(current_page > 1)
        {
            pagina = new Pagina();
            pagina.setId("prev");
            pagina.setPrevious(new Boolean(true));
            pagina.setText("<<");
            paginas.add(pagina);
        }
        
        int i = 0;
        //Iterator<Valor> iter = valoresVector.iterator();
        Iterator<Risco> iter = riscosVector.iterator();        
        while(iter.hasNext())
        {
            //Valor v = iter.next();
            Risco r = iter.next();
            pagina = new Pagina();
            //pagina.setId("valor" + v.getId().toString());            
            pagina.setId("valor" + r.getId().toString());            
            //pagina.setRisco_id(r.getId());
            i++;                                    
            pagina.setPage_nr(i);
            if(i == current_page)
            {
                pagina.setDisabled(true);
            }
            pagina.setText(i);
            paginas.add(pagina);
        }
        
        if(current_page < TOTAL_PAGINAS)
        {
            pagina = new Pagina();
            pagina.setId("next");            
            pagina.setNext(new Boolean(true));
            pagina.setText(">>");
            paginas.add(pagina);
        }   
        
        return paginas;
    }
    
    public Risco getValor(Pagina p)
    {
        if(p.isNext())
        {
            current_page++;
        }
        else if(p.isPrevious())
        {
            current_page--;            
        }
        else
        {
            current_page = p.getPage_nr().intValue();
        }

        //return valoresVector.get(current_page-1);  
        return riscosVector.get(current_page-1);          
    }
    
    public Risco nextValor()
    {
        //
        //Valor v = null;
        Risco r = null;
        
        if( current_page < TOTAL_PAGINAS)
        {
           current_page++; 
           r = riscosVector.get(current_page-1);  
           //v = valoresVector.get(current_page-1);             
        }
        
        return r;
    }
    
    public int totalPaginas()
    {
        return TOTAL_PAGINAS;
    }
    
    private void initialize(PlanoActuacao p)
    {
        for(Area a : p.getAreas())
        {
            for(Risco r : a.getRiscos())
            {
                ////for(Valor v : r.getValores())
                ////{
                    //r.setValor(v); to see later
                    riscosVector.add(r);                    
                    //valoresVector.add(v);                                        
                ////}
            }
        }  
        TOTAL_PAGINAS = riscosVector.size();
        //TOTAL_PAGINAS = valoresVector.size();        
    }
   
}
