import CollectionConfig from './CollectionConfig';

export class EYEDesmetAfficheConfig extends CollectionConfig {

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

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

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

    // Based on the Jaar field, the following variations are found
    // 1) yyyy
    // 2) yyyy weirdString (e.g. 1914 ([?]))
    // 3) yyyy - yyyy (e.g. the period of "1916 - 1920")
    // 4) yyyy - yyyy weird String (e.g. "1914  ([?])")
    getFormattedDates(date) {
        if (date) {
        	if(typeof(date) === 'string') {
	     		if(date.indexOf('-') === -1) { //variant 1 or 2
	     			return [date.trim().substring(0, 4)];
	     		} else { //variant 3 or 4
	     			const tmp = date.split(' ');
	     			return [tmp[0] + ' - ' + tmp[2]]
	     		}
	     	} else if(typeof(date) === 'object') { //array
	     		console.debug('this should never happen');
	     	}
        }
        return null;
    }

	getFacets() {
		return [
			{
				field: 'Land.keyword',
				title : 'Country',
				id : 'country',
				type : 'string'
			},
			{
				field: 'Drukker.keyword',
				title : 'Printer',
				id : 'printer',
				type : 'string'
			},
			{
				field: 'Beschadiging.keyword',
				title : 'Damage',
				id : 'damage',
				type : 'string'
			}
		];
	}

	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;

		formattedResult.title = this.__getTitle(result);
		formattedResult.description = this.__getDescription(result);
		formattedResult.date = result['Jaar'];
		formattedResult.dateField = 'Jaar';

		const content = this.__extractPlayableContent(result);
		if(content) {
			formattedResult.playableContent = content.playableContent;
			formattedResult.mediaTypes = content.mediaTypes;
		}
		formattedResult.externalSourceInfo = {
			url : null,
			message : "View in Eye's catalog: No direct link, use the \"ID\" to search in the EYE collectie catalogus: https://www.eyefilm.nl/eye-catalogus-zoek-in-de-collectie"
		};

		return formattedResult;
	}

	__getTitle(result) {
		if(result['Affichetitel']) {
			return result['Affichetitel'];
		}
		return 'Unknown'
	}

	__getDescription(result) {
		let desc = null;
		if(result['Subtitels']) {
			desc = result['Subtitels'];
		}
		if(result['Filmwerk']) {
			if(desc) {
				desc += ' (from the film: ' + result['Filmwerk'] + ')';
			} else {
				desc = 'From the film: ' + result['Filmwerk'];
			}
		}
		return desc ? desc : 'Unknown'
	}

	__extractPlayableContent(result) {
		if(result['ShownBy'] && result['ShownBy'] !== '') {
			return {
				playableContent : [{
					url : result['ShownBy'],
					mimeType : 'image/jpeg',
					assetId : result._id,
					cors : false //prevents the use of OpenSeaDragon, which cannot load images without CORS
				}],
				mediaTypes : ['image']
			}
		}
	}

	//in case the field name cannot be found in the field descriptions
	formatIndexFieldName(esFieldName) {
        return esFieldName.replace(".keyword", "");
	}

}

export default EYEDesmetAfficheConfig;
