/** @file
 * @author Edouard DUPIN
 * @copyright 2018, Edouard DUPIN, all right reserved
 * @license PROPRIETARY (see license file)
 */
import { Injectable, Component, OnInit, Input } from '@angular/core';
import { isNullOrUndefined } from '@kangaroo-and-rabbit/kar-cw';
import { Media, UserMediaAdvancement } from 'app/back-api';

import { AdvancementService, DataService } from 'app/service';

@Component({
	selector: 'app-element-video',
	templateUrl: './element-video.html',
	styleUrls: ['./element-video.less']
})

@Injectable()
export class ElementVideoComponent implements OnInit {
	// input parameters
	@Input() element: Media;
	data: Media;

	name: string = '';
	description: string = '';
	episodeDisplay: string = '';

	cover: string = '';
	covers: string[];

	advancement: UserMediaAdvancement = undefined;

	constructor(
		private dataService: DataService,
		private advancementService: AdvancementService) {
		// nothing to do.
	}
	ngOnInit() {
		this.data = this.element;
		this.name = this.element.name;
		this.description = this.element.description;
		if (isNullOrUndefined(this.element.episode)) {
			this.episodeDisplay = '';
		} else {
			this.episodeDisplay = `${this.element.episode} - `;
		}
		this.covers = this.dataService.getListThumbnailUrl(this.element.covers);

		this.advancementService.get(this.element.id)
			.then((response: UserMediaAdvancement) => {
				console.log(`  ==> get answer sub-series: ${JSON.stringify(response)}`);
				this.advancement = response;
			}).catch((response) => {
				this.advancement = undefined;
			});
	}
	updateAdvancement() {
		if (isNullOrUndefined(this.advancement)) {
			return { width: "0%" };
		}
		return { width: `${this.advancement.percent * 100}%` };
	}
}
