import CollectionConfig from './CollectionConfig';

export class SoundbitesConfig extends CollectionConfig {

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

	getFacets() {
		return [
			{
				field: 'cmd:CMD.cmd:Components.cmd:Soundbites-recording.cmd:SESSION.cmd:Content.cmd:Topic.#text.keyword',
				title: 'Topic',
				id: 'topic',
				type: 'string'
			},
			{
				field: 'cmd:CMD.cmd:Components.cmd:Soundbites-recording.cmd:GeoLocation.cmd:Country.cmd:Code.keyword',
				title: 'Country',
				id: 'country',
				type: 'string'
			},
			{
				field: 'cmd:CMD.cmd:Components.cmd:Soundbites-recording.cmd:GeoLocation.cmd:Province.keyword',
				title: 'Province',
				id: 'province',
				type: 'string'
			},
			{
				field: 'cmd:CMD.cmd:Components.cmd:Soundbites-recording.cmd:GeoLocation.cmd:Municipality.keyword',
				title: 'Municipality',
				id: 'municipality',
				type: 'string'
			}
		];
	}

	getMetadataFieldCategories() {
		return [
			{
				id : 'titles',
				label : 'Title',
				fields : [
					'cmd:CMD.cmd:Components.cmd:Soundbites-recording.cmd:SESSION.cmd:Name'
				]
			},
			{
				id : 'topics',
				label : 'Topic keywords',
				fields : [
					'cmd:CMD.cmd:Components.cmd:Soundbites-recording.cmd:SESSION.cmd:Content.cmd:Topic'
				]
			},
			{
				id : 'locations',
				label : 'Locations',
				fields : [
					'cmd:CMD.cmd:Components.cmd:Soundbites-recording.cmd:GeoLocation.cmd:City',
					'cmd:CMD.cmd:Components.cmd:Soundbites-recording.cmd:GeoLocation.cmd:Province',
					'cmd:CMD.cmd:Components.cmd:Soundbites-recording.cmd:GeoLocation.cmd:Country.cmd:Code'
				]
			}
		]
	}

	buildDescription(itemData){
		let description = "";

		if('cmd:Topic' in itemData.rawData['cmd:CMD']['cmd:Components']['cmd:Soundbites-recording']['cmd:SESSION']['cmd:Content']){
			var topics = itemData.rawData['cmd:CMD']['cmd:Components']['cmd:Soundbites-recording']['cmd:SESSION']['cmd:Content']['cmd:Topic'];
		} else {
			var topics = null;
		}

		let geolocation = itemData.rawData['cmd:CMD']['cmd:Components']['cmd:Soundbites-recording']['cmd:GeoLocation'];
		if( geolocation instanceof Array) {
			//if Array, just pick the last location to display, it should be the most specific.
			geolocation = geolocation[geolocation.length-1];
		}
		let city = geolocation['cmd:City'];
		let province = geolocation['cmd:Province'];
		let country = geolocation['cmd:Country']['cmd:Code'];

		if(itemData.date != "Unknown date"){
			description = "Opname uit "+itemData.date;
		} else {
			description = "Opname";
		}

		if (topics != null) {
			description += " over ";
			//add Topics
			if (topics instanceof Array) {
				for(let i=0; i<topics.length-1;i++){
					description += "\""+topics[i]['#text']+"\", ";
				}
				description = description.slice(0,-2) + " en \""+topics[topics.length-1]['#text']+"\"";
			} else {
				description += "\""+topics['#text']+"\"";
			}
		} else {
			description += ", onderwerp onbekend";
		}
		if(city){
			description+=", "+city;
		}
		if(province){
			description+=", "+province;
		}
		if(country){
			description+=" ("+country+")";
		}
		description+=".";
		return description;
	}

	getItemDetailData(result, currentDateField) {
		let itemData = super.getItemDetailData(result, currentDateField);

		//Additional info not captured by the super() call
		itemData.externalSourceInfo = {
				url : itemData.sourceURL,
				message : "View on Meertens Collection: Soundbites"
		}

		itemData.date = itemData.rawData['cmd:CMD']['cmd:Components']['cmd:Soundbites-recording']['cmd:CreationYear'];
		if(typeof itemData.date == 'undefined'){
			itemData.date = "Unknown date"
		}
		itemData.dateField = "cmd:CMD.cmd:Components.cmd:Soundbites-recording.cmd:CreationYear"

		itemData.description = this.buildDescription(itemData);

		let playableContent = []
		for(let i=0; i < itemData.playableContent.length; i++){
		   if(itemData.playableContent[i].mimeType.indexOf("image") === -1){
				playableContent.push(itemData.playableContent[i]);
			}
		}
		itemData.playableContent = playableContent;
		itemData.mediaTypes = ["audio"];

		return itemData;
	}
}
export default SoundbitesConfig;
