import CollectionConfig from './CollectionConfig';

export class KijkEnLuisterCijfersConfig extends CollectionConfig {

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

	getFragmentPath() {
		return 'pages.blocks';
	}

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

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

	getMetadataFieldCategories() {
		return [
			{
				id : 'ocr',
				label : 'OCR',
				nestedPath : 'pages',
				fields : [
					'pages.words'
				]
			}
		]
	}

	getFieldsToExclude() {
		return null;
	}

	getFacets() {
		return [
			{
				field: 'pages.code',
				title: 'Code',
				id: 'code',
				type : 'nested'
			},
			{
				field: 'pages.onderdeel',
				title: 'Onderdeel',
				id: 'onderdeel',
				type : 'nested'
			},
			{
				field: 'pages.paginaSoort',
				title: 'Paginasoort',
				id: 'paginaSoort',
				type : 'nested'
			},
			{
				field: 'pages.onderzoekstype',
				title: 'Onderzoekstype',
				id: 'onderzoekstype',
				type : 'nested'
			}
		]
	}

	requiresPlayoutAccess() {
		return true;
	}

	getItemDetailData(result, currentDateField) {
		result = this.formatSearchResult(result);
		const formattedResult = {}

		console.debug(result);
		//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 = formattedResult['resourceId']
		formattedResult.description = 'not yet'
		formattedResult.date = '19-10-2018'
		formattedResult.dateField = 'date';


		//for search snippet
		if(result._type == 'media_fragment') {
			formattedResult.posterURL = this.__getPosterURL(formattedResult);
			formattedResult.mediaFragment = this.__toMediaFragment(formattedResult);
		} else { //resource viewer
			const content = this.__extractPlayableContent(result);
			if(content) {
				formattedResult.playableContent = content.playableContent;
				formattedResult.mediaTypes = content.mediaTypes;

			}
		}

		return formattedResult;
	}

	__extractPlayableContent(result) {
		let playableContent = []
		const mediaTypes = ['image']
		if(result.pages) {
			playableContent = result.pages.map(page => {
				let contentId = this.__getContentId(page.assetId)
				return {
					url :  _play +
						'/api/play/beng-image/' +
						contentId +
						'/info.json',
					mimeType : 'image/jpeg',
					assetId : page.assetId, //is this a good asset ID?
					contentId : contentId,
					contentServerId : 'beng-image'
				}
			})
		}
		return {
			playableContent : playableContent,
			mediaTypes : mediaTypes
		}
	}

	__getPosterURL(result) {
		console.debug('get poster URL', result);
		const contentId = this.__getContentId(result.rawData.assetId)
		if(contentId) {
			let imgUrl = _play + '/api/play/beng-image/' + contentId + '/';
			imgUrl += result.rawData.x + ',';
			imgUrl += result.rawData.y + ',';
			imgUrl += result.rawData.w + ',';
			imgUrl += result.rawData.h;
			imgUrl += '/pct:50/0/default.jpg';
			return imgUrl;
		}
		return null
	}

	__toMediaFragment(result) {
		return {
			assetId : result.rawData.assetId,
			x : result.rawData.x,
			y : result.rawData.y,
			w : result.rawData.w,
			h : result.rawData.h
		}
	}

	__getContentId(assetId) {
		const parts = ['kijkenluistercijfers', assetId]
		let contentId = parts.map(p => this.__getContentPartId(p)).join('___');
		return contentId.replace('/', '|||')
	}

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



}

export default KijkEnLuisterCijfersConfig;