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

package admin;

import entities.Semester;
import helper.SchoolYearState;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
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 javax.faces.model.SelectItem;
import manager.SemesterSessionBeanRemote;

/**
 *
 * @author Szilárd
 */
@ManagedBean(name="ListSemester")
@RequestScoped
public class ListSemester {

    @EJB
    private SemesterSessionBeanRemote semesterSessionBean;

    int itemsPerPage = 15;
    int currentPage;
    HtmlDataTable tableComponent;
    String pagingDirection;
    String sortDirection;
    String from, to;
    Semester sem;
    List<Semester> listSemester;
    ArrayList listState;
    Integer [] state1, state2;
    boolean first;

    public ListSemester() {
        listSemester = new LinkedList<Semester>();
        listState = new ArrayList();
        listState.add(new SelectItem(0,"Lezárt"));
        listState.add(new SelectItem(1,"Aktív"));
        listState.add(new SelectItem(2,"Új"));

        state1 = new Integer[14];
        state2 = new Integer[14];
        first = true;
    }

    public List<Semester> getListSemester() {
        if(this.semesterSessionBean.allSemester().isEmpty()){
            String tanév;
            int év;
            SchoolYearState syt = SchoolYearState.CLOSED;
            for(int i=2003;i<2012;i++)
                for(int j=1;j<3;j++){
                    sem = new Semester();
                    év = i+1;
                    tanév = i+"/"+év+"/"+j;
                    sem.setSchoolYear(tanév);

                if(tanév.equals("2009/2010/2"))
                    syt = SchoolYearState.OPEN;
                if(i == 2010)
                    syt = SchoolYearState.NEW;

                sem.setSchoolYearState(syt);
                this.semesterSessionBean.newSemester(sem);
            }
        }
        int fromIndex = currentPage*itemsPerPage;
        int toIndex = fromIndex+itemsPerPage;

        listSemester = this.semesterSessionBean.allSemester();

        if(first){
            first = false;
            for(int i=0;i<14;i++){
                switch(listSemester.get(i).getSchoolYearState()){
                    case CLOSED :
                        state1[i] = 0;
                        state2[i] = 0;
                        break;
                    case OPEN :
                        state1[i] = 1;
                        state2[i] = 1;
                        break;
                    case NEW :
                        state1[i] = 2;
                        state2[i] = 2;
                        break;
                }
            }

        }

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

    public int getCurrentPage() {
        return currentPage;
    }

    protected int calculatePageCount(){
        return (listSemester.size()/itemsPerPage)+((listSemester.size()%itemsPerPage) != 0 ? 1 : 0);
    }

    public int getPageCount(){
        return calculatePageCount();
    }

    public String getPagingDirection() {
        return pagingDirection;
    }

    public void setPagingDirection(String pagingDirection) {
        this.pagingDirection = pagingDirection;
    }

    public void setListSemester(List<Semester> listSemester) {
        this.listSemester = listSemester;
    }

    public String page(){
        if(pagingDirection.equals("<") && currentPage!=0)
            currentPage--;
        else if (pagingDirection.equals(">") && currentPage+1<calculatePageCount())
            currentPage++;
        return null;
    }

    public HtmlDataTable getTableComponent() {
        return tableComponent;
    }

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

    public ArrayList getListState() {
        return listState;
    }

    public void setListState(ArrayList listState) {
        this.listState = listState;
    }

    public Integer[] getState1() {
        return state1;
    }

    public void setState1(Integer[] state1) {
        this.state1 = state1;
    }

    public Integer[] getState2() {
        return state2;
    }

    public void setState2(Integer[] state2) {
        this.state2 = state2;
    }


    


    public String backAction(){
        listSemester.clear();
        return "menua";
    }

    public String saveAction(){
        int c = 0, w = 0;
        for(int i=0;i<14;i++){
            if(state1[i]!=state2[i]){
                ++c;
                w = i;
            }
        }
        if(c>1){
            addMessage("Csak egyet változtathat");
            first = true;
            return null;
        }
        if(state1[w] !=1){
            addMessage("Csak aktívat választhat");
            first = true;
            return null;
        }
        Semester s = this.semesterSessionBean.findOpen();
        if(s !=null){
            sem = s;
            sem.setSchoolYearState(SchoolYearState.CLOSED);
            this.semesterSessionBean.update(sem);
        }
        Semester semm = listSemester.get(w);
        semm.setSchoolYearState(SchoolYearState.OPEN);
        this.semesterSessionBean.update(semm);
        return null;
    }

    public String setOpenAction(){
        Semester s = this.semesterSessionBean.findOpen();
        if(s !=null){
        //if(!s.isEmpty()){
        //    sem = s.get(0);
            sem = s;
            sem.setSchoolYearState(SchoolYearState.CLOSED);
            this.semesterSessionBean.update(sem);
        }
        sem = (Semester) tableComponent.getRowData();
        sem.setSchoolYearState(SchoolYearState.OPEN);
        this.semesterSessionBean.update(sem);

        return null;
    }

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

}
