/// class ProspectDirector.Web.UI.FeatureSets.Editors.Macro.AceEditor
/// using ProspectDirector.Web.UI.FeatureSets.MacroEditorBase

ProspectDirector.Web.UI.FeatureSets.Editors.Macro.AceEditor =
ProspectDirector.Web.UI.FeatureSets.MacroEditorBase.extend({

    //==============================
    // FIELDS
    //==============================

    _ace: null,

    //==============================
    // PROPERTIES
    //==============================
    
    get_node: function () {
        return $("#" + this.getId());
    },

    get_ace: function () {
        return this._ace;
    },

    get_editorNode: function () {
        return this.get_node().find("[data-id=editor]");
    },

    get_mode: function () {
        return this.get_definition().get_data()["mode"];
    },

    get_isReadOnly: function () {
        return this.get_definition().get_data()["isReadOnly"];
    },

    //==============================
    // EVENT HANDLERS
    //==============================

    _ace_onInit: function () {

        var value = this.getPrimaryValue();

        ace.config.set("basePath", PD.Path.resolve("~/areas/shared/assets/external/ace"));
        this._ace = ace.edit(this.get_editorNode()[0]);
        this.get_ace().renderer.setShowPrintMargin(false);
        this.get_ace().setShowFoldWidgets(true);
        this.get_ace().getSession().setUseSoftTabs(false);
        this.get_ace().setTheme("ace/theme/chrome");
        this.get_ace().session.setMode({ path: "ace/mode/" + this.get_mode(), v: Date.now() });
        this.get_ace().setFontSize("14px");
        this.get_ace().setValue(value == null ? "" : value);
        this.get_ace().setReadOnly(this.get_isReadOnly());
        this.focusEditor();

    },

    //==============================
    // PUBLIC METHODS
    //==============================

    activate: function () {
        this._ace_onInit();
    },

    render: function () {

        return [
            "<div id=\"" + this.getId() + "\">",
                "<div data-id='editor' style='position:absolute;top:0;left:0;right:0;bottom:0;overflow:hidden;'>",
                "</div>",
            "</div>"
        ].join("");

    },

    commit: function () {
        this.setPrimaryValue(this.get_ace().getValue());
    },

    focusEditor: function () {
        
        var session = this.get_ace().getSession();
        var lineCount = session.getLength();

        this.get_ace().focus();
        this.get_ace().gotoLine(lineCount, session.getLine(lineCount - 1).length);

    },

    size: function () {
        if (this.get_ace() != null) {
            this.get_ace().resize();
        }
    },

    destroy: function () {
        if (this.get_ace() != null) {
            this.get_ace().destroy();
        }
    }
    
});