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

package admin;

import entities.CurrentSubject;
import entities.Semester;
import entities.Subject;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
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.SemesterSessionBeanRemote;
import manager.SubjectSessionBeanRemote;

/**
 *
 * @author Szilárd
 */
@ManagedBean(name="ListSubject")
@RequestScoped
public class ListSubject {
    @EJB
    private SemesterSessionBeanRemote semesterSessionBean;
    @EJB
    private CurrentSubjectSessionBeanRemote currentSubjectSessionBean;
    @EJB
    private SubjectSessionBeanRemote subjectSessionBean;


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

    /** Creates a new instance of ListSubject */
    public ListSubject() {
    }

    public List<Subject> getListSubject() {
        if(this.subjectSessionBean.allSubjects().isEmpty()){
            Random random = new Random();
            try{
                Scanner scanner = new Scanner(new File("D:\\Subject.txt"));
                while(scanner.hasNextLine()){
                    sub = new Subject();
                    sub.setName(scanner.nextLine());
                    sub.setKredit(random.nextInt(3)+2);
                    this.subjectSessionBean.newSubject(sub);
                }
            }catch(IOException e){}
        }
        int fromIndex = currentPage*itemsPerPage;
        int toIndex = fromIndex+itemsPerPage;

        
        List<Subject> listS = this.currentSubjectSessionBean.findAktiv();
        listSubject = this.subjectSessionBean.allSubjects();
        listSubject.removeAll(listS);

        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 "menua";
    }

    public String setOpenAction(){
        Semester sem;
        CurrentSubject cs = new CurrentSubject();
        Subject selected = (Subject) tableComponent.getRowData();
        sem = this.semesterSessionBean.findOpen();
        cs.setSemesterId(sem);
        cs.setSubjectId(selected);
        this.currentSubjectSessionBean.newCurrentSubject(cs);
        cs = this.currentSubjectSessionBean.findByIDs(sem, selected).get(0);
        Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        m.put("currentsubject", cs);

        return "createsubject.jsp";
    }

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

}
