/*
 * 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.StatsDepositsView;
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.StatsDepositsViewCfg;
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 StatsDepositsController implements Serializable {

    public List<StatsDepositsView> fillDepositsDataTable(Date startDate, Date endDate, String idUser) {
        logger.debug("fillDepositsDataTable");
        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<StatsDepositsView> dataComplete = new ArrayList<>();
        
        if (idUser != null && !idUser.equals("")) {
            parameters.add(new ModelParameter(StatsDepositsViewCfg.FIELD_USER, idUser));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsDepositsView.class, StatsDepositsViewCfg.QUERY_FIND_ALL_DEPOSITS_BETWEEN_DATES_USER, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsDepositsView.class, StatsDepositsViewCfg.QUERY_FIND_ALL_DEPOSITS_BETWEEN_DATES, parameters);
        }
        
        Map<String, StatsDepositsView> mapDataDeposits = new HashMap<>();
        /*
        for (StatsDepositsView deposits : dataComplete) {
            if (mapDataDeposits.get(deposits.getIdUser()) != null) {
                if(!mapDataDeposits.get(deposits.getIdUser()).getRouteName().contains(deposits.getRouteName())){
                    mapDataDeposits.get(deposits.getIdUser()).setRouteName(mapDataDeposits.get(deposits.getIdUser()).getRouteName() + ", " + deposits.getRouteName());
                }
                
                mapDataDeposits.get(deposits.getIdUser()).setTotalDeposits(
                        mapDataDeposits.get(deposits.getIdUser()).getTotalDeposits().add(deposits.getTotalDeposits()));
            } else {
                mapDataDeposits.put(deposits.getIdUser(), deposits);
            }
        }
        */
        dataComplete.sort(Comparator.comparing(StatsDepositsView::getRouteName));
        return dataComplete;
    }

    public BigDecimal fillTotalDeposits(Date startDate, Date endDate) {
        logger.debug("fillTotalDeposits");
        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<StatsDepositsView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsDepositsView.class, StatsDepositsViewCfg.QUERY_FIND_ALL_DEPOSITS_BETWEEN_DATES, parameters);
        BigDecimal totalDeposits = new BigDecimal("0");
        for (StatsDepositsView deposits : dataComplete) {
            totalDeposits = totalDeposits.add(deposits.getTotalDeposits());
        }
        return totalDeposits;
    }

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

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