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

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

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

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

export class TypeScene implements OnInit {
	typeId = -1;
	name: string = '';
	description: string = '';
	cover: string = null;
	covers: string[] = [];
	seriessError = '';
	series = [];
	videosError = '';
	videos = [];
	constructor(
		private dataService: DataService,
		private typeService: TypeService,
		private seriesService: SeriesService,
		private arianeService: ArianeService,
		private advancementService: AdvancementService) {

		/*
		this.route.params.subscribe(params => {
				console.log(params);
				if (params["id"]) {
					this.doSearch(params["id"]);
				}
			});
		*/
	}

	ngOnInit() {
		this.typeId = this.arianeService.getTypeId();
		let self = this;
		//console.log(`get type global id: ${this.typeId}`);
		this.typeService.get(this.typeId)
			.then((response) => {
				self.name = response.name;
				self.description = response.description;
				//console.log(`  ==> get answer type detail: ${JSON.stringify(response)}`);
				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]));
					}
				}
			}).catch((response) => {
				self.name = '???';
				self.description = '';
				self.covers = [];
				self.cover = null;
			});
		this.seriesService.getSeriesWithType(this.typeId)
			.then((response: Series[]) => {
				//console.log(`  ==> get answer sub-series: ${JSON.stringify(response)}`);
				self.seriessError = '';
				self.series = response;
			}).catch((response) => {
				//console.log(`  ==> get answer sub-series (ERROR): ${JSON.stringify(response)}`);
				self.seriessError = 'Wrong e-mail/login or password';
				self.series = [];
			});
		this.typeService.getSubVideo(this.typeId)
			.then((response) => {
				//console.log(`  ==> get answer sub-video: ${JSON.stringify(response)}`);
				self.videosError = '';
				self.videos = response;
			}).catch((response) => {
				//console.log(`  ==> get answer sub-video (ERROR): ${JSON.stringify(response)}`);
				self.videosError = 'Wrong e-mail/login or password';
				self.videos = [];
			});
	}
	onSelectSeries(event: any, idSelected: number): void {
		this.arianeService.navigateSeries(idSelected, event.which === 2, event.ctrlKey);
	}

	onSelectVideo(event: any, idSelected: number): void {
		console.log(` type of event: ${typeof event}`);
		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);
		}
	}
}
