(function () {
    'use strict';
    angular.module('uniflash').service('navSrv', navSrv);
    navSrv.$inject = ['$rootScope', '$state', '$http', '$uibModal', 'ufSession', 'ufDS', 'ufDsProgressDialog', 'ufLogger', 'propertiesActionSrv', 'ufTargetSetupSrv', 'ticFileStorage', 'ufSettingsSrv', 'ufPreferencesSrv'];
    function navSrv($rootScope, $state, $http, $uibModal, ufSession, ufDS, ufDsProgressDialog, ufLogger, propertiesActionSrv, ufTargetSetupSrv, ticFileStorage, ufSettingsSrv, ufPreferencesSrv) {
        /* temporary solution to support eval. Need to figure out a better way to do this */
        var scope = $rootScope.$new();
        scope.state = $state;
        scope.ufSession = ufSession;
        scope.ufDS = ufDS;
        scope.ufDsProgressDialog = ufDsProgressDialog;
        scope.propertiesActionSrv = propertiesActionSrv;
        // name of the app in the menu
        var appName = 'UniFlash';
        var appLink = 'uniflash';
        var appLogoPath = 'nav/img/icon_32x32@2x.png';
        var appVersion = '4.2';
        var releaseType = '';
        function getAppVersion() {
            return $http.get('version.txt')
                .then(function (v) {
                appVersion = v.data;
                return appVersion;
            });
        }
        // tell common nav which module to show
        function showModule(module) {
            //if ( module === 'user' ) 	 { return true; }
            if (module === 'tour') {
                return false;
            }
            if (module === 'settings') {
                return true;
            }
            if (module === 'help') {
                return true;
            }
            if (module === 'search') {
                return false;
            }
            if (module === 'about') {
                return true;
            }
        }
        // execute the given function string (for now, the scope object needs to be able to execute the given string)
        function execute(funcStr) {
            if (funcStr) {
                scope.$eval(funcStr);
            }
        }
        function openHelp() {
            var win = window.open('http://processors.wiki.ti.com/index.php/UniFlash_v4_Quick_Guide', '_blank');
            win.focus();
        }
        function openSettingsModal() {
            var modalInstance = $uibModal.open({
                templateUrl: 'nav/settings.template.html',
                controllerAs: 'vm',
                controller: NavSettingsCtrl
            });
            function NavSettingsCtrl() {
                var vm = this;
                vm.ufSettingsSrv = ufSettingsSrv;
                vm.ufPreferencesSrv = ufPreferencesSrv;
                vm.title = 'Settings and Tools';
                vm.ufDS = ufDS;
                vm.ufSession = ufSession;
                vm.dsLogName = ufDS.getLoggingName();
                vm.dsLogStatus = ufDS.getLoggingPath() !== null;
                vm.dsLogMsg = vm.dsLogStatus ? ('DS Logging enabled at: ' + ufDS.getLoggingPath()) : '';
                vm.validateDsLogNameRegex = /^[a-zA-Z0-9_.-]*$/g;
                vm.disableToggleDSLog = function () {
                    if (vm.dsLogName) {
                        return (!vm.dsLogName.match(vm.validateDsLogNameRegex) || (vm.dsLogName.length > 128));
                    }
                    else {
                        return false;
                    }
                };
                vm.toggleDSLog = function () {
                    vm.dsLogStatus = !vm.dsLogStatus;
                    vm.dsLogName = vm.dsLogName ? vm.dsLogName : 'ds.log';
                    vm.busy = ufDS.configureLogging(vm.dsLogStatus, vm.dsLogName);
                    vm.busy
                        .then(function (fullPath) {
                        return vm.dsLogMsg = vm.dsLogStatus ? ('DS Logging enabled at: ' + fullPath) : '';
                    });
                };
                vm.ufLogger = ufLogger;
                vm.downloadGuiLog = function () {
                    console.log(vm);
                    ufLogger.save();
                };
                vm.isConfigured = function () {
                    return _.startsWith($state.current.name, 'main.');
                };
                vm.ufTargetSetupSrv = ufTargetSetupSrv;
            }
        }
        $rootScope.$on('logout.preparelogout', function () {
            return ufSession.saveToTempSession()
                .then(function () {
                $rootScope.$emit('logout.logoutready');
            });
        });
        ticFileStorage.fileExists('ufSettings')
            .then(function (isExists) {
            if (isExists) {
                return ticFileStorage.openFile('ufSettings')
                    .then(function (data) {
                    var jsonData = JSON.parse(data);
                    ufSettingsSrv.autoTargetDetectionSelectValue = jsonData.autoTargetDetectionSelectValue;
                });
            }
        });
        return {
            appName: appName,
            appLink: appLink,
            appLogoPath: appLogoPath,
            getAppVersion: getAppVersion,
            releaseType: releaseType,
            showModule: showModule,
            execute: execute,
            openSettingsModal: openSettingsModal,
            openHelp: openHelp
        };
    }
})();
