import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { ActivatedRoute, Params } from '@angular/router';
import { BaseComponent } from '@common/base/base.component';
import { takeUntil } from 'rxjs/operators';
import { Oauth2DialogService } from './service/oauth2-dialog.service';

@Component({
  selector: 'app-oauth2-dialog',
  templateUrl: './oauth2-dialog.component.html',
  styleUrls: ['./oauth2-dialog.component.scss']
})
export class Oauth2DialogComponent extends BaseComponent implements OnInit{

	constructor(
		private route: ActivatedRoute,
		private oauth2dialogService: Oauth2DialogService
	) {
		super();
	}


	ngOnInit(): void {
		this.route.queryParams.pipe(takeUntil(this._destroyed))
		.subscribe((params: Params) => {
			const url = params['url'];
			if (!params['code']) { this.loadUrl(url) } else { this.sendCode(params['code']); }
		});
	}

	private loadUrl(url: string ) {
		window.location.href = url;
	}

	private sendCode(code: string) {
		localStorage.setItem('oauthCode', code);
		window.close();
	}

}
