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

package manager;

import entities.CurrentSubject;
import entities.Indexbook;
import entities.Lesson;
import entities.LessonStudent;
import entities.Semester;
import entities.Subject;
import entities.Users;
import helper.DatabaseException;
import helper.LessonType;
import helper.SchoolYearState;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Szilárd
 */
@Stateless
public class IndexbookSessionBean implements IndexbookSessionBeanRemote {
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @PersistenceContext
    private EntityManager em;

    public void newIndexbook(Indexbook i) {
        em.persist(i);
    }

    public void deleteByStudentANDSemesterANDSubject(Users u, Semester sem, Subject sub) throws DatabaseException{
        Indexbook index;
        CurrentSubject indexCurSub;
        List<Lesson> p, l;
        Long lessonP = 0L, lessonL = 0L, studentLessonP = 0L, studentLessonL = 0L;


        Query q = em.createNamedQuery("Indexbook.findBySemesterAndUserANDSubjectId");
        q.setParameter("studentId", u);
        q.setParameter("semesterId", sem);
        q.setParameter("subjectId", sub);
        index =(Indexbook) q.getResultList().get(0);

        if(index.getMark()>0 || index.getSignature()>0)
            throw new DatabaseException("Nem törölhető");

        //if(true)
        //    throw new DatabaseException("eddig ok");

        //CurrentSubjectSessionBean c = new CurrentSubjectSessionBean();
        //indexCurSub = c.findByIDs(sem, sub).get(0);
        q = em.createNamedQuery("CurrentSubject.findBySemesterAndSubject");
        q.setParameter("semesterId", sem);
        q.setParameter("subjectId", sub);
        indexCurSub = (CurrentSubject)q.getResultList().get(0);


        //LessionSessionBean s = new LessionSessionBean();
        //l = s.findByL(indexCurSub);
        //p = s.findByP(indexCurSub);
        q = em.createNamedQuery("Lesson.findType");
        q.setParameter("lessonType", LessonType.LECTURE);
        q.setParameter("currentSubjectId", indexCurSub);
        l = q.getResultList();
        
        q = em.createNamedQuery("Lesson.findType");
        q.setParameter("lessonType", LessonType.PRACTICE);
        q.setParameter("currentSubjectId", indexCurSub);
        p = q.getResultList();

        
 
        //LessonStudentSessionBean ls = new LessonStudentSessionBean();
        List<LessonStudent> temp;
        q = em.createNamedQuery("LessonStudent.findByLessonId");
        for(int i =0; i<l.size();i++){
            q.setParameter("lessonId", l.get(i));
            q.setParameter("studentId", u);
            temp = q.getResultList();
            if(!temp.isEmpty()){
                lessonL = l.get(i).getId();
                studentLessonL = temp.get(0).getId();
            }

        }
        if(!p.isEmpty()){
            for(int i =0; i<p.size();i++){
                q.setParameter("lessonId", p.get(i));
                q.setParameter("studentId", u);
                temp = q.getResultList();
                if(!temp.isEmpty()){
                    lessonP = p.get(i).getId();
                    studentLessonP = temp.get(0).getId();
                }
            }
        }

        //if(true)
        //    throw new DatabaseException(Long.toString(lessonP));
 

        q = em.createNamedQuery("Lesson.updateDec");
        q.setParameter("id", lessonL);
        q.executeUpdate();

        q = em.createNamedQuery("LessonStudent.delete");
        q.setParameter("id", studentLessonL);
        q.executeUpdate();

        if(!p.isEmpty()){
            q = em.createNamedQuery("Lesson.updateDec");
            q.setParameter("id", lessonP);
            q.executeUpdate();

            q = em.createNamedQuery("LessonStudent.delete");
            q.setParameter("id", studentLessonP);
            q.executeUpdate();
        }
        q = em.createNamedQuery("Indexbook.delete");
        q.setParameter("id", index.getId());
        q.executeUpdate();
  
  }


    public List<Subject> allSubject(Users u) {
        Semester aktivSemester;
        Query q;
        q = em.createNamedQuery("Semester.findOpen");
        q.setParameter("state", SchoolYearState.OPEN);
        aktivSemester =(Semester) q.getResultList().get(0);

        q = em.createNamedQuery("CurrentSubject.findByOpen");
        q.setParameter("semesterId", aktivSemester);
        return q.getResultList();
    }


    public List<Subject> signedSubject(Users u) {
        Query q = em.createNamedQuery("Indexbook.findByUserAndSigned");
        q.setParameter("studentId", u);
        q.setParameter("signature", 1);
        q.setParameter("mark", 2);
        return q.getResultList();
    }

    public List<Subject> doneSubject(Users u) {
        Query q = em.createNamedQuery("Indexbook.findByUserAndMark");
        q.setParameter("studentId", u);
        q.setParameter("mark", 1);
        return q.getResultList();
    }

    public List<Subject> pickedSubject(Users u) {
        Semester aktivSemester;
        Query q;
        q = em.createNamedQuery("Semester.findOpen");
        q.setParameter("state", SchoolYearState.OPEN);
        aktivSemester =(Semester) q.getResultList().get(0);

        q = em.createNamedQuery("Indexbook.findBySemesterAndUser");
        q.setParameter("studentId", u);
        q.setParameter("semesterId", aktivSemester);
        return q.getResultList();
    }

    public List<Indexbook> findByStudentIdANDSemesterId(Users u, Semester sem) {
        Query q = em.createNamedQuery("Indexbook.findBySAndU");
        q.setParameter("studentId", u);
        q.setParameter("semesterId", sem);
        return q.getResultList();
    }
    public Indexbook findBySemesterAndUserANDSubjectId(Users u, Semester sem, Subject sub){
        Query q = em.createNamedQuery("Indexbook.findBySemesterAndUserANDSubjectId");
        q.setParameter("studentId", u);
        q.setParameter("semesterId", sem);
        q.setParameter("subjectId", sub);
        return (Indexbook) q.getResultList().get(0);
    }

    public void updateSign(String teacher, int sign, Long id){
        Query q = em.createNamedQuery("Indexbook.updateSign");
        q.setParameter("signed", teacher);
        q.setParameter("signature", sign);
        q.setParameter("id", id);
        q.executeUpdate();
    }

    public void updateMark(String teacher, int mark, Long id){
        Query q = em.createNamedQuery("Indexbook.updateMark");
        q.setParameter("marked", teacher);
        q.setParameter("mark", mark);
        q.setParameter("id", id);
        q.executeUpdate();
    }

      
}
