/*
 * 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.model.enums.PolizaType;
import com.crov.prase.controller.catalog.TipoPolizaController;
import com.crov.prase.model.catalog.Hospitales;
import com.crov.prase.model.catalog.TipoPoliza;
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("tipoPolizaBean")
@ViewScoped
public class TipoPolizaBean extends TaxiGenericBean implements Serializable, Datatable {

    @Override
    public void addRow() {
        TipoPoliza tipoObj = new TipoPoliza();
        tipoObj.setName(name);
        if (getTipo().equals("CHOFER")) {
            tipoObj.setTipo(PolizaType.CHOFER);
        } else if (getTipo().equals("PARTICULAR")) {
            tipoObj.setTipo(PolizaType.PARTICULAR);
        } else if (getTipo().equals("CONDUCTOR_APP")) {
            tipoObj.setTipo(PolizaType.CONDUCTOR_APP);
        }

        controller.saveTipoPoliza(tipoObj, getLoggedUser().getId());
        tipoPoliza.add(tipoObj);
        FacesMessage msg = new FacesMessage("Nuevo tipo de póliza", "Se agregó correctamente");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    @Override
    public void deleteRow() {
        logger.info("deleteRow");
        try {

            controller.deleteTipoPoliza(selectedTipoPoliza.getId(), getLoggedUser());
            tipoPoliza.remove(selectedTipoPoliza);
            selectedTipoPoliza = null;
            showMessage(FacesMessage.SEVERITY_INFO, "Tipo de póliza deshabilitado", "Se deshabilito correctamente.");
        } catch (Exception e) {
            logger.error("deleteRow: " + e);

            showMessage(FacesMessage.SEVERITY_FATAL,
                    "Tipo de póliza",
                    "Ocurrió un error");
        }
    }

    @Override
    public void editRow(RowEditEvent event) {
        TipoPoliza tipoPoliza = (TipoPoliza) event.getObject();
        if (tipoPoliza != null) {
            controller.saveTipoPoliza(tipoPoliza, 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 TipoPolizaController());
            setGenericController(new GenericValidationController());
            setTipoPoliza(
                    getController().findTipoPoliza()
            );

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

    private TipoPolizaController controller;
    private GenericValidationController genericController;
    private List<TipoPoliza> tipoPoliza;
    private TipoPoliza selectedTipoPoliza;

    private String id;
    private String name;
    private double costo;
    private double costoSemestral;
    private double costoTrimestral;
    private double costoMensual;
    private String tipo;

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

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

    public List<TipoPoliza> getTipoPoliza() {
        return tipoPoliza;
    }

    public void setTipoPoliza(List<TipoPoliza> tipoPoliza) {
        this.tipoPoliza = tipoPoliza;
    }

    public TipoPoliza getSelectedTipoPoliza() {
        return selectedTipoPoliza;
    }

    public void setSelectedTipoPoliza(TipoPoliza selectedTipoPoliza) {
        this.selectedTipoPoliza = selectedTipoPoliza;
    }

    public double getCosto() {
        return costo;
    }

    public void setCosto(double costo) {
        this.costo = costo;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    public double getCostoSemestral() {
        return costoSemestral;
    }

    public void setCostoSemestral(double costoSemestral) {
        this.costoSemestral = costoSemestral;
    }

    public double getCostoTrimestral() {
        return costoTrimestral;
    }

    public void setCostoTrimestral(double costoTrimestral) {
        this.costoTrimestral = costoTrimestral;
    }

    public double getCostoMensual() {
        return costoMensual;
    }

    public void setCostoMensual(double costoMensual) {
        this.costoMensual = costoMensual;
    }

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