/*
 * 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.EmployeeSaving;
import com.arrebol.apc.model.admin.constance.GoalCfg;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.model.enums.EmployeeSavingType;
import com.arrebol.apc.model.views.StatsEmployeeSavingView;
import com.arrebol.apc.model.views.constance.StatsEmployeeSavingViewCfg;
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.HashMap;
import java.util.Map;

/**
 *
 * @author David Rodriguez
 */
public class StatsEmployeeSavingController implements Serializable {
 public List<StatsEmployeeSavingView> fillEmployeeSavingDataTable (Date startDate, Date endDate) {
        logger.debug("fillEmployeeSavingDataTable");
        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<StatsEmployeeSavingView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsEmployeeSavingView.class, StatsEmployeeSavingViewCfg.QUERY_FIND_ALL_EMPLOYEE_SAVING_BETWEEN_DATES, parameters);
        Map<String, StatsEmployeeSavingView> mapDataEmployeeSaving = new HashMap<>();
         for (StatsEmployeeSavingView employeeSaving : dataComplete) {
            if (mapDataEmployeeSaving.get(employeeSaving.getIdUser()) != null) {
                if (employeeSaving.getType().equals(EmployeeSavingType.SAVING)) {
                    mapDataEmployeeSaving.get(employeeSaving.getIdUser()).setEmployeeSaving(
                            mapDataEmployeeSaving.get(employeeSaving.getIdUser()).getEmployeeSaving().add(employeeSaving.getEmployeeSaving()));
                }else{
                    mapDataEmployeeSaving.get(employeeSaving.getIdUser()).setEmployeeSaving(
                            mapDataEmployeeSaving.get(employeeSaving.getIdUser()).getEmployeeSaving().subtract(employeeSaving.getEmployeeSaving()));
                }
            } else {
                if (employeeSaving.getType().equals(EmployeeSavingType.SAVING)) {
                    employeeSaving.setEmployeeSaving(employeeSaving.getEmployeeSaving());
                }else{
                    employeeSaving.setEmployeeSaving(employeeSaving.getEmployeeSaving().negate());
                }
                mapDataEmployeeSaving.put(employeeSaving.getIdUser(), employeeSaving);
            }
        }
         
         dataComplete.clear();
         dataComplete.addAll(mapDataEmployeeSaving.values());
         
        // Asigna todas las rutas al usuario
        // dataComplete.forEach(d -> d.setRouteName(mapDataEmployeeSaving.get(d.getIdUser()).getRouteName()));
        
        return dataComplete;
    }
    
    public List<StatsEmployeeSavingView> getEmployeeSavingById (Date startDate, Date endDate, String idUser) {
        logger.debug("getEmployeeSavingById");
        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));
        parameters.add(new ModelParameter(StatsEmployeeSavingViewCfg.USER_ID, idUser));
        List<StatsEmployeeSavingView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsEmployeeSavingView.class, StatsEmployeeSavingViewCfg.QUERY_FIND_ALL_EMPLOYEE_SAVING_BY_ID_BETWEEN_DATES, parameters);
        
        return dataComplete;
        
    }

    public BigDecimal fillTotalEmployeeSaving(Date startDate, Date endDate) {
        logger.debug("fillTotalEmployeeSaving");
        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<StatsEmployeeSavingView> dataComplete = genericEntityRepository.xmlQueryAPCEntities(StatsEmployeeSavingView.class, StatsEmployeeSavingViewCfg.QUERY_FIND_ALL_EMPLOYEE_SAVING_BETWEEN_DATES, parameters);
        BigDecimal totalEmployeeSaving = BigDecimal.ZERO;
        for (StatsEmployeeSavingView employeeSaving : dataComplete) {
            if (employeeSaving.getType().equals(EmployeeSavingType.SAVING)) {
                totalEmployeeSaving = totalEmployeeSaving.add(employeeSaving.getEmployeeSaving());
            }else{
                totalEmployeeSaving = totalEmployeeSaving.subtract(employeeSaving.getEmployeeSaving());
            }
        }
        return totalEmployeeSaving;
    }

    public boolean saveEmployeeSaving(EmployeeSaving employeeSaving) {
        logger.debug("saveEmployeeSaving");
        boolean success = genericEntityRepository.insertAPCEntity(employeeSaving);

        return success;
    }
    
        /**
     *
     * Searching user by id.
     *
     * @param userId
     * @return
     */
    public User getUserById(String userId) {
        logger.debug("getUserById");

        return (User) genericEntityRepository.selectAPCEntityById(User.class, userId);
    }
    
    final Logger logger = LogManager.getLogger(StatsEmployeeSavingController.class);
    private final GenericEntityRepository genericEntityRepository;

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