(function (paw, utils) {
    function NoteEditorPopup(settings, localization) {
        var self = this;
        this.settings = paw.lang.append({
            onSave: null,
            onCancel: null,
            destroyOnClose: false,
            encode: 'none'
        }, settings || {});
        this.localization = paw.lang.append({
            popupTitle: '@Loc.Note',
            save: '@Loc.Save',
            cancel: '@Loc.Cancel'
        }, localization || {});

        this.$popup = null;
        this.noteEditor = null;
        this.lastContent = '';

        this.onEscKey = function(e){
            if (e.key === 'Escape') {
                self.hide();
            }
        };
    }

    NoteEditorPopup.prototype.show = function (content, settings) {
        var self = this;
        var placeholder = '';
        if(settings.placeholder){
            placeholder = settings.placeholder;
        }
        else if(utils.value.isSet(self.settings.placeholder)){
            placeholder = self.settings.placeholder;
        }

        placeholder = utils.value.isSet(placeholder)? 'placeholder="' + placeholder + '"' : '';

        var markup = [];
        markup.push('<div class="paw-area-v paw-holder p-t-3x" data-note-editor-component>');
        markup.push('   <div class="paw-col-d paw-nooverflow paw-area-v">');
        markup.push('      <textarea id="note-editor-content" class="paw-col-d form-control" data-note-editor-field="content"' + placeholder + '></textarea>');
        markup.push('   </div>');
        markup.push('   <div class="paw-col paw-area-h p-t-4x">');
        markup.push('      <div class="paw-col-d"></div>');
        markup.push('      <div class="paw-col">');
        markup.push('         <button id="note-editor-save-action" note-editor-action="cancel" class="btn btn-outline">');
        markup.push('            <i class="fa fa-close paw-tc-accent"></i>');
        markup.push('            <span>' + this.localization.cancel +'</span>');
        markup.push('         </button>');
        markup.push('         <button id="note-editor-cancel-action" note-editor-action="save" class="btn btn-primary">');
        markup.push('            <i class="fa fa-save"></i>');
        markup.push('            <span>' + this.localization.save +'</span>');
        markup.push('         </button>');
        markup.push('      </div>');
        markup.push('   </div>');
        markup.push('</div>');
        $().customModal({
            id: 'note-editor-popup',
            destroyOnClose: true,
            showCloseButton: true,
            type: "modal",
            title: this.localization.popupTitle,
            content: markup.join('\n'),
            onClose: function () {
                if (settings.onClose) {
                    settings.onClose(self.lastContent);
                }
                if (self.settings.onClose) {
                    self.settings.onClose(self.lastContent);
                }
                if(self.settings.destroyOnClose){
                    self.destroy();
                }
            }
        });
        self.lastContent = content;
        
        this.$popup = $('#note-editor-popup');

        this.noteEditor = new paw.com.noteEditor.NoteEditorComponent(this.$popup.find('[data-note-editor-component]'), {
            encode: this.settings.encode,
            onSave: function (content) {
                self.lastContent = content;
                self.hide();
                if(utils.value.isSet(settings) && utils.value.isFunction(settings.onSave)){
                    settings.onSave(content);
                }
                if(utils.value.isFunction(self.settings.onSave)){
                    self.settings.onSave(content);
                }
            },
            onCancel: function () {
                self.lastContent = content;
                self.hide();
                if(utils.value.isSet(settings) && utils.value.isFunction(settings.onCancel)){
                    settings.onCancel(content);
                }
                if(utils.value.isFunction(self.settings.onCancel)){
                    self.settings.onCancel();
                }
            }
        }, this.localization, content);
        this.noteEditor.init();

        $(document).on('keyup', this.onEscKey);
    };

    NoteEditorPopup.prototype.hide = function () {
        this.$popup.customModal('close');
    };

    NoteEditorPopup.prototype.destroy = function(){
        this.noteEditor.destroy();
        $(document).off('keyup', this.onEscKey);
    };

    paw.com.noteEditor.NoteEditorPopup = NoteEditorPopup;
})(paw, paw.utils);