(function (window, $) {
	'use strict';

	const app = window.WPMDEmailTemplatesApp;

	if (!app) {
		return;
	}

	function initialiseEditors() {
		const htmlField = document.getElementById('wpmd-template-html');
		const cssField = document.getElementById('wpmd-template-css');

		if (window.wp && window.wp.codeEditor && htmlField) {
			const settings = (window.wpmdCodeEditorSettings && window.wpmdCodeEditorSettings.html) || { codemirror: { mode: 'text/html' } };
			app.state.htmlEditor = window.wp.codeEditor.initialize(htmlField, settings).codemirror;
		}

		if (window.wp && window.wp.codeEditor && cssField) {
			const settings = (window.wpmdCodeEditorSettings && window.wpmdCodeEditorSettings.css) || { codemirror: { mode: 'css' } };
			app.state.cssEditor = window.wp.codeEditor.initialize(cssField, settings).codemirror;
		}
	}

	function getTextareaValue(selector) {
		const $field = $(selector);
		return $field.length ? $field.val() : '';
	}

	const service = {
		getEmailType() {
			return $('#wpmd-email-type').val();
		},
		getTemplatePayload() {
			return {
				subject: $('#wpmd-template-subject').val() || '',
				heading: $('#wpmd-template-heading').val() || '',
				html: app.state.htmlEditor ? app.state.htmlEditor.getValue() : getTextareaValue('#wpmd-template-html'),
				css: app.state.cssEditor ? app.state.cssEditor.getValue() : getTextareaValue('#wpmd-template-css'),
				is_active: $('#wpmd-template-active').is(':checked') ? 1 : 0,
				post_id: parseInt($('#wpmd-template-id').val(), 10) || 0,
			};
		},
		insertHtmlSnippet(snippet) {
			if (app.state.htmlEditor && typeof app.state.htmlEditor.replaceRange === 'function') {
				const doc = app.state.htmlEditor.getDoc();
				const cursor = doc.getCursor();
				doc.replaceRange(snippet, cursor);
				app.state.htmlEditor.focus();
				return true;
			}

			const $textarea = $('#wpmd-template-html');

			if ($textarea.length) {
				const element = $textarea.get(0);
				const start = element.selectionStart || 0;
				const end = element.selectionEnd || 0;
				const value = $textarea.val();

				$textarea.val(value.substring(0, start) + snippet + value.substring(end));

				try {
					element.selectionStart = element.selectionEnd = start + snippet.length;
				} catch (error) {
					// ignore cursor placement errors
				}

				$textarea.focus();
				return true;
			}

			return false;
		},
		evaluateLogoHint() {
			const content = app.state.htmlEditor ? app.state.htmlEditor.getValue() : getTextareaValue('#wpmd-template-html');
			const hasBrand = content && content.indexOf('{{shop_brand}}') !== -1;
			const $hint = $('.wpmd-logo-hint');

			if (!$hint.length) {
				return;
			}

			if (hasBrand) {
				$hint.attr('hidden', true);
			} else {
				$hint.removeAttr('hidden');
			}
		},
		updateStickySendButton() {
			const recipient = $('#wpmd-test-recipient').val();
			$('.maildoctor-sticky-bar .wpmd-send-test-button').prop('disabled', !recipient);
		},
	};

	app.provide('form', service);

	app.register(() => {
		initialiseEditors();

		if (app.state.htmlEditor && typeof app.state.htmlEditor.on === 'function') {
			app.state.htmlEditor.on('change', service.evaluateLogoHint);
		}

		$('#wpmd-test-recipient').on('input change', service.updateStickySendButton);
		service.updateStickySendButton();

		setTimeout(service.evaluateLogoHint, 300);
	});
})(window, jQuery);
