import { Inject, Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { Language } from '@shared/enumerators';

import { calendarRu } from '@shared/localizations/calendar-ru';
import { calendarEn } from '@shared/localizations/calendar-en';
import { calendarFr } from '@shared/localizations/calendar-fr';
import { calendarEs } from '@shared/localizations/calendar-es';
import { calendarDe } from '@shared/localizations/calendar-de';
import { calendarIt } from '@shared/localizations/calendar-it';
import { calendarJp } from '@shared/localizations/calendar-jp';
import { calendarKo } from '@shared/localizations/calendar-ko';
import { calendarZh } from '@shared/localizations/calendar-zh';
import { APP_BASE_HREF } from '@angular/common';

@Injectable({
    providedIn: 'root',
})
export class LocalizationService {
    Language: Language;
    Country: Language;
    private translate: TranslateService; // injected via method injection

    constructor(@Inject(APP_BASE_HREF) private baseHref: string) {
        // basehref is always: "/ru-ru" (/country-language)
        this.Country = baseHref.substring(1, 3) as Language || Language.ru;
        this.Language = baseHref.substring(4, 6) as Language || Language.ru;
    }

    ready$: Promise<any>;

    // method injection, not to have cyclic deps via HttpInterceptors
    init(translate: TranslateService) {
        this.translate = translate;
        
        this.translate.addLangs(Object.keys(Language));
        this.translate.setDefaultLang(this.Language);
        
        this.ready$ = this.translate.use(this.Language).toPromise();
    }

    get calendarTranslate() {
        switch (this.Language) {
            case Language.ru:
                return calendarRu;
            case Language.en:
                return calendarEn;
            case Language.fr:
                return calendarFr;
            case Language.es:
                return calendarEs;
            case Language.de:
                return calendarDe;
            case Language.it:
                return calendarIt;
            case Language.jp:
                return calendarJp;
            case Language.ko:
                return calendarKo;
            case Language.zh:
                return calendarZh;
        }
        return calendarEn;
    }
}
