import CollectionConfig from './CollectionConfig';

//vips im_vips2tiff $OUTPUT_DIR/$i $CONVERTED_OUTPUT_DIR/$i:jpeg:75,tile:256x256,pyramid

export class EYEDesmetPapersPageConfig extends CollectionConfig {

	constructor(clientId, user, collectionId, stats, info) {
		super(clientId, user, collectionId, stats, info);
	}

	getCollectionMediaTypes() {
		return ['image']
	}

	getMetadataFieldCategories() {
		return [
			{
				'id' : 'ocr',
				'label' : 'OCR',
				'fields' : [
					'layer__ocr.text'
				]
			}
		]
	}

	getFacets() {
		return [
			{
				field: 'folder.folderId.keyword',
				title : 'Folder ID',
				id : 'folder_id',
				type : 'string'
			},
			{
				field: 'folder.groupCoverPeriodRaw.keyword',
				title : 'Group cover period',
				id : 'group_cover_period',
				type : 'string'
			},
			{
				field: 'folder.chapterId',
				title : 'Chapter ID',
				id : 'chapter_id',
				type : 'string'
			}
		];
	}

	requiresPlayoutAccess() {
		return true;
	}

	getItemDetailData(result, currentDateField) {
		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._type;

		formattedResult.rawData = result;

		const contentId = result.contentId.replace('.tif', '');

		formattedResult.title = contentId;
		formattedResult.title += ': ' + result.folder.groupCoverDescription;
		formattedResult.title += ' ' + result.folder.coverPeriodRaw;
		formattedResult.title += ' ' + result.folder.subCoverDescription;
		formattedResult.title += ' ' + result.folder.coverDescription;

		formattedResult.description = result.folder.groupCoverDescription;

		formattedResult.date = result.groupCoverPeriodRaw;
		formattedResult.dateField = 'groupCoverPeriodRaw';

		const safeContentId = this.__getContentId(contentId)
		if(safeContentId) {

			//formattedResult.posterURL = this.__getPosterURL(result, safeContentId);

			formattedResult.playableContent = [{
				url :  _play +
					'/api/play/eye-image/' +
					safeContentId +
					'/info.json',
				mimeType : 'image/jpeg',
				assetId : contentId, //is this a good asset ID?
				contentId : safeContentId,
				contentServerId : 'eye-image'
			}]
		}

		if(formattedResult.rawData.layer__ocr) {
			delete formattedResult.rawData.layer__ocr;
		}

		return formattedResult;
	}

	__getPosterURL(result, safeContentId) {
		if(result.layer__ocr && result.layer__ocr.length > 0) {
			const coords = result.layer__ocr[0].coords;
			if(coords) {
				let imgUrl = _play + '/api/play/eye-image/' + safeContentId;
				imgUrl += '/50,150,200,50';
				// imgUrl += '/' + coords.l;
				// imgUrl += ',' + coords.t;
				// imgUrl += ',' + (coords.r - coords.l);
				// imgUrl += ',' + (coords.b - coords.t);
				imgUrl += '/pct:50/0/default.jpg';
				return imgUrl;
			}
		}
		return null
	}

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


}

export default EYEDesmetPapersPageConfig;