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

import { Component, OnInit } from '@angular/core';
import { Season } from 'app/back-api';

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

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

export class SeriesScene implements OnInit {
	idSeries = -1;
	name: string = '';
	description: string = '';
	cover: string = '';
	covers: Array<string> = [];
	seasonsError: string = '';
	seasons: Season[] = [];
	videosError: string = '';
	videos: Array<any> = [];
	constructor(
		private advancementService: AdvancementService,
		private dataService: DataService,
		private seriesService: SeriesService,
		private arianeService: ArianeService,
	) {

	}

	ngOnInit() {
		// this.idUniverse = parseInt(this.route.snapshot.paramMap.get('universId'));
		// this.idType = parseInt(this.route.snapshot.paramMap.get('typeId'));
		this.idSeries = this.arianeService.getSeriesId();
		let self = this;
		let updateEnded = {
			seriesMetadata: false,
			subSaison: false,
			subVideo: false,
		};
		this.seriesService.get(this.idSeries)
			.then((response) => {
				self.name = response.name;
				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]));
					}
				}
				updateEnded.seriesMetadata = true;
				self.checkIfJumpIsNeeded(updateEnded);
			}).catch((response) => {
				self.description = '';
				self.name = '???';
				self.cover = null;
				self.covers = [];
				// no check just ==> an error occurred on season
			});
		console.log(`this.seriesService.getSeason(${this.idSeries})`);
		this.seriesService.getSeason(this.idSeries)
			.then((response: Season[]) => {
				console.log(`>>>> get season : ${JSON.stringify(response)}`)
				self.seasonsError = '';
				self.seasons = response;
				updateEnded.subSaison = true;
				self.checkIfJumpIsNeeded(updateEnded);
			}).catch((response) => {
				console.log(`>>>> get season (FAIL) : ${JSON.stringify(response)}`)
				self.seasonsError = 'Can not get the list of season in this series';
				self.seasons = [];
				updateEnded.subSaison = true;
				self.checkIfJumpIsNeeded(updateEnded);
			});
		this.seriesService.getVideo(this.idSeries)
			.then((response: Season[]) => {
				console.log(`>>>> get video : ${JSON.stringify(response)}`)
				self.videosError = '';
				self.videos = response;
				updateEnded.subVideo = true;
				self.checkIfJumpIsNeeded(updateEnded);
			}).catch((response) => {
				console.log(`>>>> get video (FAIL): ${JSON.stringify(response)}`)
				self.videosError = 'Can not get the List of video without season';
				self.videos = [];
				updateEnded.subVideo = true;
				self.checkIfJumpIsNeeded(updateEnded);
			});
	}
	onSelectSeason(event: any, idSelected: number): void {
		this.arianeService.navigateSeason(idSelected, event.which === 2, event.ctrlKey);
	}

	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);
		}
	}
	checkIfJumpIsNeeded(updateEnded: { seriesMetadata: boolean; subSaison: boolean; subVideo: boolean; }): void {
		// all update is ended
		if (updateEnded.seriesMetadata === false || updateEnded.subSaison === false || updateEnded.subVideo === false) {
			return;
		}
		// no local video
		if (this.videos.length > 0) {
			return;
		}
		// only one season:
		if (this.seasons.length !== 1) {
			return;
		}
		this.arianeService.navigateSeason(this.seasons[0].id, false, false, true);
	}
}

