import moment from 'moment';

const TimeUtil = {
    //formats seconds to a neat time string hh:mm:ss
    formatTime: function(t) {
        if (t === -1) {
            return '00:00:00';
        }
        const pt = moment.duration(t * 1000);
        const h = pt.hours() < 10 ? '0' + pt.hours() : pt.hours();
        const m = pt.minutes() < 10 ? '0' + pt.minutes() : pt.minutes();
        const s = pt.seconds() < 10 ? '0' + pt.seconds() : pt.seconds();
        return h + ':' + m + ':' + s;
    },

    //formats milliseconds to a time string hh:mm:ss
    formatMillisToTime: function(millis) {
        if (millis === -1) {
            return '00:00:00';
        }
        const pt = moment.duration(millis);
        const h = pt.hours() < 10 ? '0' + pt.hours() : pt.hours();
        const m = pt.minutes() < 10 ? '0' + pt.minutes() : pt.minutes();
        const s = pt.seconds() < 10 ? '0' + pt.seconds() : pt.seconds();
        return h + ':' + m + ':' + s;
    },

    //TODO not sure anymore why this was useful. It seems bad to round off seconds
    playerPosToMillis: function(sec) {
        //a double
        return parseInt(sec) * 1000;
    },

    getYearFromDate: function(dateMillis) {
        return new Date(dateMillis).getFullYear();
    },

    yearToUNIXTime: function(year) {
        return new Date(year + '-01-01').valueOf();
    },

    UNIXTimeToPrettyDate(epochMillis) {
        if (epochMillis !== null) {
            return moment(epochMillis).format('DD-MM-YYYY');
        }
        return '-';
    },

    /* ---------------------- FUNCTIONS WORKING BASED ON CollectionConfig.getFormattedDates() ------------------------- */

    extractDate(formattedDate) {
        return formattedDate ? new Date(formattedDate) : null;
    },

    isPeriod(formattedDates) { // Checks for a range in the form "yyyy - yyyy".
    	return formattedDates && formattedDates.length === 1  && typeof formattedDates[0] === 'string' && formattedDates[0].split('-').length === 2;
    },

    //takes an array of formatted date strings or period strings and returns the timestamp of the earliest date (or period start)
    formattedDatesToLowestTimestamp(formattedDates) {
        if(!formattedDates) return null;

        if (!Array.isArray(formattedDates)) {
            formattedDates = [formattedDates];
        }
        // If found then returns the first year in the range as a TimeStamp.
        if(TimeUtil.isPeriod(formattedDates) && typeof formattedDates[0] === 'string') {
            return TimeUtil.extractDate(formattedDates[0].split('-').map(it => it.trim())[0]).getTime();
        }
        return formattedDates ? Math.min(...formattedDates.map(fd => TimeUtil.extractDate(fd) ? TimeUtil.extractDate(fd).getTime() : null)) : null;
    },

	sortedFormattedDates(formattedDates) {
		return formattedDates ? formattedDates.sort((a, b) => TimeUtil.extractDate(a) > TimeUtil.extractDate(b) ? 1 : -1) : null;
	}

};

export default TimeUtil;
