(function () {
    'use strict';
    angular.module('uniflash').directive('ufViewsList', viewsList);
    function viewsList() {
        var directive = {
            templateUrl: 'main/views_list.html',
            restrict: 'E',
            controller: ViewsListController,
            controllerAs: 'vm',
            scope: {}
        };
        return directive;
    }
    var ViewsListController = (function () {
        function ViewsListController(ufMainView, $state, ufContext, $scope, ufConsoleInternal, ufSession) {
            var _this = this;
            this.ufMainView = ufMainView;
            this.$state = $state;
            this.ufContext = ufContext;
            this.$scope = $scope;
            this.ufConsoleInternal = ufConsoleInternal;
            this.ufSession = ufSession;
            this.flattenedViewNodes = [];
            this.setLastSelected = function (viewNode) {
                _this.selectedViewNode.active = false;
                viewNode.active = true;
                _this.selectedViewNode = viewNode;
            };
            this.flattenViews(ufMainView.getMainViewNode().childViewNodes);
            this.initSelectedView();
            $scope.$on('destroy', ufMainView.registerNavPaneListener(this.setLastSelected));
        }
        ViewsListController.prototype.flattenViews = function (viewNodes) {
            var _this = this;
            _.each(viewNodes, function (viewNode) {
                _this.flattenedViewNodes.push(viewNode);
                _this.flattenViews(viewNode.childViewNodes);
            });
        };
        ViewsListController.prototype.initSelectedView = function () {
            var parts = this.$state.current.name.split('.');
            var currentViewName = parts[parts.length - 1];
            var selectedViewNode = _.findWhere(this.flattenedViewNodes, {
                displayName: currentViewName
            });
            if (selectedViewNode.hasChildren()) {
                var coreName = this.ufContext.getCurrentCore().name;
                selectedViewNode = _.findWhere(selectedViewNode.childViewNodes, {
                    displayName: coreName
                });
            }
            this.selectedViewNode = selectedViewNode;
            this.selectedViewNode.active = true;
        };
        ViewsListController.prototype.viewButtonClicked = function (viewNode) {
            this.setLastSelected(viewNode);
            viewNode.clickHandler();
        };
        ViewsListController.prototype.getViewCCS = function (viewNode) {
            if (viewNode.hasChildren()) {
                return 'uf-view-list-entry-parent';
            }
            else {
                return viewNode.active ? 'uf-view-list-entry-active' : 'uf-view-list-entry-default';
            }
        };
        ;
        ViewsListController.prototype.showCurrentView = function (viewNode) {
            var connectionProperties = this.ufSession.queryConfig().connectionProperties;
            var nodeDisplayValue = connectionProperties['ufShow' + viewNode.displayName.replace(/\s+/g, '') + 'View'];
            if ((nodeDisplayValue && nodeDisplayValue.Value === 'false') || !viewNode.showView) {
                _.each(viewNode.childViewNodes, function (childViewNode) {
                    childViewNode.showView = false;
                });
                return false;
            }
            return true;
        };
        ;
        ViewsListController.prototype.isConsoleClosed = function () {
            return this.ufConsoleInternal.isClosed();
        };
        ;
        ViewsListController.prototype.openConsole = function () {
            return this.ufConsoleInternal.open();
        };
        ;
        ViewsListController.prototype.numMessages = function () {
            return this.ufConsoleInternal.getMessages().length;
        };
        ViewsListController.$inject = ['ufMainView', '$state', 'ufContext', '$scope', 'ufConsoleInternal', 'ufSession'];
        return ViewsListController;
    }());
}());
