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'];
	}

	getCollectionIndices() {
		return ['rvd__ocr']
	}

	//we want to search based on this granularity
	getFragmentPath() {
		return 'pages.blocks.lines.*';
	}

	//this is the text field you can search on the line level
	getFragmentTextFields() {
		return ['pages.blocks.lines.line'];
	}

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

	getFacets() {
		return [
			{
				field: 'title.keyword',
				title : 'Filmtitel',
				id : 'title',
				type : 'string'
			},
			{
				field: 'contractType',
				title : 'Contract type',
				id : 'contractType',
				type : 'string'
			},
			{
				field: 'producer.keyword',
				title : 'Producent/maker',
				id : 'producer',
				type : 'string'
			},
			{
				field: 'rightsOwner.keyword',
				title : 'Rechthebbende',
				id : 'rightsOwner',
				type : 'string'
			},
			{
				field: 'rightsOwnerOther.keyword',
				title : 'Rechthebbende (extra)',
				id : 'rightsOwnerOther',
				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 = 'rvd';//change this in the index!
		formattedResult.index = 'rvd';
		formattedResult.docType = result._type;

		formattedResult.rawData = result;

		formattedResult.title = this.__getTitle(result);
		formattedResult.date = result.date;
		formattedResult.dateField = result.dateField;
		formattedResult.description = result.line;
		formattedResult.posterURL = result.imageUrl;
		formattedResult.playableContent = result.playableContent;

		//this url is used to determine the fragment target on the detail page
		if(result._type == 'media_fragment') {
			formattedResult.mediaFragment = {
				url : this.__toFragmentUrl(result.imageUrl),
				x : result.coords.l,
				y : result.coords.t,
				w : result.coords.r - result.coords.l,
				h : result.coords.b - result.coords.t
			}
		}
		return formattedResult;
	}

	__getTitle(result) {
		let pageNumber = 1;
		if(result.imageUrl) {
			let i = result.imageUrl.indexOf('page-');
			if(i != -1) {
				i += 'page-'.length;
				pageNumber = parseInt(result.imageUrl.substring(i, i + 1)) + 1;
			}
		}
		let contractType = 'Onbekend type'
		switch(result.contractType) {
			case 'PO' : contractType = 'Producent overdracht';break;
			case 'AO' : contractType = 'Auteursrecht overdracht';break;
			case 'AB' : contractType = 'Auteursrecht bemiddeling?';break;
		}
		return result.title + ' [' + contractType + ' pagina ' + pageNumber + ']'
	}

	__toFragmentUrl(url) {
		const temp = url.split('/');
		temp[temp.length - 4] = 'full';
		return temp.join('/');
	}

}

export default RVDContractsConfig;