import { HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from '../../../../environments/environment';
import { RequestItem } from '../../query/request-item';
import { ExternalSourceItemModel } from '../../model/external-sources/external-source-item';
import { DataRepositoryCriteria } from '../../query/data-repository/data-repository-criteria';
import { ExternalDatasetCriteria } from '../../query/external-dataset/external-dataset-criteria';
import { RegistryCriteria } from '../../query/registry/registry-criteria';
import { ResearcherCriteria } from '../../query/researcher/researcher-criteria';
import { ServiceCriteria } from '../../query/service/service-criteria';
import { TagCriteria } from '../../query/tag/tag-criteria';
import { BaseHttpService } from '../http/base-http.service';

@Injectable()
export class ExternalSourcesService {

	private actionUrl: string;
	private headers: HttpHeaders;

	constructor(private http: BaseHttpService) {

		this.actionUrl = environment.Server + 'external/';

		this.headers = new HttpHeaders();
		this.headers = this.headers.set('Content-Type', 'application/json');
		this.headers = this.headers.set('Accept', 'application/json');
	}

	public searchDatasetRegistry(requestItem: RequestItem<RegistryCriteria>): Observable<ExternalSourceItemModel[]> {
		return this.http.get<ExternalSourceItemModel[]>(this.actionUrl + 'registries' + '?query=' + requestItem.criteria.like + '&type=' + requestItem.criteria.type, { headers: this.headers });
	}

	public searchDatasetRepository(requestItem: RequestItem<DataRepositoryCriteria>): Observable<ExternalSourceItemModel[]> {
		return this.http.get<ExternalSourceItemModel[]>(this.actionUrl + 'datarepos' + '?query=' + requestItem.criteria.like + '&type=' + requestItem.criteria.type, { headers: this.headers });
	}

	public searchDatasetService(requestItem: RequestItem<ServiceCriteria>): Observable<ExternalSourceItemModel[]> {
		return this.http.get<ExternalSourceItemModel[]>(this.actionUrl + 'services' + '?query=' + requestItem.criteria.like + '&type=' + requestItem.criteria.type, { headers: this.headers });
	}

	public searchDatasetTags(requestItem: RequestItem<TagCriteria>): Observable<ExternalSourceItemModel[]> {
		// return Observable.of([
		// 	{ id: '1', name: 'Tag 1', description: '' },
		// 	{ id: '2', name: 'Tag 2', description: '' },
		// 	{ id: '3', name: 'Tag 3', description: '' },
		// 	{ id: '4', name: 'Tag 4', description: '' },
		// 	{ id: '5', name: 'Tag 5', description: '' },
		// 	{ id: '6', name: 'Tag 6', description: '' },
		// 	{ id: '7', name: 'Tag 7', description: '' },
		// 	{ id: '8', name: 'Tag 8', description: '' },
		// ]);
		return this.http.get<ExternalSourceItemModel[]>(this.actionUrl + "tags" + "?query=" + requestItem.criteria.like + "&type=" + requestItem.criteria.type, { headers: this.headers });
	}

	public searchDatasetSExternalDatasetservice(requestItem: RequestItem<ExternalDatasetCriteria>): Observable<ExternalSourceItemModel[]> {
		return this.http.get<ExternalSourceItemModel[]>(this.actionUrl + 'datasets' + '?query=' + requestItem.criteria.like + '&type=' + requestItem.criteria.type, { headers: this.headers });
	}

	public searchDMPResearchers(requestItem: RequestItem<ResearcherCriteria>): Observable<ExternalSourceItemModel[]> {
		return this.http.post<ExternalSourceItemModel[]>(environment.Server + 'researchers/getWithExternal', requestItem, { headers: this.headers });
	}

	public searchDMPOrganizations(like: string): Observable<ExternalSourceItemModel[]> {
		return this.http.get<ExternalSourceItemModel[]>(this.actionUrl + 'organisations' + '?query=' + like, { headers: this.headers });
	}

	// TODO: currently not used.
	public searchDMPProfiles(like: string): Observable<ExternalSourceItemModel[]> {
		return this.http.get<ExternalSourceItemModel[]>(this.actionUrl + 'datasetprofiles/get' + '?query=' + like, { headers: this.headers });
	}

}
