/**
 * @function popup
 * @param message - message  sent, Mandatory, ANY format
 * @param code - target domain message is sent, Optional, String, default value = '*'
 * @return itself
 *
 * @example
 * var messageBox = APP.messageBox(message)
 *     .addButton('Deposit', function() {
 *          //custom Logic
 *          messageBox.destroy(0);
 *      })
 *      .addButton('Continue', function() {
 *          messageBox.destroy(0);
 *      })
 *      .show();
 */
;
(function(win) {
    var APP = win.APP = win.APP || {};

    APP.messageBox = function(message, code) {

        if (APP.data.messageShown !== undefined)
            APP.data.messageShown.destroy(0);

        APP.data.messageShown = (function(message, code) {

            var messageBox = document.getElementById('popup-generic');
            messageBox.innerHTML = document.getElementById('popup-generic-template').innerHTML.trim();
            messageBox.getElementsByClassName('popup-message')[0].innerHTML = message;
            if (code !== undefined)
                messageBox.getElementsByClassName('popup-error')[0].innerHTML = 'Error ID: ' + code;

            return {
                addButton: function(labelText, callback) {
                    var button = document.createElement('div');
                    button.className = "popup-button";
                    button.innerHTML = labelText;
                    button.addEventListener('click', callback);
                    messageBox.getElementsByClassName('popop-buttons-container')[0].appendChild(button);
                    return this;

                },
                destroy: function(action) {
                    APP.data.messageShown = undefined;
                    APP.slider.hidePopup();
                    messageBox.innerHTML = ''; //dirty destruction
                    if (action !== undefined)
                        APP.IGTbridge.console.resume(action);

                    return this;

                },
                show: function(hideConsole) {
                    hideConsole = hideConsole || !!hideConsole;
                    APP.slider.showFullScreenPopup(messageBox, true);
                    if (!hideConsole)
                        document.getElementById('slider').style.display = "";
                    return this;

                }
            }


        })(message, code);

        return APP.data.messageShown;

    }

})(window);