/*
 * 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.ColorController;
import com.arrebol.taxiservicios.model.catalog.Color;
import com.arrebol.taxiservicios.services.catalog.ColorService;
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("colorManager")
@ViewScoped
public class ColorManager extends TaxiGenericBean implements Serializable, Datatable {

    @Override
    public void editRow(RowEditEvent event) {
        Color color = (Color) event.getObject();
        if (color != null) {
            colorCtrl.saveColor(color, 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", ((Color) 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() {
        Color colorObj = new Color();
        colorObj.setName(name);

        colorCtrl.saveColor(colorObj, getLoggedUser().getId());
        color.add(colorObj);
        FacesMessage msg = new FacesMessage("Nuevo Color", "Se agregó correctamente");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    @Override
    public void deleteRow() {
        colorCtrl.deleteColor(selectedColor);
        color.remove(selectedColor);
        selectedColor = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Color Eliminado", "Se eliminó correctamente.");
    }

    public List<Color> getColor() {
        return color;
    }

    public Color getSelectedColor() {
        return selectedColor;
    }

    public void setSelectedColor(Color selectedColor) {
        this.selectedColor = selectedColor;
    }

    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() {
        colorCtrl = new ColorController();
        color = colorCtrl.fillColorDatatable();
    }

    private static final long serialVersionUID = -2400590341745672164L;

    private List<Color> color;
    private Color selectedColor;

    private String id;
    private String name;
    
    private ColorController colorCtrl;

}
