import CollectionConfig from './CollectionConfig';

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

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

	getMetadataFieldCategories() {
		return [
			{
				'id' : 'ocr_text',
				'label' : 'OCR tekst',
				'fields' : [
					'aoContracts.pages.blocks.lines.line',
					'poContracts.pages.blocks.lines.line',
					'abContracts.pages.blocks.lines.line'
				]
			}
		]
	}

	getFacets() {
		return [
			{
				field: 'title.keyword',
				title : 'Filmtitel',
				id : 'title',
				type : 'string'
			},
			{
				field: 'producer.keyword',
				title : 'Producent/maker',
				id : 'producer',
				type : 'string'
			},
			{
				field: 'personX.keyword',
				title : 'Donateur',
				id : 'donor',
				type : 'string'
			},
			{
				field: 'organizationY.keyword',
				title : 'Organisatie',
				id : 'organization',
				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.collectionId = result._index;//change this in the index!
		formattedResult.index = result._index;
		formattedResult.docType = result._type;

		formattedResult.rawData = this.__formatRawData(result); //hide it for performance reasons, the OCR is a lot of data

		formattedResult.title = result.title;
		formattedResult.date = result.dateRaw;
		formattedResult.description = result.contractInfo

		formattedResult.playableContent = this.__getPlayableContent(result);

		return formattedResult;
	}

	__formatRawData(data) {
		if(data.contracts) {
			data.contracts.forEach(c => {
				if(c.pages) {
					delete c.pages
				}
			})
		}
		return data;
	}

	__getPlayableContent(result) {
		let pc = []
		if(result.contracts) {
			pc = this.__addPlayableContent(pc, result.contracts);
		}
		return pc.length > 0 ? pc : null;
	}

	__addPlayableContent(pc, contracts) {
		contracts.forEach(c => {
			pc = pc.concat(c.playableContent);
		})
		return pc;
	}

}

export default RVDContractsConfig;