/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.taxiservicios.beans.catalog;

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.catalog.CarBrandController;
import com.arrebol.taxiservicios.controller.catalog.ModelVehicleController;
import com.arrebol.taxiservicios.model.catalog.Brand;
import com.arrebol.taxiservicios.model.catalog.Vehicle;
import com.arrebol.taxiservicios.services.catalog.ModelVehicleService;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
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 oscarvargas
 */
@Named("modelVehicleManager")
@ViewScoped
public class ModelVehicleManager extends TaxiGenericBean implements Serializable, Datatable {

    @Override
    public void editRow(RowEditEvent event) {
        Vehicle vehicle = (Vehicle) event.getObject();
        if (vehicle != null) {
            vehicleCtrl.saveModelVehicle(vehicle, getLoggedUser().getId(), "");
            this.vehicle.clear();
            this.vehicle = vehicleCtrl.fillModelVehicleDatatable();
            showMessage(FacesMessage.SEVERITY_INFO, "Registro Editado", "Se hizo el cambio correctamente.");
        }
    }

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

    @Override
    public void addRow() {
        Vehicle vehicleObj = new Vehicle();
        vehicleObj.setName(name);
        System.out.print(brandId);
        vehicleCtrl.saveModelVehicle(vehicleObj, getLoggedUser().getId(), brandId);
        vehicle.clear();
        vehicle = vehicleCtrl.fillModelVehicleDatatable();
        FacesMessage msg = new FacesMessage("Nuevo Modelo de Vehículo", "Se agregó correctamente");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    @Override
    public void deleteRow() {
        vehicleCtrl.deleteModelVehicle(selectedVehicle);
        vehicle.remove(selectedVehicle);
        selectedVehicle = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Modelo de Vehículo Eliminado", "Se eliminó correctamente.");
    }

    public List<Vehicle> getVehicle() {
        return vehicle;
    }

    public Vehicle getSelectedVehicle() {
        return selectedVehicle;
    }

    public void setSelectedVehicle(Vehicle selectedVehicle) {
        this.selectedVehicle = selectedVehicle;
    }

    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 String getBrandId() {
        return brandId;
    }

    public void setBrandId(String brandId) {
        this.brandId = brandId;
    }

    public List<Brand> getBrandes() {
        return brandes;
    }

    public void setBrandes(List<Brand> brandes) {
        this.brandes = brandes;
    }

    public ModelVehicleController getVehicleCtrl() {
        return vehicleCtrl;
    }

    public void setVehicleCtrl(ModelVehicleController vehicleCtrl) {
        this.vehicleCtrl = vehicleCtrl;
    }

    public CarBrandController getBrandCtrl() {
        return brandCtrl;
    }

    public void setBrandCtrl(CarBrandController brandCtrl) {
        this.brandCtrl = brandCtrl;
    }

    @PostConstruct
    public void init() {
        vehicleCtrl = new ModelVehicleController();
        brandCtrl = new CarBrandController();
        vehicle = vehicleCtrl.fillModelVehicleDatatable();
        brandes = brandCtrl.fillCarBrandComboBox();
    }

    private static final long serialVersionUID = -4304755904973288208L;

    private List<Vehicle> vehicle;
    private List<Brand> brandes;
    private Vehicle selectedVehicle;

    private String id;
    private String name;
    private String brandId;
    
    private ModelVehicleController vehicleCtrl;
    private CarBrandController brandCtrl;

}
