/*
 * 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.model.catalog.Brand;
import com.arrebol.taxiservicios.services.catalog.CarBrandService;
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("carBrandManager")
@ViewScoped
public class CarBrandManager extends TaxiGenericBean implements Serializable, Datatable {


    @Override
    public void editRow(RowEditEvent event) {
        Brand brand = (Brand) event.getObject();
        if (brand != null) {
            carBrandCtrl.saveCarBrand(brand, 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", ((Brand) 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() {
        Brand brandObj = new Brand();
        brandObj.setName(name);

        carBrandCtrl.saveCarBrand(brandObj, getLoggedUser().getId());
        brand.add(brandObj);
        FacesMessage msg = new FacesMessage("Nueva Marca de Vehículo", "Se agregó correctamente");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    @Override
    public void deleteRow() {
        carBrandCtrl.deleteCarBrand(selectedBrand);
        brand.remove(selectedBrand);
        selectedBrand = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Marca de Vehículo Eliminado", "Se eliminó correctamente.");
    }

    public List<Brand> getBrand() {
        return brand;
    }

    public Brand getSelectedBrand() {
        return selectedBrand;
    }

    public void setSelectedBrand(Brand selectedBrand) {
        this.selectedBrand = selectedBrand;
    }

    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;
    }

    @PostConstruct
    public void init() {
        carBrandCtrl = new CarBrandController();
        brand = carBrandCtrl.fillCarBrandDatatable();
    }

    private static final long serialVersionUID = -5450130814186193205L;

    private List<Brand> brand;
    private Brand selectedBrand;

    private String id;
    private String name;

    private CarBrandController carBrandCtrl;
}
