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

import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.admin.constance.GoalCfg;
import com.arrebol.apc.model.views.StatsClosingDayView;
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;
import com.arrebol.apc.model.views.constance.StatsClosingDayViewCfg;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author David Rodriguez
 */
public class StatsClosingDayController implements Serializable {

    public List<StatsClosingDayView> fillClosingDayDataTable(Date startDate, Date endDate, String idUser) {
        logger.debug("fillClosingDayDataTable");
        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<StatsClosingDayView> dataComplete = new ArrayList<>();
                
        if (idUser != null && !idUser.equals("")) {
            parameters.add(new ModelParameter(StatsClosingDayViewCfg.FIELD_USER, idUser));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsClosingDayView.class, StatsClosingDayViewCfg.QUERY_FIND_ALL_CLOSING_DAY_BETWEEN_DATES_USER, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsClosingDayView.class, StatsClosingDayViewCfg.QUERY_FIND_ALL_CLOSING_DAY_BETWEEN_DATES, parameters);
        }
        
        Map<String, StatsClosingDayView> mapDataClosingDay = new HashMap<>();
        for (StatsClosingDayView closingDay : dataComplete) {
            if (mapDataClosingDay.get(closingDay.getIdUser()) != null) {               
                mapDataClosingDay.get(closingDay.getIdUser()).setTotalClosingDay(
                        mapDataClosingDay.get(closingDay.getIdUser()).getTotalClosingDay().add(closingDay.getTotalClosingDay()));
            } else {
                mapDataClosingDay.put(closingDay.getIdUser(), closingDay);
            }
        }
        dataComplete.clear();
        dataComplete.addAll(mapDataClosingDay.values());
        
        dataComplete.sort(Comparator.comparing(StatsClosingDayView::getRouteName));
        return dataComplete;
    }

    public BigDecimal fillTotalClosingDay(Date startDate, Date endDate) {
        logger.debug("fillTotalClosingDay");
        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<StatsClosingDayView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsClosingDayView.class, StatsClosingDayViewCfg.QUERY_FIND_ALL_CLOSING_DAY_BETWEEN_DATES, parameters);
        BigDecimal totalClosingDay = new BigDecimal("0");
        for (StatsClosingDayView closingDay : dataComplete) {
            totalClosingDay = totalClosingDay.add(closingDay.getTotalClosingDay());
        }
        return totalClosingDay;
    }

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

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