/*
 * 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.admin;

import com.arrebol.apc.controller.admin.CustomerController;
import com.arrebol.apc.controller.catalog.RouteController;
import com.arrebol.apc.model.catalog.People;
import com.arrebol.apc.model.catalog.RouteCtlg;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.model.enums.PeopleType;
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.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("customerManager")
@ViewScoped
public class CustomerBean extends GenericBean implements Serializable, Datatable{

    public List<People> fillDatatableCustomer() {

        return customerCtrl.fillCustomersDatatable(getLoggedUser().getOffice().getId());
    }
    
    public void changeRoute(){
        if(!routeId.isEmpty()){
            if(customerCtrl.updateRouteById(new RouteCtlg(routeId), selectedCustomer.getId(), getLoggedUser().getUser().getId()))
            {
              showMessage(FacesMessage.SEVERITY_INFO, "Ruta modificada", "Se modificó correctamente.");  
            }
            else
            {
                showMessage(FacesMessage.SEVERITY_WARN, "Ruta modificada", "Ocurrió un error durante el proceso.");  
            }
                
        }
    }
    
    @Override
    public void editRow(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @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."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void deleteRow() {
        customerCtrl.updatePeopleByStatus(ActiveStatus.DISABLED, selectedCustomer.getId(), getLoggedUser().getUser().getId());
        customer.remove(selectedCustomer);
        selectedCustomer = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Cliente Eliminado", "Se eliminó correctamente.");
    }
    
    public void updatePeopleTypeById() {
        if(selectedCustomer.getPeopleType().equals(PeopleType.BOTH))
        {
          showMessage(FacesMessage.SEVERITY_INFO, "Advertencia", "El cliente ya se encuentra como Aval.");
          return;
        }
        customerCtrl.updatePeopleTypeById(PeopleType.BOTH, selectedCustomer.getId(), getLoggedUser().getUser().getId());
        customer = null;
        selectedCustomer = null;
        customer = fillDatatableCustomer();
        showMessage(FacesMessage.SEVERITY_INFO, "Cliente modificado", "Se modificó correctamente.");
    }
    
    /**
     *
     * @param outcome
     * @return
     */
    public String detailCustomer(String outcome) {
        return outcome;
    }

    public CustomerController getCustomerCtrl() {
        return customerCtrl;
    }

    public void setCustomerCtrl(CustomerController customerCtrl) {
        this.customerCtrl = customerCtrl;
    }

    public List<People> getCustomer() {
        return customer;
    }

    public void setCustomer(List<People> customer) {
        this.customer = customer;
    }

    public People getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(People selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public String getRouteId() {
        return routeId;
    }

    public void setRouteId(String routeId) {
        this.routeId = routeId;
    }

    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;
    }
    
    private CustomerController customerCtrl;
    private RouteController routeCtrl;
    
    private List<People> customer;
    private List<RouteCtlg> route;
    
    private People selectedCustomer;
    private String routeId;
        
    @PostConstruct
    public void init() {
        customerCtrl = new CustomerController();
        routeCtrl = new RouteController();
        customer = fillDatatableCustomer();
        route = routeCtrl.fillRoutesDatatable(getLoggedUser().getOffice().getId());
    }
    
}
