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

package admin;

import entities.Semester;
import entities.Thesis;
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.SemesterSessionBeanRemote;
import manager.ThesisSessionBeanRemote;

/**
 *
 * @author Szilárd
 */
@ManagedBean(name="ListThesis")
@RequestScoped
public class ListThesis {
    @EJB
    private SemesterSessionBeanRemote semesterSessionBean;
    @EJB
    private ThesisSessionBeanRemote thesisSessionBean;

    int itemsPerPage = 15;
    int currentPage;
    HtmlDataTable tableComponent;
    String pagingDirection;
    String sortDirection;
    String from, to;
    List<Thesis> listThesis;
    Long aktSemester;
    Semester semester;
    ArrayList<SelectItem> listSemester;
    /** Creates a new instance of ListThesis */
    public ListThesis() {
    }

    public List<Thesis> getListThesis() {
        listThesis = this.thesisSessionBean.findBySemester(getSemester());
        int fromIndex = currentPage*itemsPerPage;
        int toIndex = fromIndex+itemsPerPage;

        if(toIndex>=listThesis.size())
            toIndex=listThesis.size();
        return listThesis.subList(fromIndex, toIndex);
    }

    public void setListThesis(List<Thesis> listThesis) {
        this.listThesis = listThesis;
    }


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


    public int getCurrentPage() {
        return currentPage;
    }

    protected int calculatePageCount(){
        return (listThesis.size()/itemsPerPage)+((listThesis.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 "listcurrentsubject.jsp";
    }

    public String nextPage(){
        ++currentPage;
        return "listcurrentsubject.jsp";
    }

    public HtmlDataTable getTableComponent() {
        return tableComponent;
    }

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

    //--------------------------------------------------------------------------
    //  LISTÁZ FÉLÉV
    //--------------------------------------------------------------------------

    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);
        listThesis.clear();
        return "listthesis.jsp";
    }

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

    public String createAction(){
        //semester = this.semesterSessionBean.findById(aktSemester);
        //Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        //m.put("semester", semester);
        //listThesis.clear();
        //return "createthesis.jsp";
        return "createthesis.jsp";
    }

    public String editAction(){
        return null;
    }

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