import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { DmpListingModel } from '../../../../core/model/dmp/dmp-listing';
import { MatDialog } from '@angular/material/dialog';
import { DmpInvitationDialogComponent } from '../../invitation/dmp-invitation.component';
import { Router, ActivatedRoute } from '@angular/router';
import { DatasetService } from '../../../../core/services/dataset/dataset.service';
import { AuthService } from '../../../../core/services/auth/auth.service';
import { Principal } from '../../../../core/model/auth/Principal';
import { TranslateService } from '@ngx-translate/core';
import { DmpStatus } from '../../../../core/common/enum/dmp-status';

@Component({
	selector: 'app-dmp-listing-item-component',
	templateUrl: './dmp-listing-item.component.html',
	styleUrls: ['./dmp-listing-item.component.scss'],
})
export class DmpListingItemComponent implements OnInit {

	@Input() dmp: DmpListingModel;
	@Input() showDivider: boolean = true;
	@Output() onClick: EventEmitter<DmpListingModel> = new EventEmitter();

	isDraft: boolean;
	isFinalized: boolean;
	isPublished: boolean;

	constructor(
		private router: Router,
		private dialog: MatDialog,
		private authentication: AuthService,
		private translate: TranslateService) { }

	ngOnInit() {
		if (this.dmp.status == DmpStatus.Draft) {
			this.isDraft = true;
			this.isFinalized = false;
			this.isPublished = false;
		}
		else if (this.dmp.status == DmpStatus.Finalized) {
			this.isDraft = false;
			this.isFinalized = true;
			this.isPublished = false;
			if (this.dmp.public == true) { this.isPublished = true }
		}
	}

	openShareDialog(rowId: any, rowName: any) {
		const dialogRef = this.dialog.open(DmpInvitationDialogComponent, {
			// height: '250px',
			// width: '700px',
			restoreFocus: false,
			data: {
				dmpId: rowId,
				dmpName: rowName
			}
		});
	}

	editClicked(dmpId: String) {
		this.router.navigate(['/plans/edit/' + dmpId]);
	}

	addDataset(rowId: String) {
		this.router.navigate(['/datasets/new/' + rowId]);
	}

	showDatasets(rowId: String, rowLabel: String) {
		this.router.navigate(['/datasets/dmp/' + rowId, { dmpLabel: rowLabel }]);
	}

	viewVersions(rowId: String, rowLabel: String) {
		this.router.navigate(['/plans/versions/' + rowId], { queryParams: { groupLabel: rowLabel } });
	}

	// itemClicked() {
	// 	this.onClick.emit(this.dmp);
	// }

	// grantClicked(grantId: String) {
	// 	this.router.navigate(['/grants/edit/' + grantId]);
	// }

	// datasetClicked(dmpId: string) {
	// 	this.router.navigate(['/plans/edit/' + dmpId], { queryParams: { tab: "datasetDescriptions" } });
	// }

	roleDisplay(value: any) {
		const principal: Principal = this.authentication.current();
		let role: number;
		if (principal) {
			value.forEach(element => {
				if (principal.id === element.id) {
					role = element.role;
				}
			});
		}
		if (role === 0) {
			return this.translate.instant('DMP-LISTING.OWNER');
		}
		else if (role === 1) {
			return this.translate.instant('DMP-LISTING.MEMBER');
		}
		else {
			return this.translate.instant('DMP-LISTING.OWNER');
		}
	}
}
