package managedbean.systemAdmin;

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

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.inject.Named;

import TO.LoggedUserTO;
import TO.MedicalSpecialtyTO;
import common.Constants;
import managedbean.common.ManagedBeanBase;
import managedbean.common.SessionUtils;

@Named("ManageSpecialities")
@RequestScoped
public class ManageSpecialitiesMBean extends ManagedBeanBase implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private String name;
	private String description;
	private MedicalSpecialtyTO medicalSpecialty;
	private List<MedicalSpecialtyTO> medicalSpecialitiesList;

	public ManageSpecialitiesMBean() {
	}

	@PostConstruct
	public void init() {
		if (this.medicalSpecialty == null) {
			this.medicalSpecialty = new MedicalSpecialtyTO();
		}
		// Recuperamos el usuario logeado actual
		LoggedUserTO usr = null;
		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.medicalSpecialitiesList = this.getRemoteManagerCommon().listMedicalSpecialitiesPaged(0, Constants.MAX_ITEMS_AUTOCOMPLETE_SEARCH);
			}
		} catch (Exception e) {
			this.manageException(e);
		}
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	private void setSpecialtyData(MedicalSpecialtyTO ms) {
		this.id = ms.getId();
		this.name = ms.getName();
		this.description = ms.getDescription();
	}
	
	public List<MedicalSpecialtyTO> getMedicalSpecialtiesList() {
		return medicalSpecialitiesList;
	}

	public MedicalSpecialtyTO getMedicalSpecialty() {
		return medicalSpecialty;
	}

	public void setMedicalSpecialty(MedicalSpecialtyTO value) {
		this.medicalSpecialty = value;
	}
	
	public void saveData() {
		int error = 0;

		if (this.medicalSpecialty.getName() == null) {
			this.addFacesMessage(FacesMessage.SEVERITY_WARN, "Nombre no relleno", "Por favor, escriba un nombre de especialidad.");
			error++;
		}
		if (this.medicalSpecialty.getDescription() == null) {
			this.addFacesMessage(FacesMessage.SEVERITY_WARN, "Descripción no rellena", "Por favor, escriba una descripción.");
			error++;
		}

		if (error == 0) {
			try {
				MedicalSpecialtyTO ms = this.getRemoteManagerSystemAdmin().updateSpecialtyData(this.medicalSpecialty.getId(), this.medicalSpecialty.getName(), this.medicalSpecialty.getDescription());
				this.setSpecialtyData(ms);
				this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Los datos se han guardado", "Los datos de la especialidad se han guardado correctamente.");
			} catch (Exception e) {
				this.manageException(e);
			}
		}
	}
	
	public void deleteData() {
		int error = 0;

		if (this.medicalSpecialty.getName() == null) {
			this.addFacesMessage(FacesMessage.SEVERITY_WARN, "Nombre no relleno", "Por favor, escriba un nombre de especialidad.");
			error++;
		}
		if (this.medicalSpecialty.getDescription() == null) {
			this.addFacesMessage(FacesMessage.SEVERITY_WARN, "Descripción no rellena", "Por favor, escriba una descripción.");
			error++;
		}

		if (error == 0) {
			try {
				this.getRemoteManagerSystemAdmin().deleteSpecialtyData(this.medicalSpecialty.getId(), this.medicalSpecialty.getName(), this.medicalSpecialty.getDescription());
				this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Los especialidad se ha borrado", "Los datos de la especialidad se han borrado correctamente.");
			} catch (Exception e) {
				this.manageException(e);
			}
		}
	}

	public void insertData() {
		int error = 0;

		if (name == null || name.trim().length() == 0) {
			this.addFacesMessage(FacesMessage.SEVERITY_WARN, "Nombre no relleno", "Por favor, escriba un nombre de especialidad.");
			error++;
		}
		if (description == null || description.trim().length() == 0) {
			this.addFacesMessage(FacesMessage.SEVERITY_WARN, "Descripción no rellena", "Por favor, escriba una descripción.");
			error++;
		}

		if (error == 0) {
			try {
				MedicalSpecialtyTO ms = this.getRemoteManagerSystemAdmin().insertSpecialtyData(name, description);
				this.setSpecialtyData(ms);
				this.addFacesMessage(FacesMessage.SEVERITY_INFO, "Los datos se han guardado", "Los datos de la especialidad se han guardado correctamente.");
			} catch (Exception e) {
				this.manageException(e);
			}
		}
	}
}
