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

import com.arrebol.apc.controller.catalog.RouteController;
import com.arrebol.apc.model.catalog.RouteCtlg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.web.beans.Datatable;
import com.arrebol.apc.web.beans.GenericBean;
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.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
@Named("routeManager")
@ViewScoped
public class RouteBean extends GenericBean implements Serializable, Datatable{

    public List<RouteCtlg> fillDatatableRoute() {

        return routeCtrl.fillRoutesDatatable(getLoggedUser().getOffice().getId());
    }
    
    @Override
    public void editRow(RowEditEvent event) {
        RouteCtlg route = (RouteCtlg) event.getObject();
        if (route != null) {
            routeCtrl.updateByRouteId(route);
            showMessage(FacesMessage.SEVERITY_INFO, "Registro Editado", "Se hizo el cambio correctamente.");
        }
    }

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

    @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() {
        RouteCtlg routeObj = new RouteCtlg();
        routeObj.setRoute(name);
        routeObj.setOffice(new Office(getLoggedUser().getOffice().getId()));
        routeObj.setActiveStatus(ActiveStatus.ENEBLED);
        routeObj.setCreatedBy(getLoggedUser().getUser().getId());

        routeCtrl.saveRoute(routeObj);
        route.add(routeObj);
        FacesMessage msg = new FacesMessage("Nueva ruta", "Se agregó correctamente");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    @Override
    public void deleteRow() {
        routeCtrl.updateRouteByStatus(ActiveStatus.DISABLED, selectedRoute.getId(), getLoggedUser().getUser().getId());
        route.remove(selectedRoute);
        selectedRoute = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Ruta Eliminada", "Se eliminó correctamente.");
    }

    public RouteController getRouteCtrl() {
        return routeCtrl;
    }

    public void setRouteCtrl(RouteController routeCtrl) {
        this.routeCtrl = routeCtrl;
    }

    public List<RouteCtlg> getRoute() {
        return route;
    }

    public void setRoute(List<RouteCtlg> route) {
        this.route = route;
    }

    public RouteCtlg getSelectedRoute() {
        return selectedRoute;
    }

    public void setSelectedRoute(RouteCtlg selectedRoute) {
        this.selectedRoute = selectedRoute;
    }

    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;
    }
    
    private RouteController routeCtrl;
    
    private List<RouteCtlg> route;
    
    private RouteCtlg selectedRoute;

    private String id;
    private String name;
    
    @PostConstruct
    public void init() {
        routeCtrl = new RouteController();
        route = fillDatatableRoute();
    }
    
}
