import moment from 'moment';

export function formatSeconds(sec) {
    return formatTime(sec, 'seconds');
}

export function formatTime(amount, unit) {
    let duration = moment.duration(amount, unit);
    let hours = Math.floor(duration.hours());
    let result =
        zerofill(Math.floor(duration.minutes())) + ':' + zerofill(Math.floor(duration.seconds()));
    if (hours >= 1) {
        result = hours + ':' + result;
    }
    return result;
}

function zerofill(value, prefix = '00') {
    return (prefix + value).slice(-prefix.length);
}

export function getProcessedPercentage(prcessedDistance, totalWidth, indicatorRadius) {
    let processPercentage = 0;

    if (prcessedDistance < indicatorRadius) {
        processPercentage = 0;
    } else if (prcessedDistance > totalWidth - indicatorRadius) {
        processPercentage = 1;
    } else {
        processPercentage =
            (prcessedDistance - indicatorRadius) / (totalWidth - indicatorRadius * 2);
    }
    return processPercentage;
}

export const KEY_CODE = Object.freeze({
    LEFT: 37,
    UP: 38,
    RIGHT: 39,
    DOWN: 40,
    END: 35,
    HOME: 36,
    PAGEUP: 33,
    PAGEDOWN: 34
});
