import { Component, Input, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { GrantListingModel } from '@app/core/model/grant/grant-listing';
import { DmpCriteria } from '@app/core/query/dmp/dmp-criteria';
import { DmpProfileCriteria } from '@app/core/query/dmp/dmp-profile-criteria';
import { DmpProfileService } from '@app/core/services/dmp/dmp-profile.service';
import { DialodConfirmationUploadDmpProfiles } from '@app/ui/admin/dmp-profile/listing/criteria/dialog-confirmation-upload-profile/dialog-confirmation-upload-profiles.component';
import { BaseCriteriaComponent } from '@app/ui/misc/criteria/base-criteria.component';
import { ValidationErrorModel } from '@common/forms/validation/error-model/validation-error-model';
import { TranslateService } from '@ngx-translate/core';
import { takeUntil } from 'rxjs/operators';

@Component({
	selector: 'app-dmp-profile-criteria-component',
	templateUrl: './dmp-profile-criteria.component.html',
	styleUrls: ['./dmp-profile-criteria.component.scss'],
})
export class DmpProfileCriteriaComponent extends BaseCriteriaComponent implements OnInit {

	@Input()
	showGrant: boolean;
	public criteria: DmpProfileCriteria = new DmpProfileCriteria();
	filteringGrantsAsync = false;
	filteredGrants: GrantListingModel[];
	constructor(
		private dmpProfileService: DmpProfileService,
		private dialog: MatDialog,
		private language: TranslateService
	) {
		super(new ValidationErrorModel());
	}

	ngOnInit() {
		super.ngOnInit();
		if (this.criteria == null) { this.criteria = new DmpCriteria(); }
	}

	setCriteria(criteria: DmpProfileCriteria): void {
		this.criteria = criteria;
	}

	onCallbackError(error: any) {
		this.setErrorModel(error.error);
	}

	controlModified(): void {
		this.clearErrorModel();
		if (this.refreshCallback != null &&
			(this.criteria.like == null || this.criteria.like.length === 0 || this.criteria.like.length > 2)
		) {
			this.refreshCallback();
		}
	}


	// openDialog(): void {
	// 	const dialogRef = this.dialog.open(DialodConfirmationUploadDmpProfiles, {
	// 		restoreFocus: false,
	// 		data: {
	// 			message: this.language.instant('DMP-PROFILE-LISTING.UPLOAD.UPLOAD-XML-FILE-TITLE'),
	// 			confirmButton: this.language.instant('DMP-PROFILE-LISTING.UPLOAD.UPLOAD-XML'),
	// 			cancelButton: this.language.instant('DMP-PROFILE-LISTING.UPLOAD.UPLOAD-XML-FILE-CANCEL'),
	// 			name: '',
	// 			file: FileList,
	// 			sucsess: false
	// 		}
	// 	});
	// 	dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(data => {
	// 		if (data && data.sucsess && data.name != null && data.file != null) {
	// 			this.dmpProfileService.uploadFile(data.file, data.name)
	// 				.pipe(takeUntil(this._destroyed))
	// 				.subscribe();
	// 		}
	// 	});
	// }
}
