/*
 * 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.enums.FeeStatus;
import com.arrebol.apc.model.views.FeesView;
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.FeesViewCfg;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

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

    public List<FeesView> fillFeesDataTable(Date startDate, Date endDate, String idUser) {
        logger.debug("fillFeesDataTable");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        List<FeesView> dataComplete = new ArrayList<>();

        if (idUser != null && !idUser.equals("")) {
            parameters.add(new ModelParameter(FeesViewCfg.FIELD_USER, idUser));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(FeesView.class, FeesViewCfg.QUERY_FIND_ALL_FEES_BETWEEN_DATES_USER, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(FeesView.class, FeesViewCfg.QUERY_FIND_ALL_FEES_BETWEEN_DATES, parameters);
        }
        
        Map<String, FeesView> mapDataFees = new HashMap<>();
        for (FeesView fee : dataComplete) {
            if (mapDataFees.get(fee.getIdUser()) != null) {
                mapDataFees.get(fee.getIdUser()).setTotalFees(mapDataFees.get(fee.getIdUser()).getTotalFees().add(fee.getTotalFees()));
            } else {
                mapDataFees.put(fee.getIdUser(), fee);
            }
            
            if(fee.getFeeStatus() != null && fee.getFeeStatus().equals(FeeStatus.PAID)){
                mapDataFees.get(fee.getIdUser()).setTotalFeePaid(mapDataFees.get(fee.getIdUser()).getTotalFeePaid().add(fee.getTotalFees()));
            }
        }
        dataComplete.clear();
        dataComplete.addAll(mapDataFees.values());
        return dataComplete;
    }
    
    public List<FeesView> fillFeesDataTableByRoute(Date startDate, Date endDate, String idRoute) {
        logger.debug("fillFeesDataTableByRoute");
        List<ModelParameter> parameters = new ArrayList<>();

         parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        List<FeesView> dataComplete = new ArrayList<>();
        
        if (idRoute != null && !idRoute.equals("")) {
            parameters.add(new ModelParameter(FeesViewCfg.FIELD_ROUTE, idRoute));
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(FeesView.class, FeesViewCfg.QUERY_FIND_ALL_FEES_BETWEEN_DATES_ROUTE, parameters);
        } else {
            dataComplete = genericEntityRepository.xmlQueryAPCEntities(FeesView.class, FeesViewCfg.QUERY_FIND_ALL_FEES_BETWEEN_DATES, parameters);
        }
        
        Map<String, FeesView> mapDataFees = new HashMap<>();
        dataComplete.forEach((fee) -> {
            if (mapDataFees.get(fee.getIdRoute()) != null) {
                mapDataFees.get(fee.getIdRoute()).setTotalFees(mapDataFees.get(fee.getIdRoute()).getTotalFees().add(fee.getTotalFees()));
            } else {
                mapDataFees.put(fee.getIdRoute(), fee);
            }
        });
        dataComplete.clear();
        dataComplete.addAll(mapDataFees.values());
        
        dataComplete.sort(Comparator.comparing(FeesView::getRouteName));
        return dataComplete;
    }

    public BigDecimal fillTotalFees(Date startDate, Date endDate) {
        logger.debug("fillFeesDataTable");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));
        List<FeesView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(FeesView.class, FeesViewCfg.QUERY_FIND_ALL_FEES_BETWEEN_DATES, parameters);
        BigDecimal totalFees = new BigDecimal("0");
        for (FeesView fee : dataComplete) {
            totalFees = totalFees.add(fee.getTotalFees());
        }
        return totalFees;
    }

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

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