/*
 * 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.TalleresController;
import com.crov.prase.model.catalog.Hospitales;
import com.crov.prase.model.catalog.Talleres;
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("talleresBean")
@ViewScoped
public class TalleresBean extends TaxiGenericBean implements Serializable, Datatable {


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

        controller.saveTalleres(talleresObj, getLoggedUser().getId());
        setTalleres(controller.findTalleres());
        FacesMessage msg = new FacesMessage("Nuevo taller", "Se agregó correctamente");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    
 
    @Override
    public void deleteRow() {
        logger.info("deleteRow");
        try {

            controller.deleteTalleres(selectedTalleres.getId(), getLoggedUser());
            talleres.remove(selectedTalleres);
            selectedTalleres = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Talleres deshabilitado", "Se deshabilito correctamente.");
        } catch (Exception e) {
            logger.error("deleteRow: " + e);

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

    @Override
    public void editRow(RowEditEvent event) {
        Talleres taller = (Talleres) event.getObject();
        if (taller != null) {
            controller.saveTalleres(taller, 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", ((Talleres) 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 TalleresController());
            setLocationController(new LocationController());
            setGenericController(new GenericValidationController());
            setTalleres(
                    getController().findTalleres()
            );
            setLocation(locationController.fillLocationDatatable());

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

    private TalleresController controller;
    private GenericValidationController genericController;
    private LocationController locationController;
    private List<Talleres> talleres;
    private List<Location> location;
    private Talleres selectedTalleres;

    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 LocationController getLocationController() {
        return locationController;
    }

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

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

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

    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 GenericValidationController getGenericController() {
        return genericController;
    }

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

    public TalleresController getController() {
        return controller;
    }

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

    public List<Talleres> getTalleres() {
        return talleres;
    }

    public void setTalleres(List<Talleres> talleres) {
        this.talleres = talleres;
    }

    public Talleres getSelectedTalleres() {
        return selectedTalleres;
    }

    public void setSelectedTalleres(Talleres selectedTalleres) {
        this.selectedTalleres = selectedTalleres;
    }

   
    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 getEstado() {
        return estado;
    }

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

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