/*
 * 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.StatsPaymentRouteView;
import com.arrebol.apc.model.views.constance.StatsPaymentRouteViewCfg;
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 StatsPaymentRouteController implements Serializable {

    public BigDecimal fillTotalPayment(Date startDate, Date endDate) {
        logger.debug("fillTotalPayment");
        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<StatsPaymentRouteView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPaymentRouteView.class, StatsPaymentRouteViewCfg.QUERY_FIND_ALL_PAYMENT_ROUTE_BETWEEN_DATES, parameters);
        BigDecimal totalPayment = new BigDecimal("0");
        List<String> listLoanTypeSum = new ArrayList<>();
        for (StatsPaymentRouteView payment : dataComplete) {
            totalPayment = totalPayment.add(payment.getTotalPayment());
            if (!listLoanTypeSum.contains(payment.getIdLoan())) {
                listLoanTypeSum.add(payment.getIdLoan());
                totalPayment = totalPayment.add(payment.getOpeningFee());
            }
        }
        return totalPayment;
    }

      public List<StatsPaymentRouteView> fillPaymentDataTable(Date startDate, Date endDate, String idRoute) {
        logger.debug("fillPaymentDataTable");
        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();
        
        List<StatsPaymentRouteView> dataComplete = new ArrayList<>();
        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        
        if (idRoute != null && !idRoute.equals("")) {
            parameters.add(new ModelParameter(StatsPaymentRouteViewCfg.FIELD_ROUTE, idRoute));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPaymentRouteView.class, StatsPaymentRouteViewCfg.QUERY_FIND_ALL_PAYMENT_ROUTE_BETWEEN_DATES_ROUTE, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPaymentRouteView.class, StatsPaymentRouteViewCfg.QUERY_FIND_ALL_PAYMENT_ROUTE_BETWEEN_DATES, parameters);
        }
        
        Map<String, StatsPaymentRouteView> mapDataPayment = new HashMap<>();
        List<String> listLoanTypeSum = new ArrayList<>();
        
        for (StatsPaymentRouteView payment : dataComplete) {
            if (mapDataPayment.get(payment.getIdRoute()) != null) {
                if(!mapDataPayment.get(payment.getIdRoute()).getUserName().contains(payment.getUserName())){
                    mapDataPayment.get(payment.getIdRoute()).setUserName(mapDataPayment.get(payment.getIdRoute()).getUserName() + ", " + payment.getUserName());
                }
                mapDataPayment.get(payment.getIdRoute()).setTotalPayment(
                        mapDataPayment.get(payment.getIdRoute()).getTotalPayment().add(payment.getTotalPayment()));
                
                if (!listLoanTypeSum.contains(payment.getIdLoan())) {
                    listLoanTypeSum.add(payment.getIdLoan());
                    mapDataPayment.get(payment.getIdRoute()).setTotalPayment(
                            mapDataPayment.get(payment.getIdRoute()).getTotalPayment().add(payment.getOpeningFee()));
                }
            } else {
                payment.setTotalPayment(payment.getTotalPayment().add(payment.getOpeningFee()));
                mapDataPayment.put(payment.getIdRoute(), payment);
                listLoanTypeSum.add(payment.getIdLoan());
            }
        }
        dataComplete.clear();
        dataComplete.addAll(mapDataPayment.values());
        
        dataComplete.sort(Comparator.comparing(StatsPaymentRouteView::getRouteName));
        return dataComplete;
    }
    
    final Logger logger = LogManager.getLogger(StatsPaymentRouteController.class);
    private final GenericEntityRepository genericEntityRepository;

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