(function () {
    'use strict';
    angular.module('uniflash').directive('ufConsole', consoleDrct);
    function consoleDrct($timeout) {
        var directive = {
            templateUrl: 'console/console.html',
            restrict: 'E',
            controller: ConsoleController,
            controllerAs: 'vm',
            scope: {},
            link: link
        };
        function link(scope, element) {
            scope.$watchCollection('vm.messages', function () {
                var containerDiv = angular.element(element[0].querySelector('.uf-console-body'))[0];
                if (containerDiv) {
                    $timeout(function () {
                        containerDiv.scrollTop = containerDiv.scrollHeight;
                    }, 0, false);
                }
            });
        }
        return directive;
    }
    consoleDrct.$inject = ['$timeout'];
    ConsoleController.$inject = ['$scope', '$timeout', 'ufConsoleInternal', 'ticHighlightSrv', 'ufPropertiesValue', 'ufContext'];
    function ConsoleController($scope, $timeout, ufConsoleInternal, ticHighlightSrv, ufPropertiesValue, ufContext) {
        var vm = this;
        vm.ticHighlightSrv = ticHighlightSrv;
        vm.showConsole = true;
        vm.messages = ufConsoleInternal.getMessages();
        vm.isMSPUSB = (ufContext.$stateParams.connectionXMLFile.indexOf('MSP430-USB') !== -1);
        ufConsoleInternal.addNewMessageListener(function () {
            $timeout(function () {
                vm.showConsole = true;
            }, 0);
        });
        vm.closeConsole = function () {
            ufConsoleInternal.close();
        };
        vm.isClosed = function () {
            return ufConsoleInternal.isClosed();
        };
        vm.clearConsole = function () {
            ufConsoleInternal.clearMessages();
            vm.messages = ufConsoleInternal.getMessages();
        };
        vm.verboseIcon = 'volume_down';
        vm.updateVerbose = function () {
            var verbose = ufPropertiesValue.getValueObj('FlashVerboseMode', ufContext.getCurrentCore().name);
            if (verbose && verbose.value) {
                vm.verboseIcon = 'volume_up';
            }
            else {
                vm.verboseIcon = 'volume_down';
            }
        };
        vm.updateVerbose();
        vm.toggleVerbose = function () {
            var curValueObj = ufPropertiesValue.getValueObj('FlashVerboseMode', ufContext.getCurrentCore().name);
            curValueObj.value = !curValueObj.value;
            curValueObj.modified = true;
            vm.updateVerbose();
        };
        vm.verboseAvailable = function () {
            var curValueObj = ufPropertiesValue.getValueObj('FlashVerboseMode', ufContext.getCurrentCore().name);
            if (vm.isMSPUSB || !curValueObj) {
                return false;
            }
            return true;
        };
        $scope.$on('$stateChangeSuccess', function () {
            vm.updateVerbose();
        });
        $scope.$on('$destroy', function () {
            ufConsoleInternal.addNewMessageListener();
        });
    }
}());
