declare var Undecked: Undecked;

import { Undecked } from '../Core';
import { Log } from '../Logger';
import { ActionAPI } from './ActionAPI';
import { IntegrationsManager_Integration } from './IntegrationsManager';

export class IntegrationAPI {
	private _integrationID: string;
	private _integration: IntegrationsManager_Integration;

	private _actions: {
		[actionID: string]: { api: ActionAPI; name: string };
	};

	private _connectionvalidators: {
		[connectionType: string]: (connectionValidatorAPI: ConnectionValidatorAPI) => void;
	};

	constructor(settings: IntegrationAPI_Settings) {
		this._integrationID = settings.integrationID;
		this._integration = settings.integration;
		this._actions = {};
		this._connectionvalidators = {};
	}

	registerAction(actionID: string, name: string | any, callback?: (actionAPI: ActionAPI) => void) {
		//Backwards compatibility
		if (typeof name == 'function') {
			callback = name;
			name = actionID;
			if (this._integration.integration.actions != undefined)
				for (let i = 0; i < this._integration.integration.actions.length; i++)
					if (this._integration.integration.actions[i].id == actionID) {
						name = this._integration.integration.actions[i].name;
						break;
					}
		}

		if (this._actions[actionID] == undefined) {
			this._actions[actionID] = { api: new ActionAPI({ integrationID: this._integrationID }), name };
			callback(this._actions[actionID].api);
		} else Log('error', `Integration '${this._integrationID}' tried to register duplicate action '${actionID}'`);
	}

	registerConnectionValidator(
		connectionType: string,
		callback: (connectionValidatorAPI: ConnectionValidatorAPI) => void
	) {
		if (this._integration.connectionslist.includes(connectionType)) {
			if (this._connectionvalidators[connectionType] == undefined) {
				this._connectionvalidators[connectionType] = callback;
			} else
				Log(
					'error',
					`Integration '${this
						._integrationID}' tried to register duplicate connection validator '${connectionType}'`
				);
		} else
			Log(
				'error',
				`Integration '${this
					._integrationID}' tried to register non-existing connection validator '${connectionType}'`
			);
	}

	hasAction(actionID: string): boolean {
		return this._actions[actionID] != undefined;
	}

	getAction(actionID: string): ActionAPI {
		if (this.hasAction(actionID)) return this._actions[actionID].api;
		return null;
	}

	getActionList(): { id: string; name: string }[] {
		var list: { id: string; name: string }[] = [];
		for (var actionID in this._actions) list.push({ id: actionID, name: this._actions[actionID].name });
		return list;
	}

	hasValidator(connectionType: string): boolean {
		return this._connectionvalidators[connectionType] != undefined;
	}

	getValidator(connectionType: string): (connectionValidatorAPI: ConnectionValidatorAPI) => void {
		if (this.hasValidator(connectionType)) return this._connectionvalidators[connectionType];
		return null;
	}
}

export interface IntegrationAPI_Settings {
	integrationID: string;
	integration: IntegrationsManager_Integration;
}

export interface ConnectionValidatorAPI {
	properties: { [key: string]: any };
	instance: any;
	callback: (valid: boolean, errormessage?: string) => void;
	setInstance: (instance: any) => void;
}
