import CollectionConfig from './CollectionConfig';

export class RVDContractsConfig extends CollectionConfig {

	constructor(clientId, user, collectionId, collectionMetadata) {
		super(clientId, user, collectionId, collectionMetadata);
	}

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

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

	getFieldsToExclude() {
		return ['contracts.pages']
	}

	requiresPlayoutAccess() {
		return true;
	}

	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.collectionId = result.collectionId;

		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.map(pc => {
					const contentId = 'rvd_contracts/' + pc.assetId.replace('.tif', '');
					const safeContentId = this.__getContentId(contentId);
					return {
						url :  _play +
							'/api/play/beng-image/' +
							safeContentId +
							'/info.json',
						mimeType : 'image/jpeg',
						assetId : contentId, //is this a good asset ID?
						contentId : safeContentId,
						contentServerId : 'beng-image'
					}
				})
			);
		})
		return pc;
	}

	__getContentId(contentId) {
		console.debug(contentId)
		let i = contentId.length-1;
		let cid = '';
		while(i >= 0) {
			cid += String.fromCharCode(contentId.charCodeAt(i) + 1);
			i--;
		}
		return cid;
	}

}

export default RVDContractsConfig;