/// class PD.Web.UI.MessageDialog
/// using PD.Web.UI.Dialog

/**
@class MessageDialog
@constructor
@namespace Web.UI
@extends Web.UI.Dialog
**/
PD.Web.UI.MessageDialog =
PD.Web.UI.Dialog.extend({

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

    _buttons: null,
    _message: null,
    _title: null,
    _icon: null,
    _width: null,

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

    get_buttons: function () {
        return this._buttons;
    },

    get_message: function () {
        return this._message;
    },

    get_title: function () {
        return this._title;
    },

    get_icon: function () {
        return this._icon;
    },

    get_width: function () {
        return this._width;
    },

    get_textNode: function () {
        return this.get_node().find(".-pd-msg-dialog-text");
    },

    get_buttonsNode: function () {
        return this.get_node().find(".-pd-msg-dialog-buttons");
    },

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

    onInit: function (e) {

        var options = this.get_options();

        this._buttons = this._getButtonsList(options);
        this._message = options.message || "message";
        this._title = options.title || "title";
        this._icon = options.icon || null;
        this._width = options.width || 450;

    },

    onActivate: function () {
        this._setIcon();
        this._setText();
        this.setTitle(this.get_title());
        this._renderButtons();
        this._window._kendo.setOptions({
            width: this._width,
            resizable: false,
            actions: []
        });
        this.center();
        this._setDefaultFocus();
    },

    //================================
    // PROTECTED METHODS
    //================================

    template: function () {
        return [
            "<div class='-pd-msg-dialog'>",
                "<div class='-pd-msg-dialog-text'>",
                "</div>",
                "<div class='-pd-msg-dialog-buttons'>",
                "</div>",
            "</div>"
        ];
    },

    fireButtonCallback: function (callback) {
        callback({
            sender: this,
            close: $.proxy(this.close, this)
        });
    },

    //================================
    // PRIVATE METHODS
    //================================

    _setDefaultFocus: function () {
        this.get_buttonsNode().find("button:first").focus();
    },

    _setIcon: function () {

        var icon = this.get_icon(),
            path = null;

        if (icon != null) {

            if (icon.indexOf("~") === 0) {
                path = PD.Path.resolve(icon);
            }
            else {
                path = PD.Path.resolve("~/application/icons/32x32/" + icon);
            }

            this.get_textNode().addClass("w-icon");

            this.get_textNode().css({
                backgroundImage: "url(" + path + ")"
            });

        }

    },

    _setText: function () {
        this.get_textNode().append(this.get_message());
    },

    _renderButtons: function () {

        var that = this,
            template = "<button class='-pd-button'></button>";

        this.get_buttons().each(function (index, button) {

            var $template = $(template);

            $template.text(button.label);
            $template.data("id", button.id ? button.id : "-");

            if (button.callback) {
                $template.on("click", function () {
                    that.fireButtonCallback(button.callback);
                });
            }
            else {
                if (button.id === "cancel") {
                    $template.on("click", function () {
                        that.fireButtonCallback(that.close);
                    });
                }
            }

            that.get_buttonsNode().append($template);

        });

    },

    _getButtonsList: function () {

        var result = null,
            buttons = this.get_options()["buttons"];

        if (PD.Type.isArray(buttons)) {
            result = new PD.BCL.List(buttons);
        }
        else {
            result = new PD.BCL.List();
            this._insertDefaultButtons(result);
        }

        return result;

    },

    _insertDefaultButtons: function (buttons) {
        
        var that = this,
            options = this.get_options(),
            closeCallback = function (data) {
                that.close(data);
            };

        if (PD.Type.isFunction(options.ok)) {

            buttons.add({
                id: "ok",
                label: "OK",
                callback: options.ok
            });

            if (!options.noCancel) {

                buttons.add({
                    id: "cancel",
                    label: "Cancel",
                    callback: closeCallback
                });

            }

        }
        else {

            buttons.add({
                id: "ok",
                label: "OK",
                callback: closeCallback
            });

        }

    }

});

//================================
// STATIC METHODS
//================================

PD.Web.UI.MessageDialog.open = function (options) {
    return new PD.Web.UI.MessageDialog($.extend({
        title: "Message"
    }, options));
};

PD.Web.UI.MessageDialog.info = function (options, type) {
    type = type || PD.Web.UI.MessageDialog;
    return new type($.extend({
        title: "Information",
        icon: "information.png"
    }, options));
};

PD.Web.UI.MessageDialog.warn = function (options, type) {
    type = type || PD.Web.UI.MessageDialog;
    return new type($.extend({
        title: "Warning",
        icon: "sign_warning.png"
    }, options));
};

PD.Web.UI.MessageDialog.error = function (options, type) {
    type = type || PD.Web.UI.MessageDialog;
    return new type($.extend({
        title: "Error",
        icon: "error.png"
    }, options));
};

PD.Web.UI.MessageDialog.success = function (options, type) {
    type = type || PD.Web.UI.MessageDialog;
    return new type($.extend({
        title: "Success",
        icon: "ok.png"
    }, options));
};