import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AppSettingsContract } from '../models-legacy';
import { environment } from '@environment';

@Injectable({
    providedIn: 'root',
})
export class NetworkService {
    private readonly baseUrl = environment.apiRootUrl;
    private readonly apiVersion = '1';
    private readonly assetsDir = 'assets';

    constructor(private http: HttpClient) {}

    public getAssetFile(fileName: string): Promise<any> {
        return this.http.get<any>(`${this.assetsDir}/${fileName}`).toPromise();
    }

    public getAppSettings(): Promise<AppSettingsContract> {
        return this.http
            .get<AppSettingsContract>(`${this.baseUrl}/appSettings`)
            .toPromise();
    }

    public getApiVersion(): Promise<string> {
        return this.http.get<string>(`${this.baseUrl}/version`).toPromise();
    }

    public getDictionary(dictionaryName: string): Promise<any[]> {
        return this.http
            .get<any[]>(
                `${this.baseUrl}/dictionary/${this.apiVersion}/${dictionaryName}`,
            )
            .toPromise();
    }
}
