/*
 * 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.StatsPaymentView;
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.StatsPaymentViewCfg;
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 StatsPaymentController implements Serializable {

    public List<StatsPaymentView> fillPaymentDataTable(Date startDate, Date endDate, String idUser) {
        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();
        
        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        List<StatsPaymentView> dataComplete = new ArrayList<>();
        
        if (idUser != null && !idUser.equals("")) {
            parameters.add(new ModelParameter(StatsPaymentViewCfg.FIELD_USER, idUser));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPaymentView.class, StatsPaymentViewCfg.QUERY_FIND_ALL_PAYMENT_BETWEEN_DATES_USER, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPaymentView.class, StatsPaymentViewCfg.QUERY_FIND_ALL_PAYMENT_BETWEEN_DATES, parameters);
        }
        
        Map<String, StatsPaymentView> mapDataPayment = new HashMap<>();
        List<String> listLoanTypeSum = new ArrayList<>();
        
        for (StatsPaymentView payment : dataComplete) {
            if (mapDataPayment.get(payment.getIdUser()) != null) {
                mapDataPayment.get(payment.getIdUser()).setTotalPayment(
                        mapDataPayment.get(payment.getIdUser()).getTotalPayment().add(payment.getTotalPayment()));
                if(!listLoanTypeSum.contains(payment.getIdLoan())){
                    listLoanTypeSum.add(payment.getIdLoan());
                    mapDataPayment.get(payment.getIdUser()).setTotalPayment(
                        mapDataPayment.get(payment.getIdUser()).getTotalPayment().add(payment.getOpeningFee()));
                }
            } else {
                payment.setTotalPayment(payment.getTotalPayment().add(payment.getOpeningFee()));
                mapDataPayment.put(payment.getIdUser(), payment);
                listLoanTypeSum.add(payment.getIdLoan());
                
            }
        }
        dataComplete.clear();
        dataComplete.addAll(mapDataPayment.values());
        
        dataComplete.sort(Comparator.comparing(StatsPaymentView::getRouteName));
        return dataComplete;
    }

    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<StatsPaymentView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsPaymentView.class, StatsPaymentViewCfg.QUERY_FIND_ALL_PAYMENT_BETWEEN_DATES, parameters);
        List<String> listLoanTypeSum = new ArrayList<>();
        BigDecimal totalPayment = new BigDecimal("0");
        for (StatsPaymentView payment : dataComplete) {
            totalPayment = totalPayment.add(payment.getTotalPayment());
            if(!listLoanTypeSum.contains(payment.getIdLoan())){
                    listLoanTypeSum.add(payment.getIdLoan());
                    totalPayment = totalPayment.add(payment.getOpeningFee());
                }
        }
        return totalPayment;
    }

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

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