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

package student;

import entities.CurrentSubject;
import entities.Lesson;
import entities.Semester;
import entities.Subject;
import entities.Users;
import helper.DatabaseException;
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.LessonStudentSessionBeanRemote;
import manager.SemesterSessionBeanRemote;

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


    int itemsPerPage = 15;
    int currentPage;
    HtmlDataTable tableComponent;
    String pagingDirection;
    String sortDirection;
    String from, to;
    Subject sub;
    List<Subject> listSubject;
    Semester semester;
    Users user;

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

    public List<Subject> getListSubject() {

        int fromIndex = currentPage*itemsPerPage;
        int toIndex = fromIndex+itemsPerPage;

        listSubject = this.indexbookSessionBean.pickedSubject(user);

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

    public void setListSubject(List<Subject> 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 Semester getSemester() {
        semester = this.semesterSessionBean.findOpen();
        return semester;
    }

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

    public String backAction(){
        return "menus";
    }

     public String deleteAction(){
         try{
             Subject selected = (Subject) tableComponent.getRowData();
             this.indexbookSessionBean.deleteByStudentANDSemesterANDSubject(user, getSemester(), selected);
         }catch(DatabaseException e){
             addMessage(e.getMessage());
         }
         return null;

     }

    public String editAction(){
        
        CurrentSubject cs;
        boolean update = true, signature;
        List<Lesson> listLessonL, listLessonP;
        Long lessonidL = 0L, lessonidP = 0L, lessonStudentidL = 0L, lessonStudentidP = 0L;

        Subject selected = (Subject) tableComponent.getRowData();
        cs = this.currentSubjectSessionBean.findByIDs(getSemester(), selected).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);

        for(int i = 0; i < listLessonL.size() ;i++)
            if(this.lessonStudentSessionBean.findByLessonIdAndStudentId(listLessonL.get(i),user)){
                lessonidL = listLessonL.get(i).getId();
                lessonStudentidL = this.lessonStudentSessionBean.findByLessonId(listLessonL.get(i),user).getId();
             }

        for(int i = 0; i < listLessonP.size(); i++)
            if(this.lessonStudentSessionBean.findByLessonIdAndStudentId(listLessonP.get(i),user)){
                lessonidP = listLessonP.get(i).getId();
                lessonStudentidP = this.lessonStudentSessionBean.findByLessonId(listLessonP.get(i),user).getId();
            }
        signature = lessonidP.equals(0L) ? true : false;

        m.put("lessonidL", lessonidL);
        m.put("lessonStudentidL", lessonStudentidL);
        m.put("signature", signature);
        if(!signature){
            m.put("lessonidP", lessonidP);
            m.put("lessonStudentidP", lessonStudentidP);
        }

        return "createkurzus";
        
        
    }

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

}
