package ru.ulstu.is.cbapp.service;

import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.cbapp.models.DrivingSchool;
import ru.ulstu.is.cbapp.models.Student;
import ru.ulstu.is.cbapp.models.Category;

import java.util.List;

@Service
public class StudentService {
    @PersistenceContext
    private EntityManager em;

    @Transactional
    public Student addStudent(String surname, String name, String phoneNumber) {
        if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
            throw new IllegalArgumentException("Student's data is null or empty");
        }
        final Student student = new Student(surname, name, phoneNumber);
        em.persist(student);
        return student;
    }

    @Transactional
    public Student findStudent(Long id) {
        final Student student = em.find(Student.class, id);
        if (student == null) {
            throw new EntityNotFoundException(String.format("Student with id [%s] is not found", id));
        }
        return student;
    }

    @Transactional(readOnly = true)
    public List<Student> findAllStudents() {
        return em.createQuery("select e from Student e", Student.class)
                .getResultList();
    }

    @Transactional
    public Student updateStudent(Long id, String surname, String name, String phoneNumber) {
        if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
            throw new IllegalArgumentException("Student's data is null or empty");
        }
        final Student currentStudent = findStudent(id);
        currentStudent.setName(name);
        currentStudent.setSurname(surname);
        currentStudent.setPhoneNumber(phoneNumber);

        return em.merge(currentStudent);
    }

    @Transactional
    public Student deleteStudent(Long id) {
        final Student student = findStudent(id);
        em.remove(student);
        return student;
    }

    @Transactional
    public void deleteAllStudents() {
        em.createQuery("delete from Student").executeUpdate();
    }

    @Transactional
    public void addDrivingSchool(Long id, DrivingSchool d) {
        final Student student = findStudent(id);
        student.setDrivingSchool(d);
        em.merge(student);
    }

    @Transactional
    public void deleteDrivingSchool(Long id) {
        final Student student = findStudent(id);
        student.deleteDrivingSchool();
        em.merge(student);
    }

    @Transactional
    public void addCategory(Long id, Category p) {
        Student e = findStudent(id);
        e.addNewCategory(p);
        System.out.println(e.getCategories().size());
        em.merge(e);
    }

    @Transactional
    public void deleteCategory(Long id, Category p) {
        Student e = findStudent(id);
        e.removeCategory(p);
        System.out.println("Количество должностей после удаления: " + e.getCategories().size());
        em.merge(e);
        em.flush();
    }
}
