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

import { Injectable } from '@angular/core';
import { SessionService, DataStore } from '@kangaroo-and-rabbit/kar-cw';
import { Media, MediaResource, UUID } from 'app/back-api';
import { ProgressCallback, RESTAbort, RESTConfig } from 'app/back-api/rest-tools';
import { environment } from 'environments/environment';
import { GenericDataService } from './GenericDataService';

@Injectable()
export class MediaService extends GenericDataService<Media> {
	getRestConfig(): RESTConfig {
		return {
			server: environment.server.karideo,
			token: this.session.getToken()
		}
	}
	private lambdaGets(): Promise<Media[]> {
		return MediaResource.gets({ restConfig: this.getRestConfig() });
	}

	constructor(private session: SessionService) {
		super();
		console.log('Start MediaService');
		const self = this;
		this.setStore(new DataStore<Media>(() => self.lambdaGets()));
	}

	uploadFile(file: File,
		series?: string,
		seriesId?: number,
		season?: number,
		episode?: number,
		title?: string,
		typeId?: number,
		progress: ProgressCallback | undefined = undefined,
		cancelHandle: RESTAbort | undefined = undefined) {
		const formData = {
			fileName: file.name,
			file,
			seriesId: seriesId !== null ? seriesId.toString() : null,
			series: series ?? null,
			season: season !== null ? season.toString() : null,
			episode: episode !== null ? episode.toString() : null,
			title: title ?? null,
			typeId: typeId !== null ? typeId.toString() : null,
			universe: null,
		};
		return MediaResource.uploadFile({
			restConfig: this.getRestConfig(),
			data: formData,
			callbacks: {
				progressUpload: progress,
				abortHandle: cancelHandle,
			}
		});
	}

	patch(id: number, data: Media): Promise<Media> {
		const self = this;
		return new Promise((resolve, reject) => {
			MediaResource.patch({
				restConfig: this.getRestConfig(),
				params: {
					id
				},
				data
			}).then((value: Media) => {
				self.dataStore.updateValue(value);
				resolve(value);
			}).catch((error) => {
				reject(error);
			});
		});
	}

	delete(id: number): Promise<void> {
		const self = this;
		return new Promise((resolve, reject) => {
			MediaResource.remove({
				restConfig: this.getRestConfig(),
				params: {
					id
				}
			}).then(() => {
				self.dataStore.delete(id);
				resolve();
			}).catch((error) => {
				reject(error);
			});
		});
	}

	deleteCover(id: number, coverId: UUID): Promise<Media> {
		let self = this;
		return new Promise((resolve, reject) => {
			MediaResource.removeCover({
				restConfig: this.getRestConfig(),
				params: {
					id,
					coverId
				}
			}).then((value) => {
				self.dataStore.updateValue(value);
				resolve(value);
			}).catch((error) => {
				reject(error);
			});
		});
	}
	uploadCover(id: number,
		file: File,
		progress: ProgressCallback | undefined = undefined,
		cancelHandle: RESTAbort | undefined = undefined): Promise<Media> {
		let self = this;
		return new Promise((resolve, reject) => {
			MediaResource.uploadCover({
				restConfig: this.getRestConfig(),
				params: {
					id,
				},
				data: {
					file,
				},
				callbacks: {
					progressUpload: progress,
					abortHandle: cancelHandle,
				}
			}).then((value) => {
				self.dataStore.updateValue(value);
				resolve(value);
			}).catch((error) => {
				reject(error);
			});
		});
	}
}

