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

import { Component, OnInit } from '@angular/core';

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

@Component({
	selector: 'app-season',
	templateUrl: './season.html',
	styleUrls: ['./season.less']
})
export class SeasonScene implements OnInit {
	name: string = '';
	seriesName: string = '';
	description: string = '';
	seriesId: number = null;
	cover: string = '';
	covers: Array<string> = [];
	idSeason = -1;
	videosError = '';
	videos = [];
	constructor(
		private advancementService: AdvancementService,
		private dataService: DataService,
		private seasonService: SeasonService,
		private seriesService: SeriesService,
		private arianeService: ArianeService,
	) {

	}

	ngOnInit() {
		this.idSeason = this.arianeService.getSeasonId();

		let self = this;
		this.seasonService.get(this.idSeason)
			.then((response: any) => {
				console.log("Get element ! " + JSON.stringify(response));
				self.name = response.name;
				self.seriesId = response.parentId;
				self.description = response.description;
				if (response.covers === undefined || response.covers === null || response.covers.length === 0) {
					self.cover = null;
					self.covers = [];
				} else {
					self.cover = self.dataService.getThumbnailUrl(response.covers[0]);
					for (let iii = 0; iii < response.covers.length; iii++) {
						self.covers.push(self.dataService.getThumbnailUrl(response.covers[iii]));
					}
				}
				self.seriesService.get(self.seriesId)
					.then((response2: any) => {
						self.seriesName = response2.name;
					}).catch((response2: any) => {
						self.seriesName = '';
					});
			}).catch((response3: any) => {
				self.description = '';
				self.name = '???';
				self.seriesName = '';
				self.seriesId = null;
				self.cover = null;
				self.covers = [];
			});
		console.log(`get parameter id: ${this.idSeason}`);
		this.seasonService.getVideo(this.idSeason)
			.then((response4: any) => {
				self.videosError = '';
				self.videos = response4;
			}).catch((response5: any) => {
				self.videosError = 'Can not get the List of video without season';
				self.videos = [];
			});
	}

	onSelectVideo(event: any, idSelected: number): void {
		if (event.ctrlKey && event.altKey && event.which === 2) {
			// special feature ==> add count id...
			this.advancementService.updateTime(idSelected, 0, 1, true);
		} else {
			this.arianeService.navigateVideo(idSelected, event.which === 2, event.ctrlKey);
		}
	}
}
