/*
 * 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.constance.GoalCfg;
import com.arrebol.apc.model.views.StatsGasolineView;
import com.arrebol.apc.model.views.constance.StatsGasolineViewCfg;
import com.arrebol.apc.repository.GenericEntityRepository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
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 StatsGasolineController implements Serializable {

    public List<StatsGasolineView> fillGasolineDataTable(Date startDate, Date endDate) {
        logger.debug("fillGasolineDataTable");
        List<ModelParameter> parameters = new ArrayList<>();
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        startDate = calendar.getTime();

        Calendar calendarF = Calendar.getInstance();
        calendarF.setTime(endDate);
        calendarF.add(Calendar.DAY_OF_YEAR, 1);
        endDate = calendarF.getTime();
        
        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        List<StatsGasolineView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsGasolineView.class, StatsGasolineViewCfg.QUERY_FIND_ALL_GASOLINE_BETWEEN_DATES, parameters);

        return dataComplete;
    }

    public Double fillTotalGasoline(Date startDate, Date endDate) {
        logger.debug("fillTotalGasoline");
        List<ModelParameter> parameters = new ArrayList<>();
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        startDate = calendar.getTime();

        Calendar calendarF = Calendar.getInstance();
        calendarF.setTime(endDate);
        calendarF.add(Calendar.DAY_OF_YEAR, 1);
        endDate = calendarF.getTime();
        
        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        List<StatsGasolineView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsGasolineView.class, StatsGasolineViewCfg.QUERY_FIND_ALL_GASOLINE_BETWEEN_DATES, parameters);
        Double totalGasoline = 0.0;
        for (StatsGasolineView gasoline : dataComplete) {
            totalGasoline = totalGasoline + gasoline.getTotalGasoline();
        }
        return totalGasoline;
    }

    final Logger logger = LogManager.getLogger(StatsAdvancesController.class);
    private final GenericEntityRepository genericEntityRepository;

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

}
