import { ActionAPI } from '../../../ActionAPI';
import { EditorAPI, EditorAPI_Field } from '../../../EditorAPI';
import { Getiyo } from '../getiyo';
import {
	getiyoSimpleExecute,
	getiyoSimpleOpenEditor,
} from '../simpleGetiyoActions';

module.exports = (ActionAPI: ActionAPI) => {
	ActionAPI.onExecute((properties, status) => {
		var connectionID =
			properties.connectionID != undefined
				? properties.connectionID
				: 'none';
		var sectionName =
			properties.sectionName != undefined
				? properties.sectionName
				: 'none';

		if (connectionID != 'none') {
			if (sectionName != 'none') {
				var connection = ActionAPI.getConnection(
					'channel',
					connectionID
				);
				if (connection) {
					var channel: Getiyo = connection.instance;
					channel
						.jumpToTimelineSections(sectionName, 'main')
						.then(() => {
							status(`Jumped to section ${sectionName}`);
						})
						.catch(() =>
							status('Unable to jump to section', 'error')
						);
				} else status(`Connection doesn't exist`, 'error');
			} else status('No section name specfied', 'error');
		} else status('No connection specfied', 'error');
	});

	ActionAPI.onOpenEditor((EditorAPI: EditorAPI, properties) => {
		var connectionID =
			properties.connectionID != undefined
				? properties.connectionID
				: 'none';
		var sectionName =
			properties.sectionName != undefined
				? properties.sectionName
				: 'none';

		var connectionField: EditorAPI_Field = {
			id: 'connectionID',
			name: 'Connection',
			type: 'connection',
			connectionType: 'channel',
			value: connectionID,
		};
		var sectionField: EditorAPI_Field = {
			id: 'sectionName',
			name: 'Section',
			type: 'select',
			value: sectionName,
			values: [],
		};

		var lastConnectionID = null;

		var validate = () => {
			if (connectionID != lastConnectionID) {
				lastConnectionID = connectionID;

				if (lastConnectionID != 'none') {
					var connection = ActionAPI.getConnection(
						'channel',
						lastConnectionID
					);
					if (connection) {
						var channel: Getiyo = connection.instance;

						channel
							.getTimelineSections()
							.then((sections: string[]) => {
								sectionField.values = sections.map(
									(sectionText) => {
										return {
											id: sectionText,
											text: sectionText,
										};
									}
								);
								EditorAPI.setFields([
									connectionField,
									sectionField,
								]);
							})
							.catch((error) => {
								sectionField.values = [];
								EditorAPI.setFields([
									connectionField,
									sectionField,
								]);
							});
					} else {
						sectionField.values = [];
						EditorAPI.setFields([connectionField, sectionField]);
					}
				} else {
					sectionField.values = [];
					EditorAPI.setFields([connectionField, sectionField]);
				}
			}
		};

		EditorAPI.onFieldChanges((fields) => {
			var fieldObject = EditorAPI.tools.objectifyFieldsValues(fields);

			connectionID = fieldObject.connectionID;
			sectionName = fieldObject.sectionName;

			connectionField.value = connectionID;
			sectionField.value = sectionName;

			EditorAPI.saveProperties({ connectionID, sectionName });
			validate();
		});

		validate();
	});
};
