import { ConnectionManager_Connection } from '../ConnectionManager';
import { Undecked } from '../Core';
import { Log } from '../Logger';
import { Page_Key, Page_Key_Action } from '../Pages/Page';
import { ActionAPI_Properties } from './ActionAPI';
import { Integration_Connection_Field } from './IntegrationsManager';

declare var Undecked: Undecked;
export class EditorAPI {
	_change: (properties: ActionAPI_Properties) => void;

	keyPageID: string;
	keyX: number;
	keyY: number;
	key: Page_Key;

	private _integrationID: string;
	private _actionInstance: Page_Key_Action;
	private _editorID: string;
	isOpen: boolean;

	tools: {
		getFieldFromFields: (fields: EditorAPI_Field[], fieldID: string) => EditorAPI_Field;
		objectifyFields: (fields: EditorAPI_Field[]) => { [fieldID: string]: EditorAPI_Field };
		objectifyFieldsValues: (fields: EditorAPI_Field[]) => { [fieldID: string]: string };
	};

	constructor(settings: EditorAPI_Settings) {
		this._actionInstance = settings.actionInstance;

		this._integrationID = settings.integrationID;
		this.keyPageID = settings.pageID;
		this._editorID = settings.editorID;
		this.keyX = settings.x;
		this.keyY = settings.y;
		this.key = settings.key;
		this.isOpen = false;

		this.tools = {
			getFieldFromFields(fields: EditorAPI_Field[], fieldID: string) {
				for (let i = 0; i < fields.length; i++) if ((fields[i].id = fieldID)) return fields[i];
				return null;
			},
			objectifyFields(fields: EditorAPI_Field[]): { [fieldID: string]: EditorAPI_Field } {
				var fieldObject = {};
				for (let i = 0; i < fields.length; i++) fieldObject[fields[i].id] = fields[i];
				return fieldObject;
			},
			objectifyFieldsValues(fields: EditorAPI_Field[]): { [fieldID: string]: string } {
				var fieldObject = {};
				for (let i = 0; i < fields.length; i++) fieldObject[fields[i].id] = fields[i].value;
				return fieldObject;
			}
		};
	}

	private _emit(query: string, ...args: any[]) {
		if (this.isOpen) Undecked.SocketServer.broadcast(`AE_${this._editorID}`, query, ...args);
	}

	close() {
		this.isOpen = false;
	}

	saveProperties(properties: { [property: string]: any }) {
		this._actionInstance.properties = properties;

		if (Undecked.Pages.exists(this.keyPageID)) Undecked.Pages.get(this.keyPageID).save();
	}

	onFieldChanges(callback: (fields: EditorAPI_Field[]) => void) {
		if (this._change == undefined) {
			this._change = callback;
		} else Log('error', `Unable to register two 'onChange' events`);
	}

	setFields(fields: EditorAPI_Field[]) {
		for (let i = 0; i < fields.length; i++)
			if (fields[i].type == 'connection') {
				fields[i].values = [
					{ id: 'none', text: 'None' }
				];
				var connectionType = fields[i].connectionType;
				if (connectionType) {
					fields[i].values = [
						{ id: 'none', text: 'None' },
						...Undecked.Connections.getConnections(this._integrationID, connectionType)
					];
				}
			}

		this._emit('fields', fields);
	}
}

export interface EditorAPI_Settings {
	actionInstance: Page_Key_Action;

	integrationID: string;
	pageID: string;
	editorID: string;
	x: number;
	y: number;
	key: Page_Key;
}

export interface EditorAPI_Field {
	id: string;
	name: string;
	type: 'text' | 'number' | 'select' | 'connection' | 'color' | 'checkbox';
	values?: { id: string; text: string }[];
	multi?: boolean;
}

export interface EditorAPI_Field extends Integration_Connection_Field {
	required?: boolean;
	value: any;
}
