package com.arrebol.taxiservicios.beans.catalog;

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.catalog.EtapasJuridicoController;
import com.arrebol.taxiservicios.model.catalog.EtapasJuridico;
import com.arrebol.taxiservicios.model.enums.GenericEnumType;
import java.io.Serializable;
import java.util.List;
import java.util.UUID;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
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 Crov
 */
@Named("etapasJuridicoBean")
@ViewScoped
public class EtapasJuridicoBean extends TaxiGenericBean implements Serializable, Datatable {

    private static final long serialVersionUID = -2400590341745672164L;
    final Logger logger = LogManager.getLogger(getClass());

    private EtapasJuridicoController etapasJuridicasController;
    private List<EtapasJuridico> listaEtapas;
    private EtapasJuridico selectedEtapaJuridica;
    private String nombre;

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            setEtapasJuridicasController(new EtapasJuridicoController());
            setListaEtapas(getEtapasJuridicasController().fillAllEtapasJuridicas(getLoggedUser().getLocation()));
            getListaEtapas();
        } catch (Exception e) {
            logger.error("EtapasJuridicasBean init", e);
        }
    }

    public void addEtapaJuridica() {
        logger.info("addEtapaJuridica");
        try {
            setEtapasJuridicasController(new EtapasJuridicoController());
            EtapasJuridico etapa = new EtapasJuridico();
            etapa.setId(UUID.randomUUID().toString());
            etapa.setNombre(getNombre());
            etapa.setLocation(getLoggedUser().getLocation());
            etapa.setEstatusActivo(GenericEnumType.ENABLED);
            getEtapasJuridicasController().addEtapaJuridica(etapa, getLoggedUser(), true);
            logger.info("Add etapa juridica");
            showMessage(FacesMessage.SEVERITY_INFO, "Se agregó correctamente ", "Etapa juridica agregada correctamente.");
            getListaEtapas().add(etapa);
            clearselected();
        } catch (Exception e) {
            logger.error("addEtapaJuridica: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL, "Error", "Ocurrió un error");
        }
    }

    public void updatedForm() {
        setNombre(getSelectedEtapaJuridica().getNombre());
    }

    public void updatedEtapaJuridica() {
        logger.info("updatedEtapaJuridica");
        try {
            getSelectedEtapaJuridica().setNombre(getNombre());
            getEtapasJuridicasController().addEtapaJuridica(getSelectedEtapaJuridica(), getLoggedUser(), false);

            logger.info("Updated etapa juridica");
            showMessage(FacesMessage.SEVERITY_INFO, "Se actualizó correctamente ", "Etapa jurídica se actualizó correctamente.");
            clearselected();
        } catch (Exception e) {
            logger.error("updatedEtapaJuridica: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL, "Error", "Ocurrió un error");
        }
    }

    public void deleteEtapaJuridica() {
        logger.info("deleteEtapaJuridica");
        try {
            getSelectedEtapaJuridica().setEstatusActivo(GenericEnumType.DISABLED);
            getEtapasJuridicasController().addEtapaJuridica(getSelectedEtapaJuridica(), getLoggedUser(), false);
            logger.info("Delete etapa jurídica");
            showMessage(FacesMessage.SEVERITY_INFO, "Se elimino correctamente ", "Etapa jurídica se elimino correctamente.");
            getListaEtapas().remove(getSelectedEtapaJuridica());
        } catch (Exception e) {
            logger.error("deleteEtapaJuridica: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL, "Error", "Ocurrió un error");
        }
    }

    public void clearselected() {
        setSelectedEtapaJuridica(new EtapasJuridico());
        setNombre("");
    }

    @Override
    public void editRow(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

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

    @Override
    public void addRow() {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    @Override
    public void deleteRow() {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    public List<EtapasJuridico> getListaEtapas() {
        return listaEtapas;
    }

    public void setListaEtapas(List<EtapasJuridico> listaEtapas) {
        this.listaEtapas = listaEtapas;
    }

    public EtapasJuridico getSelectedEtapaJuridica() {
        return selectedEtapaJuridica;
    }

    public void setSelectedEtapaJuridica(EtapasJuridico selectedEtapaJuridica) {
        this.selectedEtapaJuridica = selectedEtapaJuridica;
    }

    public EtapasJuridicoController getEtapasJuridicasController() {
        return etapasJuridicasController;
    }

    public void setEtapasJuridicasController(EtapasJuridicoController etapasJuridicasController) {
        this.etapasJuridicasController = etapasJuridicasController;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

}
