/*
 * 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.StatsOpeningFeesView;
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.StatsOpeningFeesViewCfg;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

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

    public List<StatsOpeningFeesView> fillOpeningFeesDataTable(Date startDate, Date endDate, String idUser) {
        logger.debug("fillOpeningFeesDataTable");
        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<StatsOpeningFeesView> dataComplete = new ArrayList<>();
        
        if (idUser != null && !idUser.equals("")) {
            parameters.add(new ModelParameter(StatsOpeningFeesViewCfg.FIELD_USER, idUser));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsOpeningFeesView.class, StatsOpeningFeesViewCfg.QUERY_FIND_ALL_OPENING_FEES_BETWEEN_DATES_USER, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsOpeningFeesView.class, StatsOpeningFeesViewCfg.QUERY_FIND_ALL_OPENING_FEES_BETWEEN_DATES, parameters);
        }
        
        Map<String, StatsOpeningFeesView> mapDataOpeningFees = new HashMap<>();
        for (StatsOpeningFeesView openingFee : dataComplete) {
            if (mapDataOpeningFees.get(openingFee.getIdUser()) != null) {
                if(!mapDataOpeningFees.get(openingFee.getIdUser()).getRouteName().contains(openingFee.getRouteName())){
                    mapDataOpeningFees.get(openingFee.getIdUser()).setRouteName(mapDataOpeningFees.get(openingFee.getIdUser()).getRouteName() + ", " + openingFee.getRouteName());
                }
                
                mapDataOpeningFees.get(openingFee.getIdUser()).setTotaltotalOpeningFees(
                        mapDataOpeningFees.get(openingFee.getIdUser()).getTotalOpeningFees().add(openingFee.getTotalOpeningFees()));
            } else {
                mapDataOpeningFees.put(openingFee.getIdUser(), openingFee);
            }
        }
        dataComplete.clear();
        dataComplete.addAll(mapDataOpeningFees.values());
        
        dataComplete.sort(Comparator.comparing(StatsOpeningFeesView::getRouteName));
        return dataComplete;
    }

    public BigDecimal fillTotalOpeningFees(Date startDate, Date endDate) {
        logger.debug("fillTotalOpeningFees");
        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<StatsOpeningFeesView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsOpeningFeesView.class, StatsOpeningFeesViewCfg.QUERY_FIND_ALL_OPENING_FEES_BETWEEN_DATES, parameters);
        BigDecimal totalOpeningFees = new BigDecimal("0");
        for (StatsOpeningFeesView openingFee : dataComplete) {
            totalOpeningFees = totalOpeningFees.add(openingFee.getTotalOpeningFees());
        }
        return totalOpeningFees;
    }

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

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