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

package student;

import entities.CurrentSubject;
import entities.Indexbook;
import entities.Lesson;
import entities.LessonStudent;
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.IndexbookSessionBeanRemote;
import manager.LessionSessionBeanRemote;
import manager.LessonStudentSessionBeanRemote;

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

    CurrentSubject currentSubject;
    List<Lesson> listLessonL, listLessonP;
    HtmlDataTable tableComponentL, tableComponentP;
    Boolean[] checkedL, checkedP;
    boolean signature, update;
    Long lessonStudentidL,lessonStudentidP,lessonidL,lessonidP;
    Users user;
    /** Creates a new instance of CreateKurzus */

    public CreateKurzus() {
         Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
         this.currentSubject =  (CurrentSubject) m.get("currentsubject");
         this.update = (Boolean)m.get("update");
         this.listLessonL = (List<Lesson>) m.get("listLessonL");
         this.listLessonP = (List<Lesson>) m.get("listLessonP");
         this.user = (Users) m.get("felhasznalo");
         this.signature = (Boolean) m.get("signature");

         checkedL = new Boolean[listLessonL.size()];
         checkedP = new Boolean[listLessonP.size()];

         if(update){
            this.lessonidL = (Long) m.get("lessonidL");
            this.lessonStudentidL = (Long) m.get("lessonStudentidL");

            for(int i =0;i<listLessonL.size();i++){
                //addMessage(Long.toString(listLessonL.get(i).getId())+" = "+Long.toString(lessonidL));
                if(listLessonL.get(i).getId().equals(lessonidL))
                    checkedL[i] = true;
                else
                    checkedL[i] = false;
            }

            //addMessage(Boolean.toString(signature));
            //addMessage(Long.toString(lessonidL));
            //addMessage(Long.toString(lessonStudentidL));
                

            if(!signature){
                this.lessonidP = (Long) m.get("lessonidP");
                this.lessonStudentidP = (Long) m.get("lessonStudentidP");
                for(int i =0;i<listLessonP.size();i++){
                    if(listLessonP.get(i).getId().equals(lessonidP))
                        checkedP[i] = true;
                    else
                        checkedP[i] = false;
                }
            }
         }
         else{
            for(int i =0;i<listLessonL.size();i++)
            checkedL[i]=false;

            for(int i =0;i<listLessonP.size();i++)
                checkedP[i]=false;
            
         }
         

    }

    public boolean isUpdate() {
        return update;
    }

    public void setUpdate(boolean update) {
        this.update = update;
    }

    

    public CurrentSubject getCurrentSubject() {
        return currentSubject;
    }

    public void setCurrentSubject(CurrentSubject currentSubject) {
        this.currentSubject = currentSubject;
    }

    public List<Lesson> getListLessonL() {
        return listLessonL;
    }

    public void setListLessonL(List<Lesson> listLessonL) {
        this.listLessonL = listLessonL;
    }

    public List<Lesson> getListLessonP() {
        return listLessonP;
    }

    public void setListLessonP(List<Lesson> listLessonP) {
        this.listLessonP = listLessonP;
    }

    public HtmlDataTable getTableComponentL() {
        return tableComponentL;
    }

    public void setTableComponentL(HtmlDataTable tableComponentL) {
        this.tableComponentL = tableComponentL;
    }

    public HtmlDataTable getTableComponentP() {
        return tableComponentP;
    }

    public void setTableComponentP(HtmlDataTable tableComponentP) {
        this.tableComponentP = tableComponentP;
    }

    public Boolean[] getCheckedL() {
        return checkedL;
    }

    public void setCheckedL(Boolean[] checkedL) {
        this.checkedL = checkedL;
    }

    public Boolean[] getCheckedP() {
        return checkedP;
    }

    public void setCheckedP(Boolean[] checkedP) {
        this.checkedP = checkedP;
    }

    public String backAction(){        
        String url;
        url = update ? "listindex" : "listkurzus";
        return url;
    }

    public String saveAction(){
        int Lsize = 0, Psize = 0, iL = 0, iP = 0;
        for(int i=0; i<checkedL.length;i++){            
            if(checkedL[i]){
                ++Lsize;
                iL = i;
            }
                
        }
        for(int i=0; i<checkedP.length;i++){
            if(checkedP[i]){
                ++Psize;
                iP = i;
            }
        }
        if(Lsize == 0){
            addMessage("Előadást kötelező felvenni");
            return null;
        }
        if(Psize == 0 && !signature){
            addMessage("Gyakorlatot kötelező felvenni");
            return null;
        }
        if(Lsize > 1 ){
            addMessage("Csak egy előadást választhat");
            for(int i =0;i<listLessonL.size();i++)
            checkedL[i]=false;
            return null;
        }
        if(Psize > 1 ){
            addMessage("Csak egy gyakorlatot választhat");
            for(int i =0;i<listLessonP.size();i++)
            checkedP[i]=false;
            return null;
        }
        if(signature && Psize>0){
            addMessage("Már van aláírása, ezért nem vehet fel gyakorlatot");
            for(int i =0;i<listLessonP.size();i++)
                checkedP[i]=false;
            return null;
        }

        if(Lsize == 1 && !this.lessionSessionBean.findById(listLessonL.get(iL).getId()))
           addMessage("Az előadás betelt");

        if(Psize == 1 && !this.lessionSessionBean.findById(listLessonP.get(iP).getId()))
           addMessage("A gyakorlat betelt");

        LessonStudent lsL = new LessonStudent();
        Indexbook indexbook = new Indexbook();
        Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        user = (Users) map.get("felhasznalo");

        lsL.setLessonId(listLessonL.get(iL));
        lsL.setStudentId(user);
        this.lessonStudentSessionBean.newLessonStudent(lsL);
        this.lessionSessionBean.updateInc(listLessonL.get(iL).getId());
        indexbook.setSemesterId(currentSubject.getSemesterId());
        indexbook.setSubjectId(currentSubject.getSubjectId());
        indexbook.setStudentId(user);
        indexbook.setMark(0);
        indexbook.setSignature(0);
        this.indexbookSessionBean.newIndexbook(indexbook);

        if(!signature){
            LessonStudent lsP = new LessonStudent();
            lsP.setLessonId(listLessonP.get(iP));
            lsP.setStudentId(user);
            this.lessonStudentSessionBean.newLessonStudent(lsP);
            this.lessionSessionBean.updateInc(listLessonP.get(iP).getId());
        }

        return "listkurzus";
    }

    public String updateAction(){
        int Lsize = 0, Psize = 0, iL = 0, iP = 0;
        for(int i=0; i<checkedL.length;i++){
            if(checkedL[i]){
                ++Lsize;
                iL = i;
            }
        }
        for(int i=0; i<checkedP.length;i++){
            if(checkedP[i]){
                ++Psize;
                iP = i;
            }
        }
        if(Lsize == 0){
            addMessage("Előadást kötelező felvenni");
            return null;
        }
        if(Psize == 0 && !signature){
            addMessage("Gyakorlatot kötelező felvenni");
            return null;
        }
        if(Lsize > 1 ){
            addMessage("Csak egy előadást választhat");
            for(int i =0;i<listLessonL.size();i++)
            checkedL[i]=false;
            return null;
        }
        if(Psize > 1 ){
            addMessage("Csak egy gyakorlatot választhat");
            for(int i =0;i<listLessonP.size();i++)
            checkedP[i]=false;
            return null;
        }
        if(signature && Psize>0){
            addMessage("Már van aláírása, ezért nem vehet fel gyakorlatot");
            for(int i =0;i<listLessonP.size();i++)
                checkedP[i]=false;
            return null;
        }
        if(listLessonL.get(iL).getId().equals(lessonidL) &&
           listLessonP.get(iP).getId().equals(lessonidP)){
            addMessage("Nem történt változás");
            return null;
        }

        if(Lsize == 1 && !this.lessionSessionBean.findById(listLessonL.get(iL).getId())
           && !listLessonL.get(iL).getId().equals(lessonidL)){
           addMessage("Az előadás betelt");
           return null;
        }

        if(Psize == 1 && !this.lessionSessionBean.findById(listLessonP.get(iP).getId())
           && !listLessonP.get(iP).getId().equals(lessonidP)){
           addMessage("A gyakorlat betelt");
           return null;
        }

        if(!listLessonL.get(iL).getId().equals(lessonidL)){
            this.lessionSessionBean.updatedec(lessonidL);
            this.lessionSessionBean.updateInc(listLessonL.get(iL).getId());
            LessonStudent temp = this.lessonStudentSessionBean.findId(lessonStudentidL);
            this.lessonStudentSessionBean.update(temp.getId(), listLessonL.get(iL));
        }
        if(!signature && !listLessonP.get(iP).getId().equals(lessonidP)){
            this.lessionSessionBean.updatedec(lessonidP);
            this.lessionSessionBean.updateInc(listLessonP.get(iP).getId());
            LessonStudent temp = this.lessonStudentSessionBean.findId(lessonStudentidP);
            this.lessonStudentSessionBean.update(temp.getId(), listLessonP.get(iP));
        }
        return "listindex";
    }


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

}
