define([
	'Global',
	'Text!../html/templates/message-popup.html'
],
	function (Global, template) {
		'use strict';

		return Backbone.View.extend({
			tagName: "div",
			id: "message-for-user",
			attributes: {
				"data-role": "popup",
				"data-dismissible": "false",
				"class": "ui-corner-all"
			},

			initialize: function (options) {
                this.options = options || {};
				this.alertMessage = options && options.alertMessage ? options.alertMessage : "";
                this.code = options && options.code ? options.code : "";
                this.title = options && options.title ? options.title : "";
				this.message = options && options.message ? options.message : "";
				this.serverMessage = options && options.serverMessage ? options.serverMessage : "";
                this.btnNames = options && options.btnNames ? options.btnNames : ["OK"];
                this.callBacks = options && options.callBacks ? options.callBacks : [];
				this.listOfItems = options && options.listOfItems ? options.listOfItems : [];
				this.template = _.template(template);
			},

			render: function () {
				var $container = getMainContent();

				this.$el
					.empty()
					.append(this.template({
						alertMessage: this.alertMessage,
						code: this.code,
						title: this.title,
						message: this.message,
						serverMessage: this.serverMessage,
						btnNames: this.btnNames,
						listOfItems: this.listOfItems
					}));

				$container
					.append(this.$el);

				this.$el
					.trigger('create')
					.popup()
					.popup('open');

				$('#popup-btn-0').focus();

				return this;
			},

			events: {
				"click #popup-btn-0, #popup-btn-1, #popup-btn-2": "handleAction"
			},

            handleAction: function(e) {
                var $targetBtn = $(e.currentTarget),
                    btnIndex = $targetBtn.data("index");

                this.closeMessage();

                if (_.isFunction(this.callBacks[btnIndex])){
                    this.callBacks[btnIndex]();
                }

            },

			closeMessage: function () {
				this.$el
					.popup('close')
					.remove();
			}
		});
	}
);