package ejb.profile;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import TO.FamilyDoctorTO;
import TO.MedicalSpecialtyTO;
import TO.PatientTO;
import TO.PrimaryHealthCareCenterTO;
import TO.SpecialistDoctorTO;
import jpa.FamilyDoctorJPA;
import jpa.MedicalSpecialtyJPA;
import jpa.PatientJPA;
import jpa.PrimaryHealthCareCenterJPA;
import jpa.SpecialistDoctorJPA;

/**
 * 
 * @author Marcos García Núñez (mgarcianun@uoc.edu)
 *
 */
@Stateless
public class ProfileFacadeBean implements ProfileFacadeRemote {

	// Persistence Unit Context
	@PersistenceContext(unitName = "MyHealth")
	private EntityManager entman;

	public PatientTO changeFamilyDoctor(Integer id, Integer ProfessionalNumberId) {
		PatientTO paTO = null;

		FamilyDoctorJPA fd = entman.find(FamilyDoctorJPA.class, ProfessionalNumberId);
		PatientJPA pa = entman.find(PatientJPA.class, id);

		if (fd != null && pa != null) {
			pa.setFamilyDoctor(fd);
			paTO = new PatientTO();

		}

		return paTO;
	}

	public PatientTO registerPatient(Integer id, String nif, String name, String surname, String password, String email) {
		PatientTO paTO = null;

		PatientJPA ms = new PatientJPA(id, nif, name, surname, password, email);
		entman.persist(ms);
		paTO = new PatientTO(ms.getId(), ms.getNif(), ms.getName(), ms.getSurname(), ms.getPassword(), ms.getEmail());

		return paTO;
	}

	public SpecialistDoctorTO registerSpecialistDoctor(Integer id, String nif, String name, String surname, String password, String email, MedicalSpecialtyTO specialty) {
		SpecialistDoctorTO sdTO = null;

		MedicalSpecialtyJPA ms = entman.find(MedicalSpecialtyJPA.class, specialty.getName());

		// TODO: Lanzar error si no se encuentra la especialidad.
		if (ms != null) {
			SpecialistDoctorJPA sd = new SpecialistDoctorJPA(id, nif, name, surname, password, email);
			entman.persist(sd);
			sdTO = new SpecialistDoctorTO(sd.getId(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail());
		}
		
		return sdTO;
	}

	public FamilyDoctorTO registerFamilyDoctor1(Integer id, String nif, String name, String surname, String password, String email, PrimaryHealthCareCenterTO cap) {
		FamilyDoctorTO fdTO = null;

		PrimaryHealthCareCenterJPA phcC = entman.find(PrimaryHealthCareCenterJPA.class, cap.getName());

		// TODO: Lanzar error si no encontramos el cap!!!!!
		if (phcC != null) {

			FamilyDoctorJPA fd = new FamilyDoctorJPA(id, nif, name, surname, password, email);
			fd.setPrimaryHealthCareCenter(phcC);

			entman.persist(fd);

			fdTO = new FamilyDoctorTO(fd.getId(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail());
		}

		return fdTO;
	}

	public PatientTO updatePacientData(Integer id, String nif, String name, String surname, String password, String email) {
		PatientJPA fd = entman.find(PatientJPA.class, id);
		PatientTO ptTO = null;

		if (fd != null) {
			fd.setNif(nif);
			fd.setName(name);
			fd.setSurname(surname);
			fd.setPassword(password);
			fd.setEmail(email);

			entman.persist(fd);

			ptTO = new PatientTO(fd.getId(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail());
		}

		return ptTO;
	}

	public SpecialistDoctorTO updateSpecialistDoctorData(Integer id, String nif, String name, String surname, String password, String email, MedicalSpecialtyTO specialty) {
		SpecialistDoctorTO sdTO = null;

		SpecialistDoctorJPA sd = entman.find(SpecialistDoctorJPA.class, id);
		MedicalSpecialtyJPA ms = entman.find(MedicalSpecialtyJPA.class, specialty.getName());

		// TODO: Lanzar error al no encontrar la especialidad indicada ?????

		if (sd != null && ms != null) {
			sd.setNif(nif);
			sd.setName(name);
			sd.setSurname(surname);
			sd.setPassword(password);
			sd.setEmail(email);
			sd.setMedicalSpecialty(ms);

			entman.persist(sd);

			sdTO = new SpecialistDoctorTO(sd.getId(), sd.getNif(), sd.getName(), sd.getSurname(), sd.getPassword(), sd.getEmail());
		}

		return sdTO;
	}

	public FamilyDoctorTO updateFamilyDoctorData(Integer id, String nif, String name, String surname, String password, String email, String cap) {
		FamilyDoctorJPA fd = entman.find(FamilyDoctorJPA.class, id);
		FamilyDoctorTO fdTO = null;

		if (fd != null) {
			fd.setNif(nif);
			fd.setName(name);
			fd.setSurname(surname);
			fd.setPassword(password);
			fd.setEmail(email);

			// TODO: Es posible actualizar el cap? ¿No debería utilizar el método
			// changePrimaryHealthCareCenter?
			// cap debería ser PrimaryHealthCareCenterTO newCenter
			// fd.setPrimaryHealthCareCenter(cap);

			entman.persist(fd);

			fdTO = new FamilyDoctorTO(fd.getId(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail());
		}

		return fdTO;
	}

	public FamilyDoctorTO changePrimaryHealthCareCenter(Integer FamilyDoctorId, PrimaryHealthCareCenterTO newCenter) {
		FamilyDoctorTO fdTO = null;

		PrimaryHealthCareCenterJPA phcC = entman.find(PrimaryHealthCareCenterJPA.class, newCenter);
		FamilyDoctorJPA fd = entman.find(FamilyDoctorJPA.class, FamilyDoctorId);

		if (phcC != null && fd != null) {
			fd.setPrimaryHealthCareCenter(phcC);
			fdTO = new FamilyDoctorTO(fd.getId(), fd.getNif(), fd.getName(), fd.getSurname(), fd.getPassword(), fd.getEmail());

		}

		return fdTO;
	}

}
