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

package student;

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

/**
 *
 * @author Szilárd
 */
@ManagedBean(name="ListDiplomas")
@RequestScoped
public class ListDiplomas {
    @EJB
    private ThesisStudentSessionBeanRemote thesisStudentSessionBean;
    @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;
    Semester semester;
    Boolean [] checked;
    Thesis t;
    int db;
    Users user;
    boolean update;
    /** Creates a new instance of ListDiplomas */
    public ListDiplomas() {
        Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        this.db = (Integer) m.get("db");
        user = (Users) m.get("felhasznalo");
        update = (Boolean) m.get("update");
        t = (Thesis) m.get("selected");
        checked = new Boolean[db];
        for(int i = 0;i<db;i++){
            checked[i] = false;
        }
    }


    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 null;
    }

    public String nextPage(){
        ++currentPage;
        return null;
    }

    public HtmlDataTable getTableComponent() {
        return tableComponent;
    }

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

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

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

    public Boolean[] getChecked() {
        return checked;
    }

    public void setChecked(Boolean[] checked) {
        this.checked = checked;
    }

     public String backAction(){         
         return "listdiploma";
     }
     
     public String chooseAction(){
        Thesis selected = (Thesis) tableComponent.getRowData();
        Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        m.put("thesis", selected);
        return "listdiplomasmore";
    }

     public String saveAction(){
         int piece = 0, no = 0;
         for(int i=0;i<db;i++)
            if(checked[i]){
                ++piece;
                no = i;
            }
         if(piece<1){
             addMessage("Legalább egy témát választani kell");
             return null;
         }
         if(piece>1){
             addMessage("Csak egy témát választhat");
             return null;
         }
         if(update && t.getId().equals(listThesis.get(no).getId())){
             addMessage("Nem történt módosítás");
             return null;
         }
         if(!update){
            ThesisStudent ts = new ThesisStudent();
            ts.setThesisId(listThesis.get(no));
            ts.setStudentId(user);
            this.thesisStudentSessionBean.newThesisStudent(ts);
            return "listdiploma";
         }
         else{
           ThesisStudent ts = this.thesisStudentSessionBean.findByStudentID(user);
           ts.setThesisId(listThesis.get(no));
           this.thesisStudentSessionBean.update(ts);
           return "listdiploma";
         }
     }

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