/*
 * 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.StatsAdvancesView;
import com.arrebol.apc.model.views.constance.StatsAdvancesViewCfg;
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 java.math.BigDecimal;
import java.util.Calendar;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

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

    public List<StatsAdvancesView> fillAdvancesDataTable(Date startDate, Date endDate, String idRoute) {
        logger.debug("fillAdvancesDataTable");
        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<StatsAdvancesView> dataComplete = new ArrayList<>();
        
        if (idRoute != null && !idRoute.equals("")) {
            parameters.add(new ModelParameter(StatsAdvancesViewCfg.FIELD_ROUTE, idRoute));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsAdvancesView.class, StatsAdvancesViewCfg.QUERY_FIND_ALL_ADVANCES_BETWEEN_DATES_ROUTE, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsAdvancesView.class, StatsAdvancesViewCfg.QUERY_FIND_ALL_ADVANCES_BETWEEN_DATES, parameters);
        }
        
        Map<String, StatsAdvancesView> mapDataAdvances = new HashMap<>();
        for (StatsAdvancesView advances : dataComplete) {
            if (mapDataAdvances.get(advances.getIdRoute()) != null) {
                mapDataAdvances.get(advances.getIdRoute()).setTotalAdvances(mapDataAdvances.get(advances.getIdRoute()).getTotalAdvances().add(advances.getTotalAdvances()));
            } else {
                mapDataAdvances.put(advances.getIdRoute(), advances);
            }
        }
        dataComplete.clear();
        dataComplete.addAll(mapDataAdvances.values());
        
        dataComplete.sort(Comparator.comparing(StatsAdvancesView::getRouteName));
        return dataComplete;
    }

    public BigDecimal fillTotalAdvances(Date startDate, Date endDate) {
        logger.debug("fillTotalAdvances");
        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<StatsAdvancesView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsAdvancesView.class, StatsAdvancesViewCfg.QUERY_FIND_ALL_ADVANCES_BETWEEN_DATES, parameters);
        BigDecimal totalAdvances = new BigDecimal("0");
        for (StatsAdvancesView advances : dataComplete) {
            totalAdvances = totalAdvances.add(advances.getTotalAdvances());
        }
        return totalAdvances;
    }

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

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