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

package student;

import entities.CurrentSubject;
import entities.Lesson;
import entities.Semester;
import entities.Subject;
import entities.Users;
import helper.ExtendedSubject;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import manager.CurrentSubjectSessionBeanRemote;
import manager.IndexbookSessionBeanRemote;
import manager.LessionSessionBeanRemote;
import manager.SemesterSessionBeanRemote;

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

    int itemsPerPage = 15;
    int currentPage;
    HtmlDataTable tableComponent;
    String pagingDirection;
    String sortDirection;
    String from, to;
    int méret;

    List<ExtendedSubject> listSubject;
    List<Subject> openSubject, doneSubject, signedSubject, pickedSubject;
    Users user;
    Semester semester;

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

    }

    public List<ExtendedSubject> getListSubject() {
        int fromIndex = currentPage*itemsPerPage;
        int toIndex = fromIndex+itemsPerPage;

        openSubject = this.indexbookSessionBean.allSubject(user);
        doneSubject = this.indexbookSessionBean.doneSubject(user);
        signedSubject = this.indexbookSessionBean.signedSubject(user);
        pickedSubject = this.indexbookSessionBean.pickedSubject(user);

        listSubject = new LinkedList<ExtendedSubject>();
        boolean did = false;
        for(Subject open: openSubject){
            did = false;
            for(Subject done: doneSubject){
                if(open.getId().equals(done.getId())){
                    listSubject.add(new ExtendedSubject(open,true,true,false));
                    did = true;
                    break;
                }
            }
            if(!did){
                for(Subject signed: signedSubject){
                    if(open.getId().equals(signed.getId())){
                        listSubject.add(new ExtendedSubject(open,true,false,false));
                        did = true;
                        break;
                    }
                }
            }
            if(!did)
                listSubject.add(new ExtendedSubject(open,false,false,false));
        }
        for(ExtendedSubject akt: listSubject){
            for(Subject picked: pickedSubject)
                if(akt.getSubject().getId().equals(picked.getId()))
                    akt.setPicked(true);
        }

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

    public void setListSubjects(List<ExtendedSubject> listSubject) {
        this.listSubject = listSubject;
    }

    public Semester getSemester() {
        return this.semesterSessionBean.findOpen();
    }

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


    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 String backAction(){
        return "menus";
    }

    public String createAction(){
        CurrentSubject cs;
        List<Lesson> listLessonL, listLessonP;
        boolean update = false;
        ExtendedSubject selected = (ExtendedSubject) tableComponent.getRowData();
        cs = this.currentSubjectSessionBean.findByIDs(getSemester(), selected.getSubject()).get(0);
        Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        m.put("currentsubject", cs);

        listLessonL = this.lessionSessionBean.findByL(cs);
        listLessonP = this.lessionSessionBean.findByP(cs);
        m.put("listLessonL", listLessonL);
        m.put("listLessonP", listLessonP);
        m.put("update", update);
        m.put("signature", selected.isSignature());
        return "createkurzus";
    }

     protected void addMessage(String message){
        FacesMessage m = new FacesMessage(message);
        FacesContext.getCurrentInstance().addMessage(null, m);
    }

}
