import './style.scss';

// TinyMCE
const tinymce = require('tinymce/tinymce');
require('tinymce/icons/default');
require('tinymce/themes/silver');
require('tinymce/skins/ui/oxide/skin.css');
require('tinymce/models/dom');
require('tinymce/plugins/advlist');
require('tinymce/plugins/code');
require('tinymce/plugins/emoticons');
require('tinymce/plugins/emoticons/js/emojis');
require('tinymce/plugins/link');
require('tinymce/plugins/lists');
require('tinymce/plugins/table');
require('tinymce/plugins/autolink');
require('tinymce/plugins/image');
require('tinymce/plugins/charmap');
require('tinymce/plugins/preview');
require('tinymce/plugins/anchor');
require('tinymce/plugins/pagebreak');
require('tinymce/plugins/searchreplace');
require('tinymce/plugins/wordcount');
require('tinymce/plugins/visualblocks');
require('tinymce/plugins/visualchars');
require('tinymce/plugins/code');
require('tinymce/plugins/fullscreen');
require('tinymce/plugins/insertdatetime');
require('tinymce/plugins/media');
require('tinymce/plugins/nonbreaking');
require('tinymce/plugins/save');
require('tinymce/plugins/table');
require('tinymce/plugins/directionality');
require('tinymce/plugins/emoticons');
require('tinymce/plugins/template');
const contentUiCss = require('tinymce/skins/ui/oxide/content.css');
const contentCss = require('tinymce/skins/content/default/content.css');

export default new Class({
	Extends: Hedera.Form,
	Template: require('./ui.xml'),
	
	editor: null,

	activate() {
		this.$.model.mode = Db.Model.Mode.ON_DEMAND;
		this.$.model.setDefault('userFk', 'news',
			new Sql.Function({schema: 'account', name: 'myUser_getId'}));

		tinymce.init({
			 target: this.$.htmlEditor
			,plugins:
				 ' advlist autolink lists link image charmap preview'
				+' anchor pagebreak searchreplace wordcount visualblocks'
				+' visualchars code fullscreen insertdatetime media nonbreaking'
				+' save table directionality emoticons template'
			,toolbar1:
				 ' print preview paste'
				+'|link image media emoticons blockquote hr'
				+'|insertfile undo redo'
				+'|bold italic'
				+'|alignleft aligncenter alignright alignjustify'
				+'|bullist numlist outdent indent'
				+'|styleselect'
				+'|fontselect fontsizeselect'
				+'|forecolor backcolor '
			,image_advtab: true
			,init_instance_callback: editor => this._onEditorInit(editor)
			,skin: false
			,content_css: false
			,content_style: contentUiCss.toString() + '\n' + contentCss.toString()
		});
	},
	
	deactivate() {
		this.editor.destroy();
	},
	
	_onEditorInit(editor) {
		this.editor = editor;
		editor.getDoc().body.style.fontSize = '1rem';
		this.setEditorText();
	},
	
	setEditorText() {
		if (!this.editor) return;
		const row = this.$.iter.$;
		this.editor.setContent(row ? row.text : '');
	},

	onReady() {
		if (this.hash.$.new)
			this.setEditorText();
		else
			this.$.iter.insertRow();
	},

	async onAcceptClick() {
		this.$.iter.set('text', this.editor.getContent());
		await this.$.iter.performOperations();
		Htk.Toast.showMessage(_('NewChangedSuccessfully'));
		this.onReturnClick();
	},

	onReturnClick() {
		this.hash.setAll({form: 'news/news'});
	}
});
