import {Component, Inject, OnInit} from "@angular/core";
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
import {map, takeUntil} from "rxjs/operators";
import {ProgressIndicationService} from "@app/core/services/progress-indication/progress-indication-service";
import {BaseComponent} from "@common/base/base.component";
import {SingleAutoCompleteConfiguration} from "@app/library/auto-complete/single/single-auto-complete-configuration";
import {Observable, of} from "rxjs";
import {Prefilling} from "@app/core/model/dataset/prefilling";
import {PrefillingService} from "@app/core/services/prefilling.service";
import {FormBuilder, FormGroup, Validators} from "@angular/forms";

@Component({
	selector: 'prefill-dataset-component',
	templateUrl: 'prefill-dataset.component.html',
	styleUrls: ['prefill-dataset.component.scss']
})
export class PrefillDatasetComponent extends BaseComponent implements OnInit {

	progressIndication = false;
	prefillAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
	configId: string = "zenodo";
	isPrefilled: boolean = false;
	prefillForm: FormGroup;

	constructor(public dialogRef: MatDialogRef<PrefillDatasetComponent>,
				private prefillingService: PrefillingService,
				private progressIndicationService: ProgressIndicationService,
				private fb: FormBuilder,
				@Inject(MAT_DIALOG_DATA) public data: any) {
		super();
	}

	ngOnInit() {
		this.progressIndicationService.getProgressIndicationObservable().pipe(takeUntil(this._destroyed)).subscribe(x => {
			setTimeout(() => { this.progressIndication = x; });
		});
		this.prefillForm = this.fb.group({
			type: this.fb.control(false),
			profile: this.fb.control('', Validators.required),
			prefill: this.fb.control(null, Validators.required)
		})
		if(this.data.availableProfiles && this.data.availableProfiles.length === 1) {
			this.prefillForm.get('profile').patchValue(this.data.availableProfiles[0]);
		}
		this.prefillAutoCompleteConfiguration = {
			filterFn: this.searchDatasets.bind(this),
			loadDataOnStart: false,
			displayFn: (item) => (item['name'].length > 60)?(item['name'].substr(0, 60) + "..." ):item['name'],
			titleFn: (item) => item['name'],
			subtitleFn: (item) => item['pid']
		};
	}

	public compareWith(object1: any, object2: any) {
		return object1 && object2 && object1.id === object2.id;
	}

	searchDatasets(query: string): Observable<Prefilling[]> {
		return this.prefillingService.getPrefillingList(query, this.configId).pipe(map(prefilling => prefilling.sort((a, b) => {
			if(a.name > b.name) {
				return 1;
			} else if(a.name < b.name) {
				return -1;
			} else {
				return 0;
			}
		})));
	}

	next() {
		if(this.isPrefilled) {
			this.prefillingService.getPrefillingDataset(this.prefillForm.get('prefill').value.pid, this.prefillForm.get('profile').value.id, this.configId).subscribe(wizard => {
				wizard.profile = this.prefillForm.get('profile').value;
				this.closeDialog(wizard);
			})
		} else {
			this.closeDialog();
		}
	}

	closeDialog(result = null): void {
		this.dialogRef.close(result);
	}
}
