import CollectionConfig from './CollectionConfig';

export class DANSOralHistoryConfig extends CollectionConfig {

	constructor(clientId, user, collectionId, stats, info) {
		super(clientId, user, collectionId, stats, info);
	}

	getCollectionMediaTypes() {
		return ['video'];
	}

	getMetadataFieldCategories() {
		return [
			{
				'id' : 'titles',
				'label' : 'Search in: titles',
				'fields' : [
					'@graph.dcterms:title'
				]
			},
			{
				'id' : 'descriptions',
				'label' : 'Search in: descriptions',
				'fields' : [
					'@graph.dcterms:abstract'
				]
			},
			{
				'id' : 'subject metadata',
				'label' : 'Search in: subject metadata',
				'fields' : [
					'@graph.dcterms:subject'
				]
			}
		]
	}

	getFacets() {
		return [
			{
				field: '@graph.dcterms:isPartOf.keyword',
				title: 'Thematic collection',
				id: 'thematic_collection',
				type: 'string'
			},
			{
				field: '@graph.dcterms:creator.keyword',
				title : 'Creator',
				id : 'creator',
				type : 'string'
			},
			{
				field: '@graph.dcterms:subject.keyword',
				title : 'Subject',
				id : 'subject',
				type : 'string'
			},
			{
				field: '@graph.dcterms:accessRights.keyword',
				title : 'Access rights',
				id : 'access_rights',
				type : 'string'
			},
			{
				field: '@graph.dcterms:format.keyword',
				title : 'Media format',
				id : 'media_format',
				type : 'string'
			}
		];
	}

	getItemDetailData(result, currentDateField) {
		result = this.formatSearchResult(result);
		const formattedResult = {}

		//then add the most basic top level data
		formattedResult.resourceId = result._id;
		formattedResult.index = result._index;
		formattedResult.docType = result._type;

		formattedResult.rawData = result;

		if(result['@graph']) {
			formattedResult.title = this.__getTitle(result);
			formattedResult.description = result['@graph']['dcterms:abstract'];
			formattedResult.date = this.__getDate(result);
			formattedResult.dateField = '@graph.dcterms:created';
		}
		formattedResult.externalSourceInfo = this.__getExternalSourceInfo(result);
		if(result.playoutUrl) {
			formattedResult.playableContent = [
				{
					url : result.playoutUrl,
					mimeType : 'video/mp4',
					assetId : result._id
				}
			]
			if(result['@graph']['dcterms:accessRights'] && result['@graph']['dcterms:accessRights'].length > 0 &&
				result['@graph']['dcterms:accessRights'].indexOf('OPEN_ACCESS') != -1) {
				formattedResult.mediaTypes = ['video']
			}
		}

		return formattedResult;
	}

	__getTitle(result) {
		let title = result['@graph']['dcterms:title'];
		if(title) {
			if(typeof(title) == 'string') {
				return result['@graph']['dcterms:title'];
			} else {
				return result['@graph']['dcterms:title'].join('; ');
			}
		}
		return null;
	}

	__getDate(result) {
		let dc = result['@graph']['dcterms:created'];
		if(dc) {
			if(typeof(dc) == 'string') {
				return result['@graph']['dcterms:created'];
			} else {
				return result['@graph']['dcterms:created'].join('; ');
			}
		}
		return null;
	}

	__getExternalSourceInfo(result) {
		let source = 'https://easy.dans.knaw.nl/ui/rest/datasets/easy-dataset';
		if(result._id && result._id.lastIndexOf(':') != -1) {
			return {
				url : source + result._id.substring(result._id.lastIndexOf(':')),
				message : null
			}
		}
		return null;
	}

}

export default DANSOralHistoryConfig;