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

package student;

import entities.Indexbook;
import entities.Semester;
import entities.Users;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import manager.IndexbookSessionBeanRemote;
import manager.SemesterSessionBeanRemote;

/**
 *
 * @author Szilárd
 */
@ManagedBean(name="ListIndexbook")
@RequestScoped
public class ListIndexbook {
    @EJB
    private IndexbookSessionBeanRemote indexbookSessionBean;
    @EJB
    private SemesterSessionBeanRemote semesterSessionBean;

    int itemsPerPage = 15;
    int currentPage;
    HtmlDataTable tableComponent;
    String pagingDirection;
    String sortDirection;
    String from, to;
    List<Indexbook> listSubject;
    Long aktSemester;
    Semester semester;
    ArrayList<SelectItem> listSemester;
    Users user;
    /** Creates a new instance of ListIndexbook */
    public ListIndexbook() {
        Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        user = (Users) map.get("felhasznalo");
    }


    public List<Indexbook> getListSubject() {

        listSubject = this.indexbookSessionBean.findByStudentIdANDSemesterId(user, getSemester());
        int fromIndex = currentPage*itemsPerPage;
        int toIndex = fromIndex+itemsPerPage;

        if(toIndex>=listSubject.size())
            toIndex=listSubject.size();

        return listSubject.subList(fromIndex, toIndex);
    }

    public void setListSubject(List<Indexbook> listSubject) {
        this.listSubject = listSubject;
    }

    //--------------------------------------------------------------------------

    public int getCurrentPage() {
        return currentPage;
    }

    protected int calculatePageCount(){
        return (listSubject.size()/itemsPerPage)+((listSubject.size()%itemsPerPage) != 0 ? 1 : 0);
    }

    public int getPageCount(){
        return calculatePageCount();
    }

    public String getPagingDirection() {
        return pagingDirection;
    }

    public void setPagingDirection(String pagingDirection) {
        this.pagingDirection = pagingDirection;
    }

    public String page(){
        if(pagingDirection.equals("<") )//&& currentPage!=0)
            currentPage--;
        else if (pagingDirection.equals(">") )//&& currentPage+1<calculatePageCount())
            currentPage++;
        return null;
    }

    public String prevPage(){
        --currentPage;
        return null;
    }

    public String nextPage(){
        ++currentPage;
        return null;
    }

    public HtmlDataTable getTableComponent() {
        return tableComponent;
    }

    public void setTableComponent(HtmlDataTable tableComponent) {
        this.tableComponent = tableComponent;
    }

    //--------------------------------------------------------------------------

    public Long getAktSemester() {
        return aktSemester;
    }

    public void setAktSemester(Long aktSemester) {
        this.aktSemester = aktSemester;
    }

    public ArrayList<SelectItem> getListSemester() {
        List<Semester> s = this.semesterSessionBean.findOpenAndClosed();
        listSemester = new ArrayList();
        for(Semester akt: s){
            listSemester.add(new SelectItem(akt.getId(),akt.getSchoolYear()));
        }
        return listSemester;
    }

    public void setListSemester(ArrayList<SelectItem> listSemester) {
        this.listSemester = listSemester;
    }

    public Semester getSemester() {
        Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        this.semester =  (Semester) m.get("semester");
        if(semester == null)
           semester = this.semesterSessionBean.findOpen();
        return semester;
    }

    public void setSemester(Semester semester) {
        this.semester = semester;
    }

    public String listAction(){
        semester = this.semesterSessionBean.findById(aktSemester);
        Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        m.put("semester", semester);
        listSubject.clear();
        return "listindexbook.jsp";
    }

    public String backAction(){
        Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        m.remove("semester");
        return "menus";
    }
}
