function querySelectorAllForEachInnerHtml(documents, value) {
    documents.forEach((dom) => {
        dom.innerText = value
    })
}

function getCountDownUnit(distance) {

    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    days = days.toString()
    if (days.length === 1) {
        days = '0' + days
    }
    hours = hours.toString()
    if (hours.length === 1) {
        hours = '0' + hours
    }
    minutes = minutes.toString()
    if (minutes.length === 1) {
        minutes = '0' + minutes
    }
    seconds = seconds.toString()
    if (seconds.length === 1) {
        seconds = '0' + seconds
    }

    var result = {
        t_days: days,
        t_hours: hours,
        t_minutes: minutes,
        t_seconds: seconds
    }
    return result
}

function getCarouselDateDistance(now,end_at,carousel_days){
    var distance_days = Math.ceil((now-end_at) / (1000 * 60 * 60 * 24))
    var freq = Math.ceil(distance_days/carousel_days)
    var increase_days_times = freq * carousel_days * (1000 * 60 * 60 * 24)
    var distance = end_at + increase_days_times - now
    return distance
}

function initTimer(start_at, end_at, init,carousel_days) {

    var init_now = new Date().getTime();
    var init_distance = end_at - init_now;
    if ((init_distance < 0 && (carousel_days == 0 || !carousel_days)) || init_now < start_at || init) {
        querySelectorAllForEachInnerHtml(document.querySelectorAll(".ose-t_days"), '00')
        querySelectorAllForEachInnerHtml(document.querySelectorAll(".ose-t_hours"), '00')
        querySelectorAllForEachInnerHtml(document.querySelectorAll(".ose-t_minutes"), '00')
        querySelectorAllForEachInnerHtml(document.querySelectorAll(".ose-t_seconds"), '00')
        return null
    }

    /*if (init_now < start_at) {
        init_distance = end_at - start_at;
    }

    var init_show_units = getCountDownUnit(init_distance)
    var init_units = Object.keys(init_show_units)
    init_units.forEach(function(unit){
        querySelectorAllForEachInnerHtml(document.querySelectorAll("."+unit),init_show_units[unit])
    })
    if (init_now < start_at) {
        return null
    }*/


    var timer = setInterval(function () {
        var now = new Date().getTime();

        var distance = end_at - now;

        if (distance < 0 && (carousel_days == 0 || !carousel_days)) {
            clearInterval(timer);
            querySelectorAllForEachInnerHtml(document.querySelectorAll(".ose-t_days"), '00')
            querySelectorAllForEachInnerHtml(document.querySelectorAll(".ose-t_hours"), '00')
            querySelectorAllForEachInnerHtml(document.querySelectorAll(".ose-t_minutes"), '00')
            querySelectorAllForEachInnerHtml(document.querySelectorAll(".ose-t_seconds"), '00')
            return null
        }
        else if(distance < 0 && carousel_days > 0){
            distance = getCarouselDateDistance(now,end_at,carousel_days)
        }

        var show_units = getCountDownUnit(distance)
        //console.log(show_units)
        var units = Object.keys(show_units)
        units.forEach(function (unit) {
            querySelectorAllForEachInnerHtml(document.querySelectorAll(".ose-" + unit), show_units[unit])
        })

    }, 1000);
    return timer
}


export default {
    initTimer
}