/*
 * 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.StatsZeroPaymentsView;
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.StatsZeroPaymentsViewCfg;
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 StatsZeroPaymentsController implements Serializable {

    public List<StatsZeroPaymentsView> fillZeroPaymentsDataTable(Date startDate, Date endDate, String idUser) {
        logger.debug("fillZeroPaymentsDataTable");
        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<StatsZeroPaymentsView> dataComplete = new ArrayList<>();
        
        if (idUser != null && !idUser.equals("")) {
            parameters.add(new ModelParameter(StatsZeroPaymentsViewCfg.FIELD_USER, idUser));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsZeroPaymentsView.class, StatsZeroPaymentsViewCfg.QUERY_FIND_ALL_ZERO_PAYMENTS_BETWEEN_DATES_USER, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsZeroPaymentsView.class, StatsZeroPaymentsViewCfg.QUERY_FIND_ALL_ZERO_PAYMENTS_BETWEEN_DATES, parameters);
        }
        
        Map<String, StatsZeroPaymentsView> mapDataZeroPayments = new HashMap<>();
        for (StatsZeroPaymentsView zeroPayments : dataComplete) {
            if (mapDataZeroPayments.get(zeroPayments.getIdUser()) != null) {
                if(!mapDataZeroPayments.get(zeroPayments.getIdUser()).getRouteName().contains(zeroPayments.getRouteName())){
                    mapDataZeroPayments.get(zeroPayments.getIdUser()).setRouteName(mapDataZeroPayments.get(zeroPayments.getIdUser()).getRouteName() + ", " + zeroPayments.getRouteName());
                }
            } else {
                mapDataZeroPayments.put(zeroPayments.getIdUser(), zeroPayments);
            }
        }
        // Asigna todas las rutas al usuario
        dataComplete.forEach(d -> d.setRouteName(mapDataZeroPayments.get(d.getIdUser()).getRouteName()));
        
        mapDataZeroPayments.clear();
        for (StatsZeroPaymentsView zeroPayments : dataComplete) {
            if (mapDataZeroPayments.get(zeroPayments.getIdUser()) != null) {
                mapDataZeroPayments.get(zeroPayments.getIdUser()).setTotalZeroPayments(mapDataZeroPayments.get(zeroPayments.getIdUser()).getTotalZeroPayments().add(BigDecimal.ONE));
            } else {
                zeroPayments.setTotalZeroPayments(BigDecimal.ONE);
                mapDataZeroPayments.put(zeroPayments.getIdUser(), zeroPayments);
            }
        }
        dataComplete.clear();
        dataComplete.addAll(mapDataZeroPayments.values());
        
        dataComplete.sort(Comparator.comparing(StatsZeroPaymentsView::getRouteName));
        
        return dataComplete;
    }

    public int fillTotalZeroPayments(Date startDate, Date endDate) {
        logger.debug("fillTotalZeroPayments");
        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<StatsZeroPaymentsView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsZeroPaymentsView.class, StatsZeroPaymentsViewCfg.QUERY_FIND_ALL_ZERO_PAYMENTS_BETWEEN_DATES, parameters);
       
        return dataComplete.size();
    }

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

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