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

module.exports = (ActionAPI: ActionAPI) => {
	ActionAPI.handle((properties, status) => {
		var connectionID =
			properties.connectionID != undefined
				? properties.connectionID
				: 'none';
		var sceneID =
			properties.sceneID != undefined ? properties.sceneID : 'none';
		var displayID =
			properties.displayID != undefined ? properties.displayID : 'none';
		if (connectionID != 'none') {
			if (sceneID != 'none') {
				if (displayID != 'none') {
					var connection = ActionAPI.getConnection(
						'channel',
						connectionID
					);
					if (connection) {
						var channel: Getiyo = connection.instance;

						return channel.publishScene(sceneID, displayID);
					} else status(`Connection doesn't exist`, 'error');
				} else status('No display specfied', 'error');
			} else status('No scene specfied', 'error');
		} else status('No connection specfied', 'error');
	});

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

		var connectionField: EditorAPI_Field = {
			id: 'connectionID',
			name: 'Connection',
			type: 'connection',
			connectionType: 'channel',
			value: connectionID,
		};
		var sceneField: EditorAPI_Field = {
			id: 'sceneID',
			name: 'Scene',
			type: 'select',
			value: sceneID,
			values: [],
		};

		var displayField: EditorAPI_Field = {
			id: 'displayID',
			name: 'Display',
			type: 'select',
			value: displayID,
			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
							.getScenes()
							.then((scenes: { id: string; name: string }[]) => {
								sceneField.values = scenes.map((scene) => {
									return { id: scene.id, text: scene.name };
								});

								channel
									.getDisplays()
									.then(
										(
											displays: {
												id: string;
												name: string;
											}[]
										) => {
											displayField.values = displays.map(
												(display) => {
													return {
														id: display.id,
														text: display.name,
													};
												}
											);
											EditorAPI.setFields([
												connectionField,
												sceneField,
												displayField,
											]);
										}
									)
									.catch((error) => {
										sceneField.values = [];
										EditorAPI.setFields([
											connectionField,
											sceneField,
											displayField,
										]);
									});
							})
							.catch((error) => {
								sceneField.values = [];
								EditorAPI.setFields([
									connectionField,
									sceneField,
									displayField,
								]);
							});
					} else {
						sceneField.values = [];
						EditorAPI.setFields([
							connectionField,
							sceneField,
							displayField,
						]);
					}
				} else {
					sceneField.values = [];
					EditorAPI.setFields([
						connectionField,
						sceneField,
						displayField,
					]);
				}
			}
		};

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

			connectionID = fieldObject.connectionID;
			sceneID = fieldObject.sceneID;
			displayID = fieldObject.displayID;

			connectionField.value = connectionID;
			sceneField.value = sceneID;

			EditorAPI.saveProperties({ connectionID, sceneID, displayID });
			validate();
		});

		validate();
	});
};
