import { ViscaCommand } from 'visca-over-ip';
import { ActionAPI, ActionAPI_StatusTypes } from '../../../ActionAPI';
import { EditorAPI, EditorAPI_Field } from '../../../EditorAPI';
import { simpleCommandEditor, simpleCommandHandle } from '../simpleCommandHandler';

module.exports = (actionAPI: ActionAPI) => {
	actionAPI.handle((properties, status: (text: string, type: ActionAPI_StatusTypes) => void) => {
		var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
		var numberValue = properties.number != undefined ? properties.number : 10;

		if (connectionID != 'none') {
			var connection = actionAPI.getConnection('viscaOverIpCamera', connectionID);
			if (connection != undefined && connection.instance != undefined) {
				if (connection.instance.internal != undefined) connection.instance.internal = {};

				connection.instance.internal.pantiltSpeed = numberValue;
				status('Speed has been set', 'info');
			} else status('Not connected', 'error');
		} else status('No connection specified', 'error');
	});

	actionAPI.onOpenEditor((EditorAPI, properties) => {
		var numberField: EditorAPI_Field = {
			id: 'number',
			name: 'Speed',
			type: 'select',
			value: properties.number != undefined ? properties.number : 1,
			values: []
		};
		for (let i = 0; i < 25; i++) {
			numberField.values.push({ id: String(i), text: String(i) });
		}
		simpleCommandEditor(EditorAPI, properties, [
			numberField
		]);
	});
};
