/*
 * 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.taxiservicios.utils;

import com.arrebol.taxiservicios.controller.util.DateWrapper;
import com.arrebol.taxiservicios.model.catalog.Holiday;
import com.arrebol.taxiservicios.model.core.Person;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.dashboard.ToCollect;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Picasso
 */
public class ToCollectUtility {

    /**
     *
     * @param holidays
     * @param startDay
     * @param endDay
     * @return
     */
    public static List<Date> daysToCollect(List<Holiday> holidays, int startDay, int endDay) {
        logger.info("daysToCollect");

        List<Date> dates = new ArrayList<>();

        try {
            for (int i = startDay; i <= endDay; i++) {
                Calendar date = Calendar.getInstance();

                date.set(Calendar.DAY_OF_MONTH, i);
                date.set(Calendar.HOUR_OF_DAY, 0);
                date.set(Calendar.MINUTE, 0);
                date.set(Calendar.SECOND, 0);
                date.set(Calendar.MILLISECOND, 0);

                Holiday holiday = new Holiday(date.getTime());

                if (!holidays.contains(holiday)) {
                    dates.add(date.getTime());
                }
            }
            logger.info("Totals days to be inserted " + dates.size());
        } catch (Exception e) {
            logger.error(e);
        }

        return dates;
    }

    /**
     *
     * @param dates
     * @param drivers
     * @param user
     * @return
     */
    public static List<ToCollect> fillToCollects(List<Date> dates, List<Person> drivers, User user) {
        logger.info("fillToCollects");

        List<ToCollect> collects = new ArrayList<>();

        try {
            drivers.forEach((driver) -> {
                dates.stream().map((date) -> new ToCollect(UUID.randomUUID().toString(),
                        date, Boolean.TRUE, 0, driver, user,
                        DateWrapper.getTodayMXTime())).forEachOrdered((collect) -> {
                    collects.add(collect);
                });
            });
        } catch (Exception e) {
            logger.error(e);
        }

        return collects;
    }

    private static final Logger logger = LogManager.getLogger(ToCollectUtility.class);
}
