/*
 * 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.crov.prase.controller.catalog.TipoRiesgoController;
import com.crov.prase.controller.catalog.TipoSiniestroController;
import com.crov.prase.model.catalog.TipoRiesgo;
import com.crov.prase.model.catalog.TipoSiniestro;
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("tipoSiniestroBean")
@ViewScoped
public class TipoSiniestroBean extends TaxiGenericBean implements Serializable, Datatable {

    @Override
    public void addRow() {
        TipoSiniestro tipoObj = new TipoSiniestro();
        tipoObj.setName(getName());
        tipoObj.setTipoRiesgo(getSelectedTipoRiesgo() == null || getSelectedTipoRiesgo().isEmpty() ? null : new TipoRiesgo(getSelectedTipoRiesgo()));
        controller.saveTipoSiniestro(tipoObj, getLoggedUser().getId());
        cargarListTipoSiniestro();
        FacesMessage msg = new FacesMessage("Nuevo tipo de siniestro", "Se agregó correctamente");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    @Override
    public void deleteRow() {
        logger.info("deleteRow");
        try {
            controller.deleteTipoSiniestro(selectedTipoSiniestro.getId(), getLoggedUser());
            tipoSiniestro.remove(selectedTipoSiniestro);
            selectedTipoSiniestro = null;
            cargarListTipoSiniestro();
            showMessage(FacesMessage.SEVERITY_INFO, "Tipo de siniestro deshabilitado", "Se deshabilito correctamente.");
        } catch (Exception e) {
            logger.error("deleteRow: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL, "Tipo de siniestro", "Ocurrió un error");
        }
    }

    @Override
    public void editRow(RowEditEvent event) {
        TipoSiniestro tipoSin = (TipoSiniestro) event.getObject();
        if (tipoSin != null) {
            if (getSelectedTipoRiesgo() != null && !getSelectedTipoRiesgo().isEmpty()) {
                tipoSin.setTipoRiesgo(new TipoRiesgo(getSelectedTipoRiesgo()));
            }
            controller.saveTipoSiniestro(tipoSin, getLoggedUser().getId());
            cargarListTipoSiniestro();
            showMessage(FacesMessage.SEVERITY_INFO, "Registro Editado", "Se hizo el cambio correctamente.");
        }
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Edición Cancelada", ((TipoSiniestro) 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));
    }

    private void cargarListTipoSiniestro() {
        logger.info("cargarListTipoRiesgo()");
        try {
            setTipoSiniestro(getController().findTipoSiniestro());
        } catch (Exception e) {
            logger.info("cargarListTipoRiesgo() " + e);
        }
    }

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setController(new TipoSiniestroController());
            setGenericController(new GenericValidationController());
            setTipoRiesgoController(new TipoRiesgoController());
            cargarListTipoSiniestro();
            setListTipoRiesgo(getTipoRiesgoController().fillTipoRiesgoByLocation(getLoggedUser().getLocation()));
        } catch (Exception e) {
            logger.error(e);
        }
    }

    private TipoSiniestroController controller;
    private GenericValidationController genericController;
    private TipoRiesgoController tipoRiesgoController;
    private List<TipoRiesgo> listTipoRiesgo;
    private String selectedTipoRiesgo;
    private List<TipoSiniestro> tipoSiniestro;
    private TipoSiniestro selectedTipoSiniestro;

    private String id;
    private String name;

    public GenericValidationController getGenericController() {
        return genericController;
    }

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

    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 TipoSiniestroController getController() {
        return controller;
    }

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

    public List<TipoSiniestro> getTipoSiniestro() {
        return tipoSiniestro;
    }

    public void setTipoSiniestro(List<TipoSiniestro> tipoSiniestro) {
        this.tipoSiniestro = tipoSiniestro;
    }

    public TipoSiniestro getSelectedTipoSiniestro() {
        return selectedTipoSiniestro;
    }

    public void setSelectedTipoSiniestro(TipoSiniestro selectedTipoSiniestro) {
        this.selectedTipoSiniestro = selectedTipoSiniestro;
    }

    public TipoRiesgoController getTipoRiesgoController() {
        return tipoRiesgoController;
    }

    public void setTipoRiesgoController(TipoRiesgoController tipoRiesgoController) {
        this.tipoRiesgoController = tipoRiesgoController;
    }

    public List<TipoRiesgo> getListTipoRiesgo() {
        return listTipoRiesgo;
    }

    public void setListTipoRiesgo(List<TipoRiesgo> listTipoRiesgo) {
        this.listTipoRiesgo = listTipoRiesgo;
    }

    public String getSelectedTipoRiesgo() {
        return selectedTipoRiesgo;
    }

    public void setSelectedTipoRiesgo(String selectedTipoRiesgo) {
        this.selectedTipoRiesgo = selectedTipoRiesgo;
    }

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