/*
 * 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.BonusController;
import com.arrebol.apc.model.admin.Bonus;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.web.beans.Datatable;
import com.arrebol.apc.web.beans.GenericBean;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
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 Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */

@Named("bonusManager")
@ViewScoped
public class BonusBean extends GenericBean implements Serializable, Datatable {

    public void searchHistoricalAction() {
        try {
            if (getStarDate().after(getEndDate())) {
                showMessage(FacesMessage.SEVERITY_ERROR, getBundlePropertyFile().getString("generic.start.date"), getBundlePropertyFile().getString("generic.end.date.error"));
            } else {
                setBonus(getBonusCtrl().fillBonusDatatable(getLoggedUser().getOffice().getId(), getStarDate(), getEndDate()));
            }
        } catch (Exception e) {
        }
    }

    public List<Bonus> fillDatatableBonus() {

        return getBonusCtrl().fillBonusDatatable(getLoggedUser().getOffice().getId(), getStarDate(), getEndDate());
    }

    @Override
    public void editRow(RowEditEvent event) {
        Bonus bonus = (Bonus) event.getObject();
        if (bonus != null) {
            bonusCtrl.updateByBonusId(bonus);
            showMessage(FacesMessage.SEVERITY_INFO, "Registro Editado", "Se hizo el cambio correctamente.");
        }
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Edición Cancelada", ((Bonus) event.getObject()).getName().toString());
    }

    @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() {
        if (administrative) {
            saveBonus.setAdministrative(ActiveStatus.ENEBLED);
        } else {
            saveBonus.setAdministrative(ActiveStatus.DISABLED);
        }
        saveBonus.setActiveStatus(ActiveStatus.ENEBLED);
        saveBonus.setCreatedBy(getLoggedUser().getId());
        saveBonus.setOffice(new Office(getLoggedUser().getOffice().getId()));
        saveBonus.setCreatedOn(new Date());
        if (bonusCtrl.saveBonus(saveBonus)) {
            saveBonus = new Bonus();
            administrative = false;
            bonus.clear();
            bonus = fillDatatableBonus();
            FacesMessage msg = new FacesMessage("Nuevo bono", "Se agregó correctamente");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    @Override
    public void deleteRow() {
        bonusCtrl.updateBonusByStatus(ActiveStatus.DISABLED, selectedBonus.getId(), getLoggedUser().getUser().getId());
        bonus.remove(selectedBonus);
        selectedBonus = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Bono Eliminado", "Se eliminó correctamente.");
    }

    public BonusController getBonusCtrl() {
        return bonusCtrl;
    }

    public void setBonusCtrl(BonusController bonusCtrl) {
        this.bonusCtrl = bonusCtrl;
    }

    public List<Bonus> getBonus() {
        return bonus;
    }

    public void setBonus(List<Bonus> bonus) {
        this.bonus = bonus;
    }

    public Bonus getSelectedBonus() {
        return selectedBonus;
    }

    public void setSelectedBonus(Bonus selectedBonus) {
        this.selectedBonus = selectedBonus;
    }

    public Bonus getSaveBonus() {
        return saveBonus;
    }

    public void setSaveBonus(Bonus saveBonus) {
        this.saveBonus = saveBonus;
    }

    public boolean isAdministrative() {
        return administrative;
    }

    public void setAdministrative(boolean administrative) {
        this.administrative = administrative;
    }

    private BonusController bonusCtrl;
    private List<Bonus> bonus;
    private Bonus selectedBonus;
    private Bonus saveBonus;
    private boolean administrative;

    @PostConstruct
    public void init() {
        loadBundlePropertyFile();
        initOneWeekBeforeToCurrdate();

        bonusCtrl = new BonusController();
        bonus = fillDatatableBonus();
        saveBonus = new Bonus();
        administrative = false;
    }

}
