import { ActionAPI } from '../../../ActionAPI';
import { EditorAPI, EditorAPI_Field } from '../../../EditorAPI';

module.exports = (ActionAPI: ActionAPI) => {
	ActionAPI.handle((properties: Properties, status) => {
		var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
		var layerID = properties.layerID != undefined && properties.layerID.length > 0 ? properties.layerID : 'none';

		if (connectionID != 'none' && connectionID.length > 0) {
			if (layerID != 'none' && layerID.length > 0) {
				var connection = ActionAPI.getConnection('wirecast-bridge', connectionID);
				if (connection && connection.instance) {
					connection.instance
						.clearLayer(layerID)
						.then(() => {
							status('Shot has been published', 'info');
						})
						.catch((error) => {
							status(error, 'error');
						});
				}
			} else status('No layer specified', 'error');
		} else status('No connection specified', 'error');
	});

	function filterValues(values: { id: string; text: string }[]): { id: string; text: string }[] {
		var newValues = [
			{ id: 'none', text: 'None' }
		];
		for (let i = 0; i < values.length; i++) if (values[i].text != 'Clear Layer') newValues.push(values[i]);
		return newValues;
	}

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

		var connection = ActionAPI.getConnection('wirecast-bridge', connectionID);

		var connectionField: EditorAPI_Field = {
			id: 'connectionID',
			name: 'Connection',
			type: 'connection',
			connectionType: 'wirecast-bridge',
			value: connectionID
		};

		var layerField: EditorAPI_Field = {
			id: 'layerID',
			name: 'Layer',
			type: 'select',
			values: [],
			value: layerID
		};

		var setFields = () => {
			EditorAPI.setFields([
				connectionField,
				layerField
			]);
		};

		if (connection != undefined && connection.instance != undefined) {
			connection.instance.getLayersList((layers) => {
				layerField.values = filterValues(
					layers.map((layer) => {
						return { id: layer.id, text: layer.name };
					})
				);
				setFields();
			});
		} else setFields();

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

			connectionField.value = fieldObject.connectionID;
			layerField.value = fieldObject.layerID;

			EditorAPI.saveProperties({ connectionID: fieldObject.connectionID, layerID: fieldObject.layerID });

			if (connectionID != fieldObject.connectionID) {
				if (fieldObject.connectionID != 'none') {
					connection = ActionAPI.getConnection('wirecast-bridge', fieldObject.connectionID);
					if (connection != undefined && connection.instance != undefined) {
						connection.instance.getLayersList((layers) => {
							layerField.values = filterValues(
								layers.map((layer) => {
									return { id: layer.id, text: layer.name };
								})
							);
							setFields();
						});
					} else setFields();
				} else {
					layerField.values = [];
					connectionID = 'none';
					setFields();
				}
			}
		});
	});
};

interface Properties {
	connectionID: string;
	layerID: string;
}
