const _ = require('lodash');
const moment = require('moment');

const WorkingTime = {};

// 判断是否是节假日
WorkingTime.isHoliday = (date) => {
    if (!date) {
        date = new Date();
    }
    if (!(date instanceof Date)) {
        throw new Error("Invalid date");
    }
    const day = date.getDay();
    // 周六周日为假期
    if (day === 6 || day === 0) {
        return true;
    }
    return false;
};

// 根据传入时间(date)计算几个工作日(days)后的时间,days目前只能是整数
WorkingTime.caculateWorkingTime = (date, days) => {
    if (!(date instanceof Date)) {
        throw new Error("Invalid date");
    }
    if (typeof days !== 'number') {
        throw new Error("Days must be a number");
    }
    
    const param_date = new Date(date);
    
    const caculateDate = (i, days) => {
        if (i < days) {
            param_date.setDate(param_date.getDate() + 1);
            if (!WorkingTime.isHoliday(param_date)) {
                i++;
            }
            caculateDate(i, days);
        }
        return;
    };
    
    caculateDate(0, days);
    return param_date;
};

// 计算半个工作日后的时间
// 参数 next如果为true则表示只计算date时间后面紧接着的time_points
WorkingTime.caculatePlusHalfWorkingDay = (date, next) => {
    if (!(date instanceof Date)) {
        throw new Error("Invalid date");
    }
    
    let time_points = _.get(global, 'settings.remind.time_points');
    if (!time_points || _.isEmpty(time_points)) {
        time_points = [
            { hour: 8, minute: 30 },
            { hour: 14, minute: 30 }
        ];
    }

    const len = time_points.length;
    const start_date = new Date(date);
    const end_date = new Date(date);
    start_date.setHours(time_points[0].hour, time_points[0].minute, 0, 0);
    end_date.setHours(time_points[len - 1].hour, time_points[len - 1].minute, 0, 0);

    const caculated_date = new Date(date);

    let j = 0;
    const max_index = len - 1;

    if (date < start_date) {
        if (next) {
            j = 0;
        } else {
            // 加半个time_points
            j = len / 2;
        }
    } else if (date >= start_date && date < end_date) {
        let i = 0;
        while (i < max_index) {
            const first_date = new Date(date);
            const second_date = new Date(date);
            first_date.setHours(time_points[i].hour, time_points[i].minute, 0, 0);
            second_date.setHours(time_points[i + 1].hour, time_points[i + 1].minute, 0, 0);

            if (date >= first_date && date < second_date) {
                break;
            }
            i++;
        }

        if (next) {
            j = i + 1;
        } else {
            j = i + len / 2;
        }
    } else if (date >= end_date) {
        if (next) {
            j = max_index + 1;
        } else {
            j = max_index + len / 2;
        }
    }

    if (j > max_index) {
        // 隔天需判断节假日
        const workingDate = WorkingTime.caculateWorkingTime(date, 1);
        caculated_date.setTime(workingDate.getTime());
        caculated_date.setHours(
            time_points[j - max_index - 1].hour,
            time_points[j - max_index - 1].minute,
            0,
            0
        );
    } else if (j <= max_index) {
        caculated_date.setHours(time_points[j].hour, time_points[j].minute, 0, 0);
    }

    return caculated_date;
};

module.exports = WorkingTime;