import { CachingService } from "../Services";

export class DateUtils {
    private _cachingService = new CachingService();
    private language = 0;
    constructor() {
        if (this._cachingService.readPersistant("Language") == 1) {
            this.language = 1;
        }
    }
    public formatDate(dateTime: string) {
        if (!dateTime)
            return dateTime;

        if (typeof (dateTime) == "string" && dateTime.indexOf('T') > -1) {
            dateTime = dateTime.split('T')[0] + ' ' + dateTime.split('T')[1] + ' GMT+00:00'
        }

        let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
        if (this.language == 1) {
            return new Date(dateTime).toLocaleDateString("ar-EG", options);
        }
        return new Date(dateTime).toLocaleDateString("en-US", options);
    }
    public formatLongDateTime(dateTime: string) {
        if (!dateTime)
            return dateTime;

        if (typeof (dateTime) == "string" && dateTime.indexOf('T') > -1) {
            dateTime = dateTime.split('T')[0] + ' ' + dateTime.split('T')[1] + ' GMT+00:00'
        }

        let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' };
        if (this.language == 1) {
            return new Date(dateTime).toLocaleDateString("ar-EG", options);
        }
        return new Date(dateTime).toLocaleDateString("en-US", options);
    }
    public formatDateTime(dateTime: string) {
        if (!dateTime)
            return dateTime;

        if (typeof (dateTime) == "string" && dateTime.indexOf('T') > -1) {
            dateTime = dateTime.split('T')[0] + ' ' + dateTime.split('T')[1] + ' GMT+00:00'
        }

        let options = { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric' };
        if (this.language == 1) {
            return new Date(dateTime).toLocaleDateString("ar-EG", options);
        }
        return new Date(dateTime).toLocaleDateString("en-US", options);
    }
    public formatToServerDateTime(dateTime: Date) {
        if (!dateTime)
            return dateTime;
        let options = { year: 'numeric', month: 'numeric', day: 'numeric' };
        return dateTime.toLocaleDateString("en-US", options);
    }
}