(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;
    }

    class ViewsListController {

        static $inject = ['ufMainView', '$state', 'ufContext', '$scope', 'ufConsoleInternal', 'ufSession'];
        flattenedViewNodes: uniflash.views.ViewNode[] = [];
        selectedViewNode: uniflash.views.ViewNode;

        constructor(private ufMainView: uniflash.views.MainViewService,
            private $state: angular.ui.IStateService, private ufContext: uniflash.Context,
            private $scope: angular.IScope, private ufConsoleInternal, private ufSession) {
            this.flattenViews(ufMainView.getMainViewNode().childViewNodes);
            this.initSelectedView();
            $scope.$on('destroy', ufMainView.registerNavPaneListener(
                this.setLastSelected
            ));
        }

        private flattenViews(viewNodes: uniflash.views.ViewNode[]) {
            _.each(viewNodes, (viewNode: uniflash.views.ViewNode) => {
                this.flattenedViewNodes.push(viewNode);
                this.flattenViews(viewNode.childViewNodes);
            });
        }

        private initSelectedView() {

            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;
        }

        private setLastSelected = (viewNode: uniflash.views.ViewNode) => {
            this.selectedViewNode.active = false;
            viewNode.active = true;
            this.selectedViewNode = viewNode;
        }


        viewButtonClicked(viewNode: uniflash.views.ViewNode) {
            this.setLastSelected(viewNode);
            viewNode.clickHandler();
        }

        getViewCCS(viewNode: uniflash.views.ViewNode) {
            if (viewNode.hasChildren()) {
                return 'uf-view-list-entry-parent';
            } else {
                return viewNode.active ? 'uf-view-list-entry-active' : 'uf-view-list-entry-default';
            }
        };
        
        showCurrentView(viewNode: uniflash.views.ViewNode) {
            let connectionProperties = this.ufSession.queryConfig().connectionProperties;
            let 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;
        };
        
        isConsoleClosed() {
            return this.ufConsoleInternal.isClosed(); 
        };
        
        openConsole() {
            return this.ufConsoleInternal.open(); 
        };
        
        numMessages() {
            return this.ufConsoleInternal.getMessages().length;
        }

    }

} ());