import CollectionConfig from './CollectionConfig';

export class KBNewspaperAggrConfig extends CollectionConfig {
	constructor(clientId, user, collectionId, collectionMetadata) {
		super(clientId, user, collectionId, collectionMetadata);
	}

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

	getPreferredDateField() {
		return 'paperDate';
	}

	getMatchingTermsMsg(numHits, forSnippet) {
		if(forSnippet) {
			return numHits !== 0 ? numHits + " match(es) in archival metadata |" : ' No matches in the archival metadata, matching terms found in the OCR';
		} else {
        	return numHits <= 0 ? 'No matching terms found in archival metadata (see OCR in KB Delpher)' : 'Matching terms in archival metadata';
        }
    }

	getMetadataFieldCategories() {
		return [
			{
				id : 'articleTitles',
				label : 'article title',
				fields : [
					'articleTitle'
				]
			},
			{
				id : 'paperTitles',
				label : 'newspaper title',
				fields : [
					'paperTitle'
				]
			},
			{
				id : 'ocr',
				label : 'article text (OCR)',
				enrichment : true,
				fields : [
					'articleBody'
				]
			},
			{
				id : 'publication',
				label : 'place of publication',
				fields : [
					'placeOfPublication'
				]
			},
			{
				id : 'origin',
				label : 'origin (herkomst)',
				fields : [
					'origin'
				]
			}
		]
	}

	getFacets() {
		return [
			{
				field: 'century',
				title : 'Periode',
				id : 'periode',
				type : 'string'
			},
			{
				field: 'articleType',
				title : 'Soort bericht',
				id : 'soort',
				type : 'string'
			},
			{
				field: 'distributionArea.keyword',
				title : 'Verspreidingsgebied',
				id : 'verspreidingsgebied',
				type : 'string'
			}
		];
	}

	getKeywordFields(){
        //ArticleBody
        //ArticleTitle
        //Is part of
        //paperPDFUrl
        //publisher
        var filteredKeywordFields = [];
        for(var i = 0; i < this.keywordFields.length; i++){
            if(this.keywordFields[i] !== "articleBody.keyword" &&
               this.keywordFields[i] !== "articleTitle.keyword" &&
               this.keywordFields[i] !== "isPartOf" &&
               this.keywordFields[i] !== "paperPDFUrl" &&
               this.keywordFields[i] !== "publisher.keyword") {
                filteredKeywordFields.push(this.keywordFields[i]);
            }
        }
	    return filteredKeywordFields;
	}

	//in case the field name cannot be found in the field descriptions
	formatIndexFieldName(esFieldName) {
		return esFieldName
	}

	//TODO if available add the poster URL:
	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 = "Article";
		formattedResult.collectionId = result.collectionId;

		formattedResult.rawData = result;
		delete formattedResult.rawData['articleBody'];
		delete formattedResult.rawData['paperPDFUrl'];

		//taken from the parent doc
		formattedResult.title = result['articleTitle'];
		formattedResult.description = "Krantentitel: " + result['paperTitle'];
		formattedResult.date = result['paperDate'];
		formattedResult.dateField = 'paperDate';
		formattedResult.specialProperties = this.__extractSpecialProperties(result);
		formattedResult.externalSourceInfo = this.__getExternalSourceInfo(result);

		if(result['sourceUrl']) {
			formattedResult.playableContent = [{
				url : result['sourceUrl'],
				mimeType : 'application/javascript',
				assetId : result['sourceUrl']
			}]
		}

		return formattedResult;
	}

	getResultSnippetData(result, numHighlights) {
	    var snippet = super.getResultSnippetData(result, numHighlights);
	    snippet['playable']= false;
	    return snippet;
	}

	__getExternalSourceInfo(result) {
		if(result['sourceUrl']) {
			const source = result['sourceUrl'];
			return {
				url : source,
				message : "View in Delpher"
			}
		} else {
			return null;
		}

	}

	__extractSpecialProperties(result) {
		const specialProperties = {};
		specialProperties['Article type'] = result['articleType'];
		specialProperties['Frequency'] = result['frequency'];
		specialProperties['Publisher'] = result['publisher'];
		specialProperties['Place of publication'] = result['placeOfPublication'];
		specialProperties['Distribution area'] = result['distributionArea'];
		specialProperties['Origin'] = result['origin'];
		return specialProperties;
	}

	getFieldsToExclude() {
		return ['articleBody'];
	}

}

export default KBNewspaperAggrConfig;
