package managedbean.visit;

import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Map;

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

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

@Named("VisitView")
@ViewScoped
public class VisitMBean 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 PatientTO patient;
	private FamilyDoctorTO familyDoctor;
	private String fromPage;

	public VisitMBean() {
	}

	@PostConstruct
	public void init() {
		// Inicialización de variables y propiedades van aquí.
		FacesContext context = FacesContext.getCurrentInstance();

		Map<String, String> requestParams = context.getExternalContext().getRequestParameterMap();

		VisitTO vi = null;

		try {
			this.fromPage = requestParams.get("fromPage");

			// Parámetro con el "id" de la visita.
			int parId = Integer.valueOf(requestParams.get("id"));

			vi = this.getRemoteManagerVisit().getVisit(parId);
		} catch (Exception e) {
			this.manageException(e);
		}

		if (vi != null) {
			this.id = vi.getId();
			this.date = vi.getDate();
			this.time = vi.getTime();
			this.observations = vi.getObservations();
			this.result = vi.getResult();
			this.patient = vi.getPatient();
			this.familyDoctor = vi.getFamilyDoctor();
		} else {
			this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Identificador de visita no válido",
					"No se ha podido recuperar el identificador de visita especificado. Por favor, vuelva a intentarlo seleccionando de nuevo la visita.");
		}
	}

	public void editVisit(Integer visitId) throws IOException {
		FacesContext ctx = FacesContext.getCurrentInstance();
		ctx.getExternalContext().redirect(String.format("UpdateVisit.xhtml?id=%d&fromPage=%s", visitId, this.fromPage));
	}

	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 PatientTO getPatient() {
		return patient;
	}

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

	public FamilyDoctorTO getFamilyDoctor() {
		return familyDoctor;
	}

	public void setFamilyDoctor(FamilyDoctorTO familyDoctor) {
		this.familyDoctor = familyDoctor;
	}

	public Integer getId() {
		return id;
	}

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

	public String getFromPage() {
		return fromPage;
	}

	public void setFromPage(String fromPage) {
		this.fromPage = fromPage;
	}

}
