/*
 * 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.GoalController;
import com.arrebol.apc.model.admin.Goal;
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.math.BigDecimal;
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("goalManager")
@ViewScoped
public class GoalBean 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 {
                setGoal(getGoalCtrl().fillGoalDatatable(getLoggedUser().getOffice().getId(), getStarDate(), getEndDate()));
            }
        } catch (Exception e) {
        }
    }

    public List<Goal> fillDatatableGoal() {

        return getGoalCtrl().fillGoalDatatable(getLoggedUser().getOffice().getId(), getStarDate(), getEndDate());
    }

    @Override
    public void editRow(RowEditEvent event) {
        Goal goal = (Goal) event.getObject();
        if (goal != null) {
            goalCtrl.updateByGoalId(goal);
            showMessage(FacesMessage.SEVERITY_INFO, "Registro Editado", "Se hizo el cambio correctamente.");
        }
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Edición Cancelada", ((Goal) event.getObject()).getAmount().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() {
        goalSave.setActiveStatus(ActiveStatus.ENEBLED);
        goalSave.setAmount(new BigDecimal(amount));
        goalSave.setOffice(new Office(getLoggedUser().getOffice().getId()));
        goalSave.setCreatedOn(new Date());
        goalSave.setCreatedBy(getLoggedUser().getId());
        goalCtrl.saveGoal(goalSave);
        goalSave = new Goal();
        goal.clear();
        goal = fillDatatableGoal();
        amount = "";
        FacesMessage msg = new FacesMessage("Nueva meta", "Se agregó correctamente");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    @Override
    public void deleteRow() {
        goalCtrl.updateGoalByStatus(ActiveStatus.DISABLED, selectedGoal.getId(), getLoggedUser().getUser().getId());
        goal.remove(selectedGoal);
        selectedGoal = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Meta Eliminada", "Se eliminó correctamente.");
    }

    public GoalController getGoalCtrl() {
        return goalCtrl;
    }

    public void setGoalCtrl(GoalController goalCtrl) {
        this.goalCtrl = goalCtrl;
    }

    public List<Goal> getGoal() {
        return goal;
    }

    public void setGoal(List<Goal> goal) {
        this.goal = goal;
    }

    public Goal getSelectedGoal() {
        return selectedGoal;
    }

    public void setSelectedGoal(Goal selectedGoal) {
        this.selectedGoal = selectedGoal;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public Goal getGoalSave() {
        return goalSave;
    }

    public void setGoalSave(Goal goalSave) {
        this.goalSave = goalSave;
    }

    private GoalController goalCtrl;

    private List<Goal> goal;

    private Goal selectedGoal;
    private String amount;

    private Goal goalSave;

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

        goalCtrl = new GoalController();
        goal = fillDatatableGoal();
        goalSave = new Goal();
    }
}
