/** @file
 * @author Edouard DUPIN
 * @copyright 2018, Edouard DUPIN, all right reserved
 * @license PROPRIETARY (see license file)
 */

import { Injectable } from '@angular/core';
import { SessionService, isArrayOf, isNullOrUndefined, isString } from '@kangaroo-and-rabbit/kar-cw';
import { UUID } from 'app/back-api';
import { RESTConfig, RESTUrl } from 'app/back-api/rest-tools';
import { environment } from 'environments/environment';

@Injectable()
export class DataService {

	getRestConfig(): RESTConfig {
		return {
			server: environment.server.karideo,
			token: this.session.getToken()
		}
	}

	constructor(private session: SessionService) {
		console.log('Start DataService');
	}

	/**
	 * Retrieve the Cover URL of a specific data id
	 * @param coverId Id of te cover
	 * @returns the url of the cover
	 */
	getUrl(dataId: UUID, optionalName?: string): string {
		if (isNullOrUndefined(optionalName)) {
			const url = RESTUrl({
				restModel: {
					endPoint: "data/{dataId}",
					tokenInUrl: true,
				},
				restConfig: this.getRestConfig(),
				params: {
					dataId
				}
			});
			console.log(`get URL = ${url}`);
			if (environment?.replaceDataToRealServer === true) {
				return url.replace("http://localhost:18080", "https://atria-soft.org");
			}
			return url;
		}
		const url = RESTUrl({
			restModel: {
				endPoint: "data/{dataId}/{optionalName}",
				tokenInUrl: true,
			},
			restConfig: this.getRestConfig(),
			params: {
				dataId,
				optionalName
			}
		});
		if (environment?.replaceDataToRealServer === true) {
			return url.replace("http://localhost:18080", "https://atria-soft.org");
		}
		return url;
	}
	/**
	 * Retrieve the list Cover URL of a specific data id
	 * @param coverId Id of te cover
	 * @returns the url of the cover
	 */
	getListUrl(dataIds?: UUID[]): string[] | undefined {
		if (!isArrayOf(dataIds, isString) || dataIds.length === 0) {
			return undefined;
		}
		let covers: string[] = [];
		for (let iii = 0; iii < dataIds.length; iii++) {
			covers.push(this.getUrl(dataIds[iii]));
		}
		return covers;
	}
	/**
	 * Retrieve the thumbnail cover URL of a specific data id
	 * @param coverId Id of te cover
	 * @returns the url of the cover
	 */
	getThumbnailUrl(coverId: UUID): string {
		const url = RESTUrl({
			restModel: {
				endPoint: "data/thumbnail/{coverId}",
				tokenInUrl: true,
			},
			restConfig: this.getRestConfig(),
			params: {
				coverId
			}
		});
		if (environment?.replaceDataToRealServer === true) {
			return url.replace("http://localhost:18080", "https://atria-soft.org");
		}
		return url;
	}
	/**
	 * Retrieve the list thumbnail cover URL of a specific data id
	 * @param coverId Id of te cover
	 * @returns the url of the cover
	 */
	getListThumbnailUrl(dataIds?: UUID[]): string[] | undefined {
		if (!isArrayOf(dataIds, isString) || dataIds.length === 0) {
			return undefined;
		}
		let covers: string[] = [];
		for (let iii = 0; iii < dataIds.length; iii++) {
			covers.push(this.getThumbnailUrl(dataIds[iii]));
		}
		return covers;
	}
}

