/*
 * 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.ProductBrandController;
import com.arrebol.taxiservicios.model.catalog.Brand;
import com.arrebol.taxiservicios.services.catalog.ProductBrandService;
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.inject.Named;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author oscarvargas
 */
@Named("productBrandManager")
@javax.faces.view.ViewScoped
public class ProductBrandManager extends TaxiGenericBean implements Serializable, Datatable {

    @Override
    public void editRow(RowEditEvent event) {
        Brand brand = (Brand) event.getObject();
        if (brand != null) {
            productBrandCtrl.saveProductBrand(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);

        if (productBrandCtrl.saveProductBrand(brandObj, getLoggedUser().getId())) {
            brand.add(brandObj);
            FacesMessage msg = new FacesMessage("Nueva Marca de Producto", "Se agregó correctamente");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }else{
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR ,"Nueva Marca de Producto", "No se pudo agregar el producto");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    @Override
    public void deleteRow() {
        productBrandCtrl.deleteProductBrand(selectedBrand);
        brand.remove(selectedBrand);
        selectedBrand = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Marca de Producto 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() {
        productBrandCtrl = new ProductBrandController();
        brand = productBrandCtrl.fillProductBrandDatatable();
    }

    private static final long serialVersionUID = -2705968408514323965L;

    private List<Brand> brand;
    private Brand selectedBrand;

    private String id;
    private String name;
    
    private ProductBrandController productBrandCtrl;

}
