define(function(require) {

    var $ = require("jquery"),
        _ = require("underscore"),
        Dialog = require("common/component/dialog/Dialog"),
        i18n = require("bundle!ImportExportBundle"),

        template = require("text!tenantImportExport/import/template/dependentResourcesDialogTemplate.htm");


    return Dialog.extend({

        events: {
            "resize": "onResizeHeight"
        },

        onResizeHeight: function() {
            this.$contentContainer.height(this.$el.height() - this._resizableContainerShiftHeight);
            this.$(".control.groupBox").css("min-height", this.$contentContainer.height() - this.$(".message").outerHeight(true));
        },

        constructor: function(options) {
            options = options || {};

            this.options = options;

            Dialog.prototype.constructor.call(this, {
                modal: true,
                resizable: options.resizable,
                minWidth: options.minWidth,
                minHeight: options.minHeight,
                additionalCssClasses: options.additionalCssClasses || "dependent-resources-dialog",
                title: options.title || i18n["dialog.broken.dependencies.title"],
                content: "",
                buttons: options.buttons
            });

            this.template = _.template(options.template || template);
        },

        open: function(options) {
            this.setContent(this.template(_.defaults(options, {message: ""})));

            Dialog.prototype.open.apply(this, arguments);

            // refreshes the previous scroll position to default one.
            this.$contentContainer.scrollTop(0);

            this._resizableContainerShiftHeight = this.$(".m-Dialog-footer").outerHeight() + this.$(".m-Dialog-header").outerHeight() + this.$contentContainer.outerHeight() - this.$contentContainer.height();
        }

    });

});
define(function(require) {

    var _ = require("underscore"),
        Dialog = require("common/component/dialog/Dialog"),
        ImportView = require("./ImportView"),
        i18n = require("bundle!CommonBundle"),
        i18n2 = require("bundle!ImportExportBundle"),
        importExportTypesEnum = require("tenantImportExport/export/enum/exportTypesEnum");

    return Dialog.extend({

        constructor: function(options) {

            options || (options = {});
            this.options = options;

            Dialog.prototype.constructor.call(this, {
                model: this.model,
                modal: true,
                resizable: true,
                additionalCssClasses: "tenant-import-dialog",
                content: "",
                buttons: [
                    { label: i18n["button.import"], action: "import", primary: true },
                    { label: i18n["button.cancel"], action: "cancel", primary: false }
                ]
            });

            this.on('button:import', _.bind(this._onImportButtonClick, this));
            this.on('button:cancel', _.bind(this._closeImportDialog, this));
        },

        initialize: function(options) {
            Dialog.prototype.initialize.apply(this, arguments);

            this.importView = new ImportView();
            this.listenTo(this.importView, "import:finished", function(tenantId) {
                this.close();
                this.trigger("import:finished", tenantId);
            });

            this.listenTo(this.importView.model, "validated", function(isValid) {
                isValid ? this.buttons.enable("import") : this.buttons.disable("import");
            }, this);
        },

        openDialog: function(tenant) {
            var title = i18n2["tenant.import.dialog.title"] + " " + tenant.name;

            var type = !tenant.isRoot() ? importExportTypesEnum.TENANT : importExportTypesEnum.ROOT_TENANT;

            tenant.isRoot() && this.addCssClasses("tenant");

            this.setContent(this.importView.render({type: type, tenantId: tenant.id}).$el);
            this.setTitle(title);

            Dialog.prototype.open.apply(this, arguments);

            //TODO: Find out why applyEpoxyBindings and delegateEvents should be called after opening dialog with new content.
            this.importView.applyEpoxyBindings();
            this.importView.delegateEvents();

            this.$el.css("minHeight", this.$el.outerHeight());
        },

        // block default validation handlers
        fieldIsValid: function() {

        },

        fieldIsInvalid: function(error) {

        },

        _closeImportDialog: function() {
            this.$el.css({minHeight: 0, height: "auto", width: "auto"});
            this.close();
        },

        _onImportButtonClick: function() {
            this.importView.doImport();
            this._closeImportDialog();
        }
    });

});