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

module.exports = (ActionAPI: ActionAPI) => {
	ActionAPI.handle((properties: Properties, status) => {
		var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
		var layer1 = properties.layer1 != undefined && properties.layer1.length > 0 ? properties.layer1 : 'ignore';
		var layer2 = properties.layer2 != undefined && properties.layer2.length > 0 ? properties.layer2 : 'ignore';
		var layer3 = properties.layer3 != undefined && properties.layer3.length > 0 ? properties.layer3 : 'ignore';
		var layer4 = properties.layer4 != undefined && properties.layer4.length > 0 ? properties.layer4 : 'ignore';
		var layer5 = properties.layer5 != undefined && properties.layer5.length > 0 ? properties.layer5 : 'ignore';

		if (connectionID != 'none') {
			var connection = ActionAPI.getConnection('wirecast-bridge', connectionID);
			if (connection && connection.instance) {
				connection.instance
					.multiShot(layer1, layer2, layer3, layer4, layer5)
					.then(() => {
						status('Shots has been published', 'info');
					})
					.catch((error) => {
						status(error, 'error');
					});
			}
		} else status('No connection specified', 'error');
	});

	function filterValues(values: { id: string; text: string }[]): { id: string; text: string }[] {
		var newValues = [
			{ id: 'ignore', text: 'Ignore' },
			{ id: 'clear', text: 'Clear Layer' }
		];
		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.length > 0
				? properties.connectionID
				: 'none';
		var layerValues = {
			'1': properties.layer1 != undefined && properties.layer1.length > 0 ? properties.layer1 : 'ignore',
			'2': properties.layer2 != undefined && properties.layer2.length > 0 ? properties.layer2 : 'ignore',
			'3': properties.layer3 != undefined && properties.layer3.length > 0 ? properties.layer3 : 'ignore',
			'4': properties.layer4 != undefined && properties.layer4.length > 0 ? properties.layer4 : 'ignore',
			'5': properties.layer5 != undefined && properties.layer5.length > 0 ? properties.layer5 : 'ignore'
		};

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

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

		var fields: {
			'1'?: EditorAPI_Field;
			'2'?: EditorAPI_Field;
			'3'?: EditorAPI_Field;
			'4'?: EditorAPI_Field;
			'5'?: EditorAPI_Field;
		} = {};
		for (let i = 1; i < 6; i++) {
			fields[i] = {
				id: `layer${i}`,
				name: `Layer ${i}`,
				type: 'select',
				values: [],
				value: layerValues[i]
			};
		}

		var setFields = () => {
			EditorAPI.setFields([
				connectionField,
				fields[1],
				fields[2],
				fields[3],
				fields[4],
				fields[5]
			]);
		};

		function updateLayerFields() {
			if (connection && connection.instance) {
				var instance: WirecastConnection = connection.instance;
				instance.getShots((shots) => {
					for (var layer in shots) {
						var shotList: { id: string; text: string }[] = [];
						for (var shotIndex in shots[layer].shots) {
							var shot = shots[layer].shots[shotIndex];
							shotList.push({ id: String(shot.id), text: shot.name });
						}

						fields[layer].value = layerValues[layer];
						fields[layer].values = filterValues(shotList);
					}

					setFields();
				});
			} else setFields();
		}

		EditorAPI.onFieldChanges((fields) => {
			var fieldObject = EditorAPI.tools.objectifyFieldsValues(fields);
			var toSave = {
				connectionID: fieldObject.connectionID
			};
			connectionField.value = fieldObject.connectionID;
			for (let i = 1; i < 6; i++) {
				fields[i].value = fieldObject[`layer${i}`];
				toSave[`layer${i}`] = fieldObject[`layer${i}`];
			}
			EditorAPI.saveProperties(toSave);

			if (connectionID != fieldObject.connectionID) {
				connectionID = fieldObject.connectionID;
				if (fieldObject.connectionID != 'none') {
					connection = ActionAPI.getConnection('wirecast-bridge', connectionID);
					updateLayerFields();
				} else {
					for (let i = 1; i < 6; i++) {
						fields[i].values = [];
					}
					setFields();
				}
			}
		});

		updateLayerFields();
	});
};

interface Properties {
	connectionID: string;
	layer1: string;
	layer2: string;
	layer3: string;
	layer4: string;
	layer5: string;
}
