/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.crov.prase.catalog;

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.GenericValidationController;
import com.arrebol.taxiservicios.controller.catalog.LocationController;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.crov.prase.controller.catalog.HospitalesController;
import com.crov.prase.model.catalog.Hospitales;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Oscar Vargas
 */
@Named("hospitalesBean")
@ViewScoped
public class HospitalesBean extends TaxiGenericBean implements Serializable, Datatable {


    @Override
    public void addRow() {
        Hospitales hospitalObj = new Hospitales();
        hospitalObj.setName(name);
        hospitalObj.setDireccion(direccion);
        hospitalObj.setTelefono(telefono);
        hospitalObj.setContacto(contacto);
        hospitalObj.setComentarios(comentarios);
        hospitalObj.setEstado(estado);
        hospitalObj.setLocation(getLocationController().getLocationById(idLocation));

        controller.saveHospitales(hospitalObj, getLoggedUser().getId());
        setHospitales(controller.findHospitales());
        FacesMessage msg = new FacesMessage("Nuevo hospital", "Se agregó correctamente");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    
 
    @Override
    public void deleteRow() {
        logger.info("deleteRow");
        try {

            controller.deleteHospitales(selectedHospital.getId(), getLoggedUser());
            hospitales.remove(selectedHospital);
            selectedHospital = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Hospital deshabilitado", "Se deshabilito correctamente.");
        } catch (Exception e) {
            logger.error("deleteRow: " + e);

            showMessage(FacesMessage.SEVERITY_FATAL,
                     "Hospital",
                    "Ocurrió un error");
        }
    }

    @Override
    public void editRow(RowEditEvent event) {
        Hospitales hospital = (Hospitales) event.getObject();
        if (hospital != null) {
            controller.saveHospitales(hospital, getLoggedUser().getId());
            showMessage(FacesMessage.SEVERITY_INFO, "Registro Editado", "Se hizo el cambio correctamente.");
        }
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Edición Cancelada", ((Hospitales) event.getObject()).getName());
    }

    @Override
    public void onRowReorder(ReorderEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Registro Movido", "De columna: " + (event.getFromIndex() + 1) + " a columna: " + (event.getToIndex() + 1));
    }


    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();

            setController(new HospitalesController());
            setLocationController(new LocationController());
            setGenericController(new GenericValidationController());
            setHospitales(
                    getController().findHospitales()
            );
            setLocation(locationController.fillLocationDatatable());

        } catch (Exception e) {
            logger.error(e);
        }
    }

    private HospitalesController controller;
    private LocationController locationController;
    private GenericValidationController genericController;
    private List<Hospitales> hospitales;
    private List<Location> location;
    private Hospitales selectedHospital;

    private String id;
    private String name;
    private String telefono;
    private String direccion;
    private String contacto;
    private String comentarios;
    private String idLocation;
    private String estado;
    

    public GenericValidationController getGenericController() {
        return genericController;
    }

    public List<Location> getLocation() {
        return location;
    }

    public void setLocation(List<Location> location) {
        this.location = location;
    }

    public void setGenericController(GenericValidationController genericController) {
        this.genericController = genericController;
    }

    public HospitalesController getController() {
        return controller;
    }

    public void setController(HospitalesController controller) {
        this.controller = controller;
    }

    public List<Hospitales> getHospitales() {
        return hospitales;
    }

    public void setHospitales(List<Hospitales> hospitales) {
        this.hospitales = hospitales;
    }

    public Hospitales getSelectedHospital() {
        return selectedHospital;
    }

    public void setSelectedHospital(Hospitales selectedHospital) {
        this.selectedHospital = selectedHospital;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getTelefono() {
        return telefono;
    }

    public void setTelefono(String telefono) {
        this.telefono = telefono;
    }

    public String getDireccion() {
        return direccion;
    }

    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }

    public String getContacto() {
        return contacto;
    }

    public void setContacto(String contacto) {
        this.contacto = contacto;
    }

    public String getComentarios() {
        return comentarios;
    }

    public void setComentarios(String comentarios) {
        this.comentarios = comentarios;
    }

    public String getIdLocation() {
        return idLocation;
    }

    public void setIdLocation(String idLocation) {
        this.idLocation = idLocation;
    }

    public LocationController getLocationController() {
        return locationController;
    }

    public void setLocationController(LocationController locationController) {
        this.locationController = locationController;
    }

    public String getEstado() {
        return estado;
    }

    public void setEstado(String estados) {
        this.estado = estados;
    }

    
    private static final long serialVersionUID = -136031528440947006L;
    final Logger logger = LogManager.getLogger(HospitalesBean.class);
}
