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

import entities.Indexbook;
import entities.Lesson;
import entities.LessonStudent;
import entities.Semester;
import entities.Users;
import helper.LessonType;
import java.util.ArrayList;
import java.util.LinkedList;
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.LessionSessionBeanRemote;
import manager.LessonStudentSessionBeanRemote;
import manager.SemesterSessionBeanRemote;

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



    /** Creates a new instance of ListKurzusT */
    int itemsPerPage = 15;
    int currentPage;
    HtmlDataTable tableComponent;
    String pagingDirection;
    String sortDirection;
    String from, to;
    List<Lesson> listKurzus;
    Long aktSemester;
    Semester semester;
    ArrayList<SelectItem> listSemester;
    Users user;

    public ListKurzusT() {
        Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        user = (Users) map.get("felhasznalo");
    }

    public List<Lesson> getListKurzus() {

        listKurzus = this.lessionSessionBean.findBySemesterANDTeacher(user, getSemester());
        int fromIndex = currentPage*itemsPerPage;
        int toIndex = fromIndex+itemsPerPage;

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

    public void setListKurzus(List<Lesson> listKurzus) {
        this.listKurzus = listKurzus;
    }

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

    public int getCurrentPage() {
        return currentPage;
    }

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

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

    public String enterAction(){
        boolean mark = false,signature = false;
        Lesson selected = (Lesson) tableComponent.getRowData();
        List<Indexbook> indexB = new LinkedList<Indexbook>();
        List<LessonStudent> students = this.lessonStudentSessionBean.findStudents(selected);
        Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        for(LessonStudent akt: students){
            indexB.add(this.indexbookSessionBean.findBySemesterAndUserANDSubjectId(akt.getStudentId(), getSemester(), akt.getLessonId().getCurrentSubjectId().getSubjectId()));
        }
        if(selected.getLessonType().equals(LessonType.LECTURE))
            mark = true;
        m.put("mark", mark);
        m.put("index", indexB);
        m.put("indexsize", indexB.size());
        m.put("semester", getSemester());
        return "liststudentt";
    }

    


}
