/// class ProspectDirector.Web.UI.FeatureSets.Editors.MarkupEditorDialog
/// using ProspectDirector.Web.UI.Dialog

/**
[NEEDS DOCUMENTATION]
@class MarkupEditorDialog
@namespace Web.UI.FeatureSets.Editors
@extends Web.UI.Dialog
@constructor
**/
ProspectDirector.Web.UI.FeatureSets.Editors.MarkupEditorDialog = ProspectDirector.Web.UI.Dialog.extend({

    //================================
    // CONSTRUCTOR
    //================================

    init: function (options) {

        this.options = $.extend(true, {
            config: {
                modal: true,
                iframe: false,
                height: 500,
                width: 500,
                minHeight: 300,
                minWidth: 350,
                resizable: false
            }
        }, options);

        this._super(this.options);

        this.render();
        this.activate();

    },

    //================================
    // PROPERTIES
    //================================

    get_container: function () {
        return this.get_window().DOM().get_content();
    },

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

    get_okButton: function () {
        return this.get_container().find("BUTTON[data-id=ok]");
    },

    get_cancelButton: function () {
        return this.get_container().find("BUTTON[data-id=cancel]");
    },

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

    _okButton_onClick: function (e) {

        var value = this.get_editor().data("kendoEditor").value(),
            result = null;

        switch (true) {
            case this._containsMarkupAndText(value):
                result = value;
                break;
            case !this._containsMarkup(value) && value !== "":
                result = value;
        }

        this.hide({
            command: "ok",
            data: result
        });

    },

    _cancelButton_onClick: function (e) {
        this.hide();
    },

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

    activate: function () {

        var editor = null,
            editorElement = null;

        this.get_editor().val(this.options.data.value);

        editor = this.get_editor().kendoEditor({
            tools: [
                "bold",
                "italic",
                "underline",
                "strikethrough",
                "foreColor",
                "fontSize",
                "insertUnorderedList",
                "insertOrderedList"
            ],
            encoded: false
        }).data("kendoEditor");

        editorElement = this.get_editor().closest(".k-editor");

        editorElement.css({
            borderWidth: "1px",
            borderColor: "transparent"
        });

        editor.focus();

        this.get_okButton().on("click", $.proxy(this._okButton_onClick, this));
        this.get_cancelButton().on("click", $.proxy(this._cancelButton_onClick, this));

        this.get_window().DOM().resize();

    },

    render: function () {

        var content = "" +
        "<div class=\"-pd-markup-editor-dialog\" style=\"position:absolute;top:10px;left:10px;right:10px;bottom:10px;box-sizing:border-box;\">" +
        "<textarea data-id=\"editor\" style=\"position:absolute;top:0px;left:0px;right:0px;height:450px;font-size:12px;font-family:arial;box-sizing:border-box;\"></textarea>" +
        "<button data-id=\"ok\" class=\"-pd-button\" style=\"position:absolute;right:112px;bottom:0px;\">OK</button>" +
        "<button data-id=\"cancel\" class=\"-pd-button\" style=\"position:absolute;right:2px;bottom:0px;\">Cancel</button>" +
        "</div>";

        this.get_container().empty();
        this.get_container().append(content);

    },

    //================================
    // PRIVATE HANDLERS
    //================================

    _containsMarkup: function (value) {
        return $(value).html() !== undefined;
    },

    _containsMarkupAndText: function (value) {
        return this._containsMarkup(value) && $(value).text() !== "";
    }

});

//================================
// SHARED METHODS
//================================

ProspectDirector.Web.UI.FeatureSets.Editors.MarkupEditorDialog.open = function (options) {
    return new ProspectDirector.Web.UI.FeatureSets.Editors.MarkupEditorDialog(options);
};