import CollectionConfig from './CollectionConfig';

export class EYEConfig extends CollectionConfig {

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

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

	getMetadataFieldCategories() {
		return [
			{
				'id' : 'titles',
				'label' : 'Titles',
				'fields' : [
					'dc:title',
					'dcterms:alternative'
				]
			}
		]
	}

	getFacets() {
		return [
			{
				field: 'dc:creator.keyword',
				title : 'Creator',
				id : 'creator',
				type : 'string'
			},
			{
				field: 'dcterms:subject.@value.keyword',
				title : 'Subject',
				id : 'subject',
				type : 'string'
			},
			{
				field: 'dc:coverage.keyword',
				title : 'Location',
				id : 'coverage',
				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(currentDateField && result[currentDateField]) {
			formattedResult.date = result[currentDateField];
		} else if(result.hasOwnProperty('dc:date')) {
			formattedResult.date = result['dc:date'];
			formattedResult.dateField = 'dc:date';
		}
		if(result.hasOwnProperty('dcterms:title')) {
			if(result['dcterms:title'].hasOwnProperty('@value')) {
				formattedResult.title = result['dcterms:title']['@value'];
			}
		}
		if(result.hasOwnProperty('dcterms:abstract')) {
			const descs = [];
			const altDescs = [];
			const abs = result['dcterms:abstract'];
			//in case the value is a {}
			if(abs.hasOwnProperty('@language') && abs.hasOwnProperty('@value')) {
				if(abs['@language'] == 'NL' || abs['@language'] == 'EN') {
					descs.push(abs['@value'])
				} else {
					altDescs.push(abs['@value'])
				}
			} else if(abs.length > 0) {//otherwise it's a list
				result['dcterms:abstract'].forEach((d) => {
					if(d.hasOwnProperty('@language') && d.hasOwnProperty('@value')) {
						if(d['@language'] == 'NL' || d['@language'] == 'EN') {
							descs.push(d['@value'])
						} else {
							altDescs.push(d['@value'])
						}
					}
				});
			}
			//finally set the description, sheesh
			formattedResult.description = descs.join('\n\n');
			if(result.description == '') {
				formattedResult.description = altDescs.join('\n\n');
			}
		}
		if(result.hasOwnProperty('dcterms:hasVersion')) {
			let url = result['dcterms:hasVersion'];
			const mimeType = url.indexOf('youtu') == -1 ? 'application/x-shockwave-flash' : 'video/mp4';
			if(url.indexOf('youtu.be') != -1) {
				url = 'https://www.youtube.com/watch?v=' + url.substring(url.lastIndexOf('/'));
			}
			formattedResult.playableContent = [{
				url : url,
				mimeType : mimeType,
				assetId : result['_id']
			}];
			formattedResult.mediaTypes = ['video'];
		}
		return formattedResult;
	}

}

export default EYEConfig;