'use strict';

overlord_service.factory('modal', ['$http', '$compile', function ($http, $compile) {
    var modal = {};

    modal.get = function (create) {
        if (!modal.dialog && create) {
            modal.dialog = $('<div class="modal hide"></div>');
            modal.dialog.appendTo('BODY');
        }
        return modal.dialog;
    };

    modal.compileAndRun = function (dialog, scope, options) {
        $compile(dialog)(scope);
        dialog.modal(options);
    };

    modal.alert = function (title, text, button, scope, options) {
        text = (text) ? text : "Alert";
        button.text = (button.text) ? button.text : "Ok";
        button.class = (button.class) ? button.class : "btn";
        var dialog = modal.get(true),
            html = "";
        if (title) {
            html += '' +
                '<div class="modal-header">' +
                '    <button id="close" type="button" class="close">×</button>' +
                '    <h1>' + title + '</h1>' +
                '</div>';
        }
        html += '' +
            '<div class="modal-body">' + text + '</div>' +
            '<div class="modal-footer">' +
            '    <button id="button" class="' + button.class + '">' + button.text + '</button>' +
            '</div>';
        dialog.html(html);
        dialog.find("#button").click(function () {
            if (button.click) {
                scope.$eval(button.click);
            }
            modal.close();
        });
        modal.compileAndRun(dialog, scope, options);
    };

    modal.confirm = function (title, actionText, actionButton, cancelButton, scope, options) {
        actionText = (actionText) ? actionText : "Are you sure?";
        actionButton.text = (actionButton.text) ? actionButton.text : "Ok";
        cancelButton.text = (cancelButton.text) ? cancelButton.text : "Cancel";

        var dialog = modal.get(true),
            html = '';
        if (title) {
            html += '' +
                '<div class="modal-header">' +
                '    <button id="close" type="button" class="close">×</button>' +
                '    <h1>' + title + '</h1>' +
                '</div>';
        }
        html += '' +
            '<div class="modal-body">' + actionText + '</div>' +
            '<div class="modal-footer">' +
            '    <button id="action" class="' + actionButton.class + '">' + actionButton.text + '</button>' +
            '    <button id="cancel" class="btn btn-cancel">' + cancelButton.text + '</button>' +
            '</div>';
        dialog.html(html);

        dialog.find("#action").click(function () {
            if (actionButton.click) {
                scope.$eval(actionButton.click);
            }
            modal.close();
        });
        dialog.find("#cancel").click(function () {
            if (cancelButton.click) {
                scope.$eval(cancelButton.click);
            }
            modal.close();
        });
        dialog.find("#close").click(function () {
            modal.close();
        });
        modal.compileAndRun(dialog, scope, options);

    };

    modal.template = function (url, onSuccess, onCancel, scope, options) {
        $http.get(url).success(function (data) {

            var dialog = modal.get(true);
            dialog.html(data);
            dialog.find("#action").click(function () {
                if (!onSuccess || scope.$eval(onSuccess)) {
                    modal.close();
                }
            });
            dialog.on('hide', function () {
                if (onCancel) {
                    scope.$eval(onCancel);
                }
            });
            dialog.find("#cancel").click(function () {
                modal.close();
            });
            modal.compileAndRun(dialog, scope, options);
        });
    };

    modal.close = function () {
        var dialog = modal.get();
        if (dialog) {
            dialog.modal('hide');
        }
    };

    return modal;

}]);
