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

import { Component, OnInit } from '@angular/core';
import { ArianeService, MediaService, SeasonService, SeriesService, TypeService } from './service';
import { EventOnMenu, MenuItem, MenuPosition, SSOService, SessionService, UserRoles222, UserService, isNullOrUndefined } from '@kangaroo-and-rabbit/kar-cw';

enum MenuEventType {
	SSO_LOGIN = "SSO_CALL_LOGIN",
	SSO_LOGOUT = "SSO_CALL_LOGOUT",
	SSO_SIGNUP = "SSO_CALL_SIGNUP",
	TYPE = "TYPE",
	SERIES = "SERIES",
	SEASON = "SEASON",
	VIDEO = "VIDEO",
}

@Component({
	selector: 'app-root',
	templateUrl: './app.component.html',
	styleUrls: [
		'./app.component.less',
	]
})
export class AppComponent implements OnInit {
	title: string = 'Karideo';
	autoConnectedDone: boolean = false;
	isConnected: boolean = false;
	signUpEnable: boolean = true;
	currentMenu: MenuItem[] = [];
	location: string = "home";

	constructor(
		private mediaService: MediaService,
		private seasonService: SeasonService,
		private seriesService: SeriesService,
		private typeService: TypeService,

		private userService: UserService,
		private sessionService: SessionService,
		private ssoService: SSOService,
		private arianeService: ArianeService) {


	}

	ngOnInit() {
		this.autoConnectedDone = false;
		this.isConnected = false;
		this.updateMainMenu();
		let self = this;
		this.sessionService.change.subscribe((isConnected) => {

			self.isConnected = isConnected;
			self.autoConnectedDone = true;
			self.updateMainMenu();
		});
		this.ssoService.checkSignUpEnable()
			.then((value: boolean) => {
				console.log(`Get value signUp = ${value}`);
				self.signUpEnable = value;
				self.updateMainMenu();
			}).catch((error: any) => {
				console.log(`Can not call the sso to check the sign-up_interface: ${error}`);
			});

		this.userService.checkAutoConnect().then(() => {
			self.autoConnectedDone = true;
		}).catch(() => {
			self.autoConnectedDone = true;
		}).finally(() => {
			self.autoConnectedDone = true;
		});
		this.arianeService.segmentChange.subscribe((_segmentName: string) => {
			self.updateMainMenu();
		});
		this.arianeService.typeChange.subscribe((_typeId: number) => {
			self.updateMainMenu();
		});
		this.arianeService.seriesChange.subscribe((_seriesId: number) => {
			self.updateMainMenu();
		});
		this.arianeService.seasonChange.subscribe((_seasonId: number) => {
			self.updateMainMenu();
		});
		this.arianeService.videoChange.subscribe((_videoId: number) => {
			self.updateMainMenu();
		});
	}

	eventOnMenu(data: EventOnMenu): void {
		//console.log(`plopppppppppp ${JSON.stringify(this.route.snapshot.url)}`);
		//console.log(`Get event on menu: ${JSON.stringify(data, null, 4)}`);
		switch (data.menu.otherData) {
			case MenuEventType.SSO_LOGIN:
				this.ssoService.requestSignIn();
				break;
			case MenuEventType.SSO_LOGOUT:
				this.ssoService.requestSignOut();
				break;
			case MenuEventType.SSO_SIGNUP:
				this.ssoService.requestSignUp();
				break;
			case MenuEventType.TYPE:
				this.arianeService.navigateType(this.arianeService.getTypeId(), data.newWindows, data.ctrl);
				break;
			case MenuEventType.SERIES:
				this.arianeService.navigateSeries(this.arianeService.getSeriesId(), data.newWindows, data.ctrl);
				break;
			case MenuEventType.SEASON:
				this.arianeService.navigateSeason(this.arianeService.getSeasonId(), data.newWindows, data.ctrl);
				break;
			case MenuEventType.VIDEO:
				this.arianeService.navigateVideo(this.arianeService.getVideoId(), data.newWindows, data.ctrl);
				break;
		}

	}
	updateMainMenu(): void {
		if (this.isConnected) {
			this.currentMenu = [
				{
					position: MenuPosition.LEFT,
					hover: `You are logged as: ${this.sessionService.getLogin()}`,
					icon: "menu",
					title: "Menu",
					subMenu: [
						{
							position: MenuPosition.LEFT,
							hover: "Go to Home page",
							icon: "home",
							title: "Home",
							navigateTo: "home",
						}, {
							position: MenuPosition.LEFT,
							icon: "group_work",
							title: this.arianeService.getTypeName(),
							otherData: MenuEventType.TYPE,
							callback: true,
							enable: !isNullOrUndefined(this.arianeService.getTypeId()),
						}, {
							position: MenuPosition.LEFT,
							icon: "tag",
							title: this.arianeService.getSeriesName(),
							otherData: MenuEventType.SERIES,
							callback: true,
							enable: !isNullOrUndefined(this.arianeService.getSeriesId()),
						}, {
							position: MenuPosition.LEFT,
							icon: "album",
							title: `Season ${this.arianeService.getSeasonName()}`,
							otherData: MenuEventType.SEASON,
							callback: true,
							enable: !isNullOrUndefined(this.arianeService.getSeasonId()),
						}, {
							position: MenuPosition.LEFT,
							icon: "movie",
							title: this.arianeService.getVideoName(),
							otherData: MenuEventType.VIDEO,
							callback: true,
							enable: !isNullOrUndefined(this.arianeService.getVideoId()),
						},
					],
				}, {
					position: MenuPosition.RIGHT,
					image: "assets/images/avatar_generic.svg",
					title: "",
					subMenu: [
						{
							position: MenuPosition.LEFT,
							hover: `You are logged as: <b>${this.sessionService.getLogin()}</b>`,
							title: `Sign in as ${this.sessionService.getLogin()}`,
						}, {
							position: MenuPosition.LEFT,
							icon: "add_circle",
							title: "Add media",
							navigateTo: "upload",
							enable: this.sessionService.hasRight(UserRoles222.admin),
						}, {
							position: MenuPosition.LEFT,
							icon: "settings",
							title: "Settings",
							navigateTo: "settings",
						}, {
							position: MenuPosition.LEFT,
							icon: "help",
							title: "Help",
							navigateTo: "help",
						}, {
							position: MenuPosition.LEFT,
							hover: "Exit connection",
							icon: "exit_to_app",
							title: "Sign out",
							callback: true,
							otherData: MenuEventType.SSO_LOGOUT,
						},
					],
				},
			];
		} else {
			this.currentMenu = [
				{
					position: MenuPosition.LEFT,
					hover: "Go to Home page",
					icon: "home",
					title: "Home",
					navigateTo: "home",
				}, {
					position: MenuPosition.RIGHT,
					hover: "Create a new account",
					icon: "add_circle_outline",
					title: "Sign-up",
					callback: true,
					model: this.signUpEnable ? undefined : "disable",
					otherData: MenuEventType.SSO_SIGNUP,
				}, {
					position: MenuPosition.RIGHT,
					hover: "Login page",
					icon: "account_circle",
					title: "Sign-in",
					callback: true,
					otherData: MenuEventType.SSO_LOGIN,
				},
			];
		}
	}
	getSegmentDisplayable(): string {
		let segment = this.arianeService.getCurrentSegment();
		if (segment === "type") {
			return "Type";
		}
		if (segment === "season") {
			return "Season";
		}
		if (segment === "series") {
			return "Series";
		}
		if (segment === "video") {
			return "Video";
		}
		return "";
	}
}
