package managedbean.visit;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalTime;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import TO.FamilyDoctorTO;
import TO.PatientTO;
import managedbean.common.ManagedBeanBase;
import managedbean.common.SessionUtils;

@Named("AddVisit")
@ViewScoped
public class AddVisitMBean extends ManagedBeanBase implements Serializable {

	private static final long serialVersionUID = 1L;

	private Integer id;
	private LocalDate date;
	private LocalTime time;
	private String observations;
	private String result;
	private String patientDisplayName;
	private String familyDoctorDisplayName;
	private PatientTO patient;

	public AddVisitMBean() {
	}

	@PostConstruct
	public void init() {
		// El usuario actual es un medico de familia, recuperamos su Id de la sessión actual
		Integer patientId = Integer.valueOf(SessionUtils.getUserId());

		this.patient = this.getRemoteManagerCommon().findPatientById(patientId);

		if (this.patient.getFamilyDoctor() != null)
			this.familyDoctorDisplayName = this.patient.getFamilyDoctor().getDisplayName();
		else
			this.familyDoctorDisplayName = null;

		this.date = LocalDate.now();
	}

	public void saveData() {
		// Comprobamos que la fecha fijada para la visita no sea anterior a la actual
		int error = 0;
		LocalDate today = LocalDate.now();

		if (this.patient.getFamilyDoctor() == null) {
			this.addFacesMessage("visitForm:medico", FacesMessage.SEVERITY_ERROR, "Médico de familia no asignado",
					"Es necesario que tenga un médico de familia asignado, por favor, cambie el médico de familia que tiene asignado");
			error++;
		}
		if (this.date.isBefore(today)) {
			this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Fecha incorrecta", "La cita fijada es anterior al momento actual");
			error++;
		}
		if (this.date.equals(today) && this.time.isBefore(LocalTime.now())) {
			this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Fecha incorrecta", "La cita fijada es anterior al momento actual");
			error++;
		}
		if (error == 0) {
			try {
				this.getRemoteManagerVisit().addVisit(this.patient.getId(), this.date, this.time, this.observations);
				this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Visita creada", "La nueva visita se ha registrado correctamente en el sistema.");
			} catch (Exception e) {
				this.manageException(e);
			}
		}

	}

	public LocalDate getDate() {
		return date;
	}

	public void setDate(LocalDate date) {
		this.date = date;
	}

	public LocalTime getTime() {
		return time;
	}

	public void setTime(LocalTime time) {
		this.time = time;
	}

	public String getObservations() {
		return observations;
	}

	public void setObservations(String observations) {
		this.observations = observations;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getPatientDisplayName() {
		return patientDisplayName;
	}

	public String getFamilyDoctorDisplayName() {
		return familyDoctorDisplayName;
	}

	public void setFamilyDoctorDisplayName(String value) {
		this.familyDoctorDisplayName = value;
	}

	public PatientTO getPatient() {
		return patient;
	}

	public void setPatient(PatientTO patient) {
		this.patient = patient;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

}
