require('linqjs')
var moment=require('moment')

var schedule = require('node-schedule');

let _startDate = new moment('2018-04-25T00:00:01').utc(true);
let _endDate = new moment('2018-05-25T23:00:01').utc(true);
let _startTime = new moment('2018-05-25T17:17:40').utc(true);
let _daysEffected=127


setSchedule(1234,_startDate,_endDate,_startTime,_daysEffected,deneme)

function setSchedule(scheduleID,startDate,endDate,startTime,daysEffected,targetFunction){
    var arr=[]
    for (i=0;i<7;i++){if((daysEffected & Math.pow(2,i))==Math.pow(2,i))arr.push(i)}
    var start=startTime.toObject()
    var rule = new schedule.RecurrenceRule();
    rule.dayOfWeek = arr;
    rule.hour = start.hours;
    rule.minute =start.minutes;
    rule.second=start.seconds
    var j = schedule.scheduleJob('schedule' + scheduleID,{start:startDate,end:endDate, rule:rule}, ()=>{
        if (targetFunction && (typeof targetFunction == "function")) {
            targetFunction();
        }

    });
}

function deneme(){
    console.log('ooo yeaah')
}



function clearSchedules(){
    for(var key in schedule.scheduledJobs){
        if(key.startsWith('schedule')) {
            schedule.scheduledJobs[key].cancel()
        }
    }
}


setInterval(()=>{},500)