package managedbean.profile;

import java.io.Serializable;
import java.util.List;

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

import TO.FamilyDoctorTO;
import TO.LoggedUserTO;
import TO.PatientTO;
import common.Constants;
import common.UserType;
import managedbean.common.ManagedBeanBase;
import managedbean.common.SessionUtils;

/***
 * 
 * @author Marcos García Núñez (mgarcianun@uoc.edu)
 *
 */
@Named("ChangeFD")
@ViewScoped
public class ChangeFamilyDoctorMBean extends ManagedBeanBase implements Serializable {
	private static final long serialVersionUID = 1L;

	private int id;
	private FamilyDoctorTO currentFamilyDoctor;
	private FamilyDoctorTO newFamilyDoctor;
	private List<FamilyDoctorTO> familyDoctorList;
	private String lastUIQuery;

	public ChangeFamilyDoctorMBean() {

	}

	@PostConstruct
	public void init() {
		// Recuperamos el usuario logeado actual
		LoggedUserTO usr = null;
		this.lastUIQuery = "";
		try {
			usr = SessionUtils.getloggedOnUser();

			if (usr == null)
				this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Sesión no válida",
						"Su sesión actual no es válida, por favor cierre su sesión y vuelva a logearse en el sistema.");
			else {
				this.id = Integer.valueOf(usr.getId());

				if (usr.getUserType() == UserType.PATIENT) {
					this.familyDoctorList = this.getRemoteManagerCommon().listFamilyDoctorsPaged(0, Constants.MAX_ITEMS_AUTOCOMPLETE_SEARCH);

					PatientTO pat = this.getRemoteManagerCommon().findPatientById(this.id);
					this.setCurrentFamilyDoctor(pat.getFamilyDoctor());
				}
			}
		} catch (Exception e) {
			this.manageException(e);
		}

	}

	public List<FamilyDoctorTO> getFamilyDoctorList() {
		return familyDoctorList;
	}

	public List<FamilyDoctorTO> completeFamilyDoctor(String query) {
		if (query != null && query.equals(this.lastUIQuery) == false) {
			this.lastUIQuery = query;
			// Recuperamos las 200 primeras coincidencias
			this.familyDoctorList = this.getRemoteManagerCommon().listFamilyDoctorsFiltered(query, 0, Constants.MAX_ITEMS_AUTOCOMPLETE_SEARCH);
		}
		return this.familyDoctorList;
	}

	public int getId() {
		return id;
	}

	public void saveData() {
		int error = 0;

		if (this.getNewFamilyDoctor() == null) {
			this.addFacesMessage("frmChangeFD:newFamilyDocAC", FacesMessage.SEVERITY_WARN, "Nuevo médico de familia no seleccionado",
					"Por favor, especifique un nuevvo médico de familia.");
			error++;
		}

		if (this.getCurrentFamilyDoctor() != null && this.getNewFamilyDoctor().getId() == this.getCurrentFamilyDoctor().getId()) {
			this.addFacesMessage("frmChangeFD:newFamilyDocAC", FacesMessage.SEVERITY_WARN, "El médico de familia debe ser diferente",
					"Por favor, seleccione un médico de familia diferente al que tiene actualmente asignado.");
			error++;
		}

		if (error == 0) {
			try {
				PatientTO paTO = this.getRemoteManagerProfile().changeFamilyDoctor(this.getId(), this.newFamilyDoctor.getId());
				this.currentFamilyDoctor = paTO.getFamilyDoctor();
				this.newFamilyDoctor = null;

				this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Los datos se han guardado", "Su médico de familia asignado se ha cambiado correctamente.");
			} catch (Exception e) {
				this.manageException(e);
			}
		}
	}

	public FamilyDoctorTO getCurrentFamilyDoctor() {
		return currentFamilyDoctor;
	}

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

	public FamilyDoctorTO getNewFamilyDoctor() {
		return newFamilyDoctor;
	}

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

}
