import { Pipe, PipeTransform } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';


@Pipe({
    name: 'millisToDayHourMinuteSecond'
})
export class MillisToDayHourMinuteSecond implements PipeTransform {
    constructor(private translateService: TranslateService) {}
    transform(value: any, ...args: any[]) {
        let valueNumber: number;
        const type = typeof value;
        if (type === 'number') {
            valueNumber = value;
        } else if (type === 'string') {
            valueNumber = + value;
        }
        if (valueNumber > 0) {
            const days = Math.trunc(valueNumber / 86400);
            valueNumber = valueNumber % 86400;
            const hours = Math.trunc(valueNumber / 3600);
            valueNumber = valueNumber % 3600;
            const minutes = Math.trunc(valueNumber / 60);
            valueNumber = valueNumber % 60;
            const seconds = valueNumber;
            return (days < 10 ? '0' : '') + days + this.translateService.instant('pipe.format.day')
            + ' ' + (hours < 10 ? '0' : '') + hours + this.translateService.instant('pipe.format.hour')
            + ' ' + (minutes < 10 ? '0' : '') + minutes + this.translateService.instant('pipe.format.minute')
            + ' ' + (seconds < 10 ? '0' : '') + seconds + this.translateService.instant('pipe.format.seconds');
        }
        return value;
    }

}
