/*
 * 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.controller.admin;

import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.admin.Bonus;
import com.arrebol.apc.model.admin.constance.BonusCfg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.repository.GenericEntityRepository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
public class BonusController implements Serializable{

    private static final long serialVersionUID = 4204730064288408417L;
    
       /**
     *
     * Searching all advances by office.
     *
     * @param officeId
     * @param startDate
     * @param endDate
     * @return
     */
    public List<Bonus> fillBonusDatatable(String officeId, Date startDate, Date endDate) {
        logger.debug("fillBonusDatatable");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(BonusCfg.FIELD_OFFICE, new Office(officeId)));
        parameters.add(new ModelParameter(BonusCfg.FIELD_ACTIVE_STATUS, ActiveStatus.ENEBLED));
        parameters.add(new ModelParameter(BonusCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(BonusCfg.PARAM_END_DATE, endDate));

        return genericEntityRepository.xmlQueryAPCEntities(Bonus.class, BonusCfg.QUERY_FIND_ALL_BONUS_BETWEEN_DATES, parameters);
    }
    
    /**
     *
     * @param bonus
     * @return boolean
     */
    public boolean updateByBonusId(Bonus bonus) {
        logger.debug("updateByBonusId");

        return genericEntityRepository.updateAPCEntity(bonus);
    }
    
    /**
     *
     * @param bonus
     * @return boolean
     */
    public boolean saveBonus(Bonus bonus) {
        logger.debug("saveBonus");
        boolean success = genericEntityRepository.insertAPCEntity(bonus);

        return success;
    } 
    
       /**
     *
     * @param status
     * @param bonusIdToUpdate
     * @param lastUpdatedBy
     * @return
     */
    public boolean updateBonusByStatus(ActiveStatus status, String bonusIdToUpdate, String lastUpdatedBy) {
        logger.debug("updateBonusByStatus");

        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(BonusCfg.FIELD_ACTIVE_STATUS, status));
        parameters.add(new ModelParameter(BonusCfg.FIELD_LAST_UPDATED_BY, lastUpdatedBy));
        parameters.add(new ModelParameter(BonusCfg.FIELD_LAST_UPDATED_ON, new Date()));
        parameters.add(new ModelParameter(BonusCfg.FIELD_ID, bonusIdToUpdate));

        return genericEntityRepository.xmlUpdateOrDeleteAPCEntity(BonusCfg.QUERY_UPDATE_BONUS_BY_STATUS, parameters);
    }
    
    final Logger logger = LogManager.getLogger(BonusController.class);
    private final GenericEntityRepository genericEntityRepository;

    public BonusController() {
        this.genericEntityRepository = new GenericEntityRepository();
    }
    
}
