// angular
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { OAuthService } from 'angular-oauth2-oidc';
import { OAuthErrorEvent } from 'angular-oauth2-oidc';

// libs
import { filter, map } from 'rxjs/operators';
import { Subject, Observable, BehaviorSubject, from } from 'rxjs';

// app
import { GeneralResultModel } from '../models/general-result.model';

@Injectable()
export class OidcHelperService {
	private isAuthenticatedSubject$ = new BehaviorSubject<IAuthenticatedState>({
		isAuthenticated: false,
		action: AuthenticatedActionEnum.init
	});
	isAuthenticated$ = this.isAuthenticatedSubject$.asObservable();

	private isDoneLoadingFailed = false;
	private isDoneLoadingSubject$ = new BehaviorSubject<boolean>(false);
	isDoneLoading$ = this.isDoneLoadingSubject$.asObservable();

	constructor(
		private oauthService: OAuthService,
		private router: Router,
	) {
		this.oauthService.oidc = false;

		// all events handler
		this.oauthService.events.subscribe(event => {
			if (event instanceof OAuthErrorEvent) {
				console.error('OAuthErrorEvent Object:', event);
				if (event.type === 'discovery_document_validation_error') {
					this.isDoneLoadingFailed = true;
					this.setIsAuthenticatedSubject$(false, AuthenticatedActionEnum.update);
				}
			} else {
				var hasValidAccessToken = this.hasValidAccessToken();
				var isAuthenticated = this.isAuthenticatedSubject$.getValue().isAuthenticated;
				if (isAuthenticated !== hasValidAccessToken) {
					this.setIsAuthenticatedSubject$(hasValidAccessToken, AuthenticatedActionEnum.update);
				}
			}
		});

		// This is tricky, as it might cause race conditions (where access_token is set in another
		// tab before everything is said and done there.
		// TODO: Improve this setup. See: https://github.com/jeroenheijmans/sample-angular-oauth2-oidc-with-auth-guards/issues/2
		window.addEventListener('storage',
			(event) => {
				// The `key` is `null` if the event was caused by `.clear()`
				if (event.key !== 'access_token' && event.key !== null) {
					return;
				}

				console.warn(
					'Noticed changes to access_token (most likely from another tab), updating isAuthenticated');
				this.setIsAuthenticatedSubject$(this.hasValidAccessToken(), AuthenticatedActionEnum.update);

				if (!this.hasValidAccessToken()) {
					console.log('storage access_token updated !this.hasValidAccessToken()');
					this.logout();
					this.navigateToLoginPage();
				}
			});

		// init login stata
		this.setIsAuthenticatedSubject$(this.hasValidAccessToken(), AuthenticatedActionEnum.update);

		// TODO: some time receive 'message' with data 'error' == 'session_error''
		/*window.addEventListener('message', e => {
			console.log('message', e);;
			if (e.origin === 'https://dev-w08xm0pi.us.auth0.com') {
				console.log('message stopPropagation');;
				e.stopPropagation();
			}
		});
		this.oauthService.events.subscribe(x => {
			console.log('events', x);
		});*/
		this.oauthService.events
			.pipe(filter(e => ['session_terminated', 'session_error'].includes(e.type)))
			.subscribe(e => {
				console.log('events session_terminated', e, this.hasValidAccessToken());
				this.navigateToLoginPage();
			});

		this.oauthService.setupAutomaticSilentRefresh();
	}

	private setIsAuthenticatedSubject$(
		isAuthenticated: boolean,
		action: AuthenticatedActionEnum
	) {
		var state = this.isAuthenticatedSubject$.getValue();
		if (state.isAuthenticated !== isAuthenticated || state.action !== action
		) {
			this.isAuthenticatedSubject$.next({ isAuthenticated, action });
		}
	}

	private navigateToLoginPage() {
		this.router.navigateByUrl('/authentication/signin');
	}

	logout() {
		this.oauthService.logOut(true);
	}

