class DateUtil {


    static differenceBetweenTwoDates( date1, date2 ) {
        // Convert both dates to milliseconds
        let date1_ms = date1.getTime();
        let date2_ms = date2.getTime();
        // Calculate the difference in milliseconds
        let difference_ms = date2_ms - date1_ms;
        //take out milliseconds
        difference_ms = difference_ms/1000;
        let seconds = Math.floor(difference_ms % 60);
        difference_ms = difference_ms/60;
        let minutes = Math.floor(difference_ms % 60);
        difference_ms = difference_ms/60;
        let hours = Math.floor(difference_ms % 24);
        let days = Math.floor(difference_ms/24);

        return {days,hours,minutes,seconds};
    }
    // Ex: 28/05/2019
    static formatDate(date) {
        let day = date.getDate();
        if (day < 10) {
            day = "0" + day;
        }
        let month = date.getMonth() + 1;
        if (month < 10) {
            month = "0" + month;
        }
        let year = date.getFullYear();

        return `${day}/${month}/${year}`
    }

    // Ex: Terça-Feira, 28 de maio de 2019 - 11:06
    static formatDate2(date){
        if(!(date instanceof Date)) return ' - ';
        return this.getDay(date.getDay()) + ", "+  date.getDate() + ' de ' + this.getMonth(date.getMonth()) + ' de ' +
            date.getFullYear();
    }

    static formatTime(date) {
        if(!(date instanceof Date)) return '';
        let time = '';
        let hours = date.getHours();
        let minutes = date.getMinutes();
        if (hours < 10) {
            time += '0' + hours + ':';
        } else {
            time += hours + ':';
        }

        if (minutes < 10) {
            time += '0' + minutes;
        } else {
            time += minutes;
        }
        return time;
    }

    // Formato: dd/mm/yyyy - hh:mm
    static formatDateTime(date){
        if(!(date instanceof Date)) return ' - ';
        return this.formatDate(date) + " - " + this.formatTime(date);
    }


    static getDay(day){
        switch (day) {
            case 0: return 'Domingo';
            case 1: return 'Segunda-feira';
            case 2: return 'Terça-feira';
            case 3: return 'Quarta-feira';
            case 4: return 'Quinta-feira';
            case 5: return 'Sexta-feira';
            case 6: return 'Sabado';
        }
    }

    static getMonth(month){
        switch (month) {
            case 0: return 'janeiro';
            case 1: return 'fevereiro';
            case 2: return 'março';
            case 3: return 'abril';
            case 4: return 'maio';
            case 5: return 'junho';
            case 6: return 'julho';
            case 7: return 'agosto';
            case 8: return 'setembro';
            case 9: return 'outubro';
            case 10: return 'novembro';
            case 11: return 'dezembro';
        }
    }

    static formatDate3(date){
        return ` ${this.getDiaAbreviado(date.getDay())} ${this.getMesAbreviado(date.getMonth())} ${date.getFullYear()}`
    }

    // dia/mes/ano semana
    // 30/12/2019 Terça-feira
    static formatDate4(date){
        if(!(date instanceof Date)) return ' - ';
        return `${this.formatDate(date)} - ${this.getDay(date.getDay())}`
    }

    static getMesAbreviado(month){
        switch (month) {
            case 0: return 'Jan';
            case 1: return 'Fev';
            case 2: return 'Mar';
            case 3: return 'Abr';
            case 4: return 'Mai';
            case 5: return 'Jun';
            case 6: return 'Jul';
            case 7: return 'Ago';
            case 8: return 'Set';
            case 9: return 'Out';
            case 10: return 'Nov';
            case 11: return 'Dez';
        }
    }

    static getDiaAbreviado(day){
        switch (day) {
            case 0: return 'Dom';
            case 1: return 'Seg';
            case 2: return 'Ter';
            case 3: return 'Qua';
            case 4: return 'Qui';
            case 5: return 'Sex';
            case 6: return 'Sab';
        }
    }

    static compareDateWithoutTime(date1,date2){
        if(date1 == null || date2 == null) return false;
        return this.formatDate(date1) === this.formatDate(date2);
    }

    static formatToIsoString(date) {
        let month = date.getMonth() + 1;
        let day = date.getDate();
        if (month < 10) {
            month = "" + 0 + month;
        }
        if (day < 10) {
            day = "" + 0 + day
        }
        return date.getFullYear() + "-" + month + "-" + day
    }
}



export default DateUtil;