import { Component, OnInit, Input } from '@angular/core';
import { DatasetService } from '../../../core/services/dataset/dataset.service';
import { DataTableRequest } from '../../../core/model/data-table/data-table-request';
import { DatasetCriteria } from '../../../core/query/dataset/dataset-criteria';
import { DatasetListingModel } from '../../../core/model/dataset/dataset-listing';
import { AuthService } from '../../../core/services/auth/auth.service';
import { RecentActivityType } from '../../../core/common/enum/recent-activity-type';
import { Router} from '@angular/router';
import { DmpStatus } from '../../../core/common/enum/dmp-status';

@Component({
	selector: 'app-drafts',
	templateUrl: './drafts.component.html',
	styleUrls: ['./drafts.component.css']
})
export class DraftsComponent implements OnInit {
	datasetDrafts: DatasetListingModel[];
	datasetDraftsTypeEnum = RecentActivityType;
	@Input() routerLink: string;
	status: number;

	constructor(
		private router: Router,
		private datasetService: DatasetService,
		private authentication: AuthService
	) { }

	ngOnInit() {
		const fields: Array<string> = [];
		fields.push('-modified');
		const dmpDataTableRequest: DataTableRequest<DatasetCriteria> = new DataTableRequest(0, 2, { fields: fields });
		dmpDataTableRequest.criteria = new DatasetCriteria();
		dmpDataTableRequest.criteria.status = DmpStatus.Draft;
		this.datasetService.getPaged(dmpDataTableRequest).subscribe(response => {
			this.datasetDrafts = response.data;
		});
	}

	redirect(id: string, type: RecentActivityType) {
		switch (type) {
			case RecentActivityType.Grant: {
				this.router.navigate(["grants/edit/" + id]);
				return;
			}
			case RecentActivityType.Dataset: {
				this.router.navigate(["datasets/edit/" + id]);
				return;
			}
			case RecentActivityType.Dmp: {
				this.router.navigate(["plans/edit/" + id]);
				return;
			}
			default:
				throw new Error("Unsupported Activity Type ");
		}
	}

	public isAuthenticated(): boolean {
		return !!this.authentication.current();
	}

	navigateToUrl() {
		if (!this.isAuthenticated()) { return; }
		this.router.navigate(['/datasets'], { queryParams: { status: 0 } });
	}
}