	refresh() {
		const refreshToken = this.oauthService.getRefreshToken();
		if (refreshToken) {
			this.oauthService.refreshToken();
		}
	}

	hasValidAccessToken() {
		return this.oauthService.hasValidAccessToken();
	}

	getAccessToken() {
		return this.oauthService.getAccessToken();
	}

	login(username: string, password: string) {
		//return this.oauthService.fetchTokenUsingPasswordFlow(username, password);
		return from(this.oauthService.fetchTokenUsingPasswordFlow(username, password));
	}

	loginByExternalLogin(
		provider: string,
		token: string,
		redirectUrl?: string
	): Observable<GeneralResultModel> {
		var result = new Subject<GeneralResultModel>();

		let params = {
			token: token,
			provider: provider,
		};
		this.oauthService
			.fetchTokenUsingGrant('external', params)
			.then(x => {
				this.setIsAuthenticatedSubject$(this.hasValidAccessToken(), AuthenticatedActionEnum.loggedIn);
				result.next({ success: true });
			})
			.catch(err => {
				result.next({ success: false, error: { code: err, description: err } });
			});

		return result;
	}

	loadUserProfile(): Promise<object> {
		return this.oauthService.loadUserProfile();
	}

	runInitialLoginSequence(): Promise<void> {
		if (location.hash) {
			//console.log('Encountered hash fragment, plotting as table...');
			//console.table(location.hash.substr(1).split('&').map(kvp => kvp.split('=')));
		}
		if (this.isDoneLoadingFailed) {
			return new Promise<void>(resolve => setTimeout(() => resolve(), 1500));
		}
		// 0. LOAD CONFIG:
		// First we have to check to see how the IdServer is
		// currently configured:
		return this.oauthService.loadDiscoveryDocument()

			// For demo purposes, we pretend the previous call was very slow
			.then(() => {
				new Promise<void>(resolve => setTimeout(() => resolve(), 1500));
			})

			// 1. HASH LOGIN:
			// Try to log in via hash fragment after redirect back
			// from IdServer from initImplicitFlow:
			.then(() => {
				this.oauthService.tryLogin();
			})
			.then(() => {
				if (this.hasValidAccessToken()) {
					return Promise.resolve();
				}

				// Refresh via refresh_token (no /connect/authorize cookie session).
				const refreshToken = this.oauthService.getRefreshToken();
				if (!refreshToken) {
					return Promise.resolve();
				}

				return this.oauthService.refreshToken()
					.then(() => Promise.resolve())
					.catch(result => {
						const error =
							result?.reason?.error ??
							result?.params?.error ??
							result?.error;
						const softErrors = [
							'interaction_required',
							'login_required',
							'account_selection_required',
							'consent_required',
							'access_denied',
							'invalid_grant',
						];

						if (error && softErrors.indexOf(error) >= 0) {
							console.warn('Token refresh needs user login.', error);
							return Promise.resolve();
						}

						return Promise.reject(result);
					});
			})
			.then(() => {
				this.isDoneLoadingSubject$.next(true);

				// Check for the strings 'undefined' and 'null' just to be sure. Our current
				// login(...) should never have this, but in case someone ever calls
				// initImplicitFlow(undefined | null) this could happen.
				if (this.oauthService.state &&
					this.oauthService.state !== 'undefined' &&
					this.oauthService.state !== 'null') {
					let stateUrl = this.oauthService.state;
					if (stateUrl.startsWith('/') === false) {
						stateUrl = decodeURIComponent(stateUrl);
					}
					console.log(`There was state of ${this.oauthService.state}, so we are sending you to: ${stateUrl}`);
					this.router.navigateByUrl(stateUrl);
				}
			})
			.catch(() => this.isDoneLoadingSubject$.next(true));
	}
}

export enum AuthenticatedActionEnum {
	init,
	update,
	loggedIn,
	loggedOff,
}

export interface IAuthenticatedState {
	isAuthenticated: boolean,
	action: AuthenticatedActionEnum,
	redirectUrl?: string,
}
