import { Undecked } from '../../../../Core';
import { Deck } from '../../../../Decks/Deck';
import { ActionAPI } from '../../../ActionAPI';
import { EditorAPI, EditorAPI_Field } from '../../../EditorAPI';

declare var Undecked: Undecked;

module.exports = (actionAPI: ActionAPI) => {
	actionAPI.handle(
		(
			properties: Deck_SetBackground,
			status: (text: string, type: 'info' | 'error' | 'warn') => void,
			deck: Deck
		) => {
			if (properties.key != undefined && properties.key.length > 0) {
				if (properties.color != undefined) {
					var location = Undecked.Pages.KeyManager.getLocation(properties.key);
					if (location) {
						if (Undecked.Pages.exists(location.pageID)) {
							var page = Undecked.Pages.get(location.pageID);

							if (page.hasKey(location.x, location.y)) {
								var key = page.getKey(location.x, location.y);
								if (key.appearence == undefined) key.appearence = {};
								if (key.appearence.background == undefined) key.appearence.background = { color: null };
								key.appearence.background.color = properties.color;

								page.updateKey(location.x, location.y, key, null);

								status('Key color has been updated', 'info');
							} else status('Unable to find locaton', 'error');
						} else status('Unable to find page', 'error');
					} else status('Invalid key ID. Maybe it was removed?', 'error');
				} else status('No color specified', 'error');
			} else status('No key specified', 'error');
		}
	);

	actionAPI.onOpenEditor((editorAPI: EditorAPI, properties: Deck_SetBackground) => {
		var lastPageState = 'current';
		var initKey = 'current';
		var initColor = properties.color != undefined ? properties.color : '#ff0000';
		if (properties.key != undefined) {
			var location = Undecked.Pages.KeyManager.getLocation(properties.key);
			if (location) {
				lastPageState = location.pageID == editorAPI.keyPageID ? 'current' : location.pageID;
			}

			initKey = properties.key == editorAPI.key.id ? 'current' : properties.key;
		}

		var fields: EditorAPI_Field[] = [
			{
				id: 'page',
				name: 'Page',
				type: 'select',
				value: lastPageState,
				values: getPageValues()
			},
			{
				id: 'key',
				name: 'Key',
				type: 'select',
				value: initKey,
				values: getPageKeyValues(lastPageState)
			},
			{
				id: 'color',
				name: 'Color',
				type: 'color',
				value: initColor
			}
		];

		editorAPI.saveProperties({
			key: initKey == 'current' ? editorAPI.key.id : initKey,
			color: initColor
		});

		editorAPI.onFieldChanges((changedFields: EditorAPI_Field[]) => {
			var fieldObject = editorAPI.tools.objectifyFieldsValues(changedFields);

			fields[0].value = fieldObject.page;
			fields[1].value = fieldObject.key;
			fields[2].value = fieldObject.color;

			if (fieldObject.page != lastPageState) {
				fields[1].values = getPageKeyValues(fieldObject.page);
				editorAPI.setFields(fields);
			}

			editorAPI.saveProperties({
				key: fieldObject.key == 'current' ? editorAPI.key.id : fieldObject.key,
				color: fieldObject.color
			});
		});

		editorAPI.setFields(fields);

		function getPageValues(): { id: string; text: string }[] {
			return [
				{ id: 'current', text: 'Current Page' },
				...Undecked.Pages.getNames().map((page) => {
					return { id: page.pageID, text: page.name };
				})
			];
		}

		function getPageKeyValues(pageID: string): { id: string; text: string }[] {
			var values = [
				{ id: 'current', text: 'Current Key' }
			];

			if (pageID == 'current') pageID = editorAPI.keyPageID;
			else values = [];

			if (Undecked.Pages.exists(pageID))
				return [
					...values,
					...Undecked.Pages.get(pageID).getKeyTextList().map((key) => {
						return { id: key.id, text: `${key.x},${key.y} - ${key.text != null ? key.text : 'Empty'}` };
					})
				];
			else return [];
		}
	});
};

interface Deck_SetBackground {
	key: string;
	color: string;
}
