import { Component, Inject, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { ExternalResearcherService } from '@app/core/services/external-sources/researcher/external-researcher.service';
import { BaseComponent } from '@common/base/base.component';
import { takeUntil } from 'rxjs/operators';
import { OrganizationEditorModel } from './add-organization.model';

@Component({
	selector: 'app-add-organization-component',
	templateUrl: 'add-organization.component.html',
})
export class AddOrganizationComponent extends BaseComponent implements OnInit {

	public formGroup: FormGroup;

	constructor(
		private externalResearcherService: ExternalResearcherService,
		public dialogRef: MatDialogRef<AddOrganizationComponent>,
		@Inject(MAT_DIALOG_DATA) public data: any
	) { super(); }

	ngOnInit(): void {
		const researcher = new OrganizationEditorModel();
		this.formGroup = researcher.buildForm();
	}

	send(value: any) {
		this.externalResearcherService.createResearcher(this.formGroup.value)
			.pipe(takeUntil(this._destroyed))
			.subscribe(
				null, null, () => this.dialogRef.close()
			);
	}

	addOrganization() {
		this.dialogRef.close(this.formGroup.value);
	}

	isFormValid() {
		return this.formGroup.valid;
	}
}
