import CollectionConfig from './CollectionConfig';
import CollectionUtil from '../../util/CollectionUtil';

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

	getCollectionMediaTypes() {
		return ['image', 'text'];
	}

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

	getFacets() {
		return [
			{
				field: '@graph.dc:subject.keyword',
				title : 'Soort bericht',
				id : 'subject',
				type : 'string'
			},
			{
				field: '@graph.dc:date',
				title : 'Date (krant)',
				id : 'date',
				type : 'date_histogram'
			},
			{
				field: '@graph.@type.keyword',
				title : 'Krant / Artikel / Media object',
				id : 'result_type',
				type : 'string'
			},
			{
				field: '@graph.dc:title.keyword',
				title : 'Krantentitel',
				id : 'title',
				type : 'string'
			},
			{
				field: '@graph.dcterms:accessRights.keyword',
				title : 'Toegangsrechten (krant)',
				id : 'accessRights',
				type : 'string'
			},
			{
				field: '@graph.dcterms:spatial.keyword',
				title : 'Plaats van uitgave',
				id : 'publishing_place',
				type : 'string'
			}
		];
	}

	//TODO if available add the poster URL:
	//http://hugodrax.beeldengeluid.nl:84/fcgi-bin/iipsrv.fcgi?IIIF=BG0261104_0081.tif/108,1876,514,29/full/0/default.jpg
	getItemDetailData(result, currentDateField) {
		//first flatten the pure ES response
		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['@graph']['@type'];

		formattedResult.rawData = result;

		//console.debug(result);
		//taken from the parent doc
		if(result['@graph']['@type'] == "schema:Newspaper") {
			formattedResult.title = result['@graph']['dc:title'];
			formattedResult.description = result['@graph']['dcterms:alternative'];
			formattedResult.date = result['@graph']['dc:date'];
			formattedResult.dateField = '@graph.dc:date';
		} else if(result['@graph']['@type'] == "schema:Article") {
			formattedResult.title = result['@graph']['dc:title'];
			//'Text unavailable because of copyright restrictions'
			formattedResult.description = CollectionUtil.highlightSearchTermInDescription(
				result['@graph']['schema:articleBody'],
				null,
				35
			)
			const content = this.__getPlayableContent(result);
			if(content) {
				formattedResult.playableContent = content.playableContent;
				formattedResult.mediaTypes = content.mediaTypes;
				if(content.playableContent.length > 0) {
					formattedResult.externalSourceInfo = {
						url : content.playableContent[0].url,
						message : null
					}
				}
			}

			delete formattedResult.rawData['@graph']['schema:articleBody']
		} else if(result['@graph']['@type'] == "schema:MediaObject") {
			formattedResult.title = 'media object (' + formattedResult.resourceId + ')';
			if(result['@graph']['schema:contentUrl']) {
				formattedResult.playableContent = [{
					url : result['@graph']['schema:contentUrl'],
					mimeType : 'application/javascript',
					assetId : result['@graph']['schema:contentUrl']
				}]
				formattedResult.mediaTypes = ['image'];
			}
		}
		return formattedResult;
	}

	//TODO this should only be fetched on the item detail page!
	__getPlayableContent(result) {
		let playableContent = [];
		let dcId = result['@graph']['dc:identifier']
		let url = null;
		if(dcId) {
			if(typeof(dcId) == 'string') {
				url = dcId
			} else {
				url = dcId.join('')
			}
			playableContent.push({
				url : url,
				mimeType : 'application/javascript',
				assetId : url
			})
			return {
				playableContent : playableContent,
				mediaTypes : ['image']
			}
		}

		return null;
	}

	toPrettyFieldName(esFieldName) {
		if(esFieldName) {
			//first split the field based on a dot
			const tmp = esFieldName.split('.');

			//if the last field is called raw or keyword (ES reserved names), drop it
			if(tmp[tmp.length -1] == 'raw' || tmp[tmp.length -1] == 'keyword') {
				tmp.pop();
			}
			//take the leaf field and make it the first in the pretty name
			let fn = tmp[tmp.length-1];

			//remove any prefix particle separated by ':'
			if(fn.indexOf(':') != -1) {
				fn = fn.replace(':', '_')
			}

			//add between brackets the parent of the leaf field
			if(tmp.length > 1) {
			 	fn += ' (in: ' + tmp[tmp.length-2] + ')';
			}
			return fn
		}
		return esFieldName;
	}

}

export default KBNewspaperConfig;