import CollectionConfig from './CollectionConfig';

class OpenBeeldenNISVConfig extends CollectionConfig {
	constructor(clientId, user, collectionId, stats, info) {
		super(clientId, user, collectionId, stats, info);
	}

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

	getPreferredLanguage() {
		return 'nl'
	}

	getPreferredDateField() {
		return '@graph.dcterms:date';
	}

	getFacets() {
		return [
			{
				field: '@graph.dcterms:subject.@value.keyword',
				title : 'Subject',
				id : 'subject',
				type : 'string'
			},
			{
				field: '@graph.dcterms:date',
				title : 'Date',
				id : 'date',
				type : 'date_histogram'
			},
			{
				field: '@graph.dcterms:spatial.@value.keyword',
				title : 'Spatial',
				id : 'spatial',
				type : 'string'
			},
			{
				field: '@graph.dcterms:language',
				title : 'Language',
				id : 'language',
				type : 'string'
			},
			{
				field: '@graph.dcterms:publisher',
				title : 'Publisher',
				id : 'publisher',
				type : 'string'
			}
		];
	}

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

	//currentDateField is always the dcterms:date field, so no need to check for it
	getItemDetailData(result, currentDateField) {
		result = this.formatSearchResult(result);
		const formattedResult = {}
		formattedResult.resourceId = result._id;
		formattedResult.index = result._index;
		formattedResult.docType = result._type;
		formattedResult.title = this.__extractFieldValue(result['@graph']['dcterms:title']);
		formattedResult.description = this.__extractFieldValue(result['@graph']['dcterms:description']);
		formattedResult.date = result['@graph']['dcterms:date'];
		formattedResult.dateField = '@graph.dcterms:date';
		formattedResult.externalSourceInfo = {
			url : result['@graph']['cc:attributionURL'],
			message : null
		}
		formattedResult.specialProperties = this.__extractSpecialProperties(result);

		const content = this.__extractPlayableContent(result);
		formattedResult.playableContent = content.playableContent;
		formattedResult.mediaTypes = content.mediaTypes;
		formattedResult.rawData = result;
		// placeholderImage
		const formats = result['@graph']['dcterms:hasFormat'].filter((f)=>(f.toLowerCase().endsWith('.png') || f.toLowerCase().endsWith('.jpg')))
		formattedResult.placeholderImage = formats.length ? formats[0] : null;
		return formattedResult;
	}

	//extracts the value of a field in the preferred language (if possible)
	__extractFieldValue(field) {
		let lang = this.getPreferredLanguage();
		//if there is one object, just get that one, regardless of language
		if(field.hasOwnProperty('@value')) {
			return field['@value'];
		}
		if(field.length > 0) {
			let titles = field.filter((t) => {
				return t['@language'] == lang;
			})
			if(titles.length > 0) {
				return titles[0]['@value']
			} else {
				//otherwise just get the first one (if the preferred language was not found)
				return field[0]['@value']
			}
		}
		return null;
	}

	__extractPlayableContent(result) {
		let playableContent = null;
		let mediaTypes = null;
		if (result['@graph'].hasOwnProperty('dcterms:hasFormat')) {
			let formats = result['@graph']['dcterms:hasFormat'] || [];
			if(typeof(formats) == 'string') {
				formats = [formats];
			}
			//get the mp4s only
			let mp4s = formats.filter((f) => {
				return f.indexOf('.mp4') != -1;
			});
			if(mp4s.length > 0) {
				//sort the list by string length
				mp4s.sort((a,b)=> {
					b.length > a.length;
				})
				//grab the one with the shortest filename: it usually is the source video
				playableContent = [{
					url : mp4s[0],
					mimeType : 'video/mp4',
					assetId : result._id
				}];
				mediaTypes = ['video'];
			}
		}
		return {
			playableContent : playableContent,
			mediaTypes : mediaTypes
		}
	}

	//these are the properties that will be shown in the regular item details table
	__extractSpecialProperties(result) {
		const specialProperties = {};
		//TODO
		return specialProperties;
	}

}

export default OpenBeeldenNISVConfig;