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, collectionMetadata) {
		super(clientId, user, collectionId, collectionMetadata);
	}

	getCollectionMediaTypes() {
		return ['text']
	}

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

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

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

	// Formats accepted are either [yyyy1,yyyy2] or yyyy1,yyyy2 (no square brackets)
    getFormattedDates(date) {
	    return date ? date.map(it => typeof it === 'number' ? String(it) : it) : null;
    }

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

		formattedResult.rawData = result;

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

		formattedResult.title = contentId;
		formattedResult.title +=': ' + (result.folder.folderGroupDescription == null ? '' : result.folder.folderGroupDescription);
		formattedResult.title += ' ' + (result.folder.folderPeriodOriginal == null ? '' : result.folder.folderPeriodOriginal);
		formattedResult.title += ' ' + (result.folder.subFolderDescription == null ? '' : result.folder.subFolderDescription);
		formattedResult.title += ' ' + (result.folder.folderDescription == null ? '' : result.folder.folderDescription);

		formattedResult.description = result.folder.folderGroupDescription;

		formattedResult.date = result.folder.folderGroupPeriodOriginal;
		formattedResult.dateField = 'folder.folderGroupPeriodOriginal';

		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'
			}];
			formattedResult.mediaTypes = ['text'];
		}

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

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

}

export default EYEDesmetPapersPageConfig;
