/// <summary>
        /// Loads the user configuration.
        /// </summary>
        /// <returns>The UserConfiguration.</returns>
        private UserConfiguration LoadUserConfiguration()
        {
            UserConfiguration configuration;

            var filePath = FileHelpers.GetConfigFilePath(FileNames.UserData);

            if (!File.Exists(filePath))
            {
                var dirinfo = new DirectoryInfo(filePath);

                if (dirinfo.Parent != null && !dirinfo.Parent.Exists)
                {
                    dirinfo.Parent.Create();
                }

                configuration = new UserConfiguration();

                FileHelpers.WriteFileJson(FileNames.UserData, configuration);
            }
            else
            {
                configuration = FileHelpers.ReadFileJson <UserConfiguration>(FileNames.UserData);
            }

            return(configuration);
        }
        /// <summary>
        /// Writes the file json.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="fileData">The file data.</param>
        /// <exception cref="AzureDevOpsMgmt.Exceptions.EmptyIdFoundException">The ID Field is Empty</exception>
        /// <exception cref="EmptyIdFoundException">An input account contained an empty token ID.</exception>
        /// <exception cref="T:System.IO.IOException">An I/O error occurs.</exception>
        /// <exception cref="T:System.IO.DirectoryNotFoundException">The specified path is invalid (for example, it is on an unmapped drive).</exception>
        /// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.</exception>
        public static void WriteFileJson(string fileName, AzureDevOpsAccountCollection fileData)
        {
            if (fileData.PatTokens.Any(p => p.Id == Guid.Empty))
            {
                throw new EmptyIdFoundException(EventMessages.TOKEN_ID_CANNOT_BE_EMPTY_GUID);
            }

            FileHelpers.WriteFileJson(fileName, (object)fileData);
        }
        /// <summary>
        /// Loads the account data.
        /// </summary>
        /// <returns>Account configuration collection.</returns>
        private AzureDevOpsAccountCollection LoadAccountData()
        {
            AzureDevOpsAccountCollection accountData;

            if (!File.Exists(FileHelpers.GetConfigFilePath(FileNames.AccountData)))
            {
                var dirInfo = new DirectoryInfo(FileHelpers.GetConfigFilePath(FileNames.AccountData));

                if (dirInfo.Parent != null && !dirInfo.Parent.Exists)
                {
                    dirInfo.Parent.Create();
                }

                accountData = new AzureDevOpsAccountCollection
                {
                    Accounts  = new ObservableCollection <AzureDevOpsAccount>(),
                    PatTokens = new ObservableCollection <AzureDevOpsPatToken>()
                };

                accountData.Init();

                FileHelpers.WriteFileJson(FileNames.AccountData, accountData);
            }
            else
            {
                accountData = FileHelpers.ReadFileJson <AzureDevOpsAccountCollection>(FileNames.AccountData);
                accountData.Init();

#pragma warning disable 618,612
                if (accountData.Accounts.Any(a => a.TokenId != null))
#pragma warning restore 618,612
                {
                    accountData = this.MigrateTokenIdToTokenList(accountData);
                }
            }

            return(accountData);
        }
        /// <summary>
        /// Repairs the users default settings.
        /// </summary>
        private void ResetUserDefaultSettings()
        {
            var configuration = new UserConfiguration();

            FileHelpers.WriteFileJson(FileNames.UserData, configuration);
        }
        public void OnImport()
        {
            var accountData   = this.LoadAccountData();
            var configuration = this.LoadUserConfiguration();

            AzureDevOpsConfiguration.Config.Accounts      = accountData;
            AzureDevOpsConfiguration.Config.Configuration = configuration;

            if ((configuration.DefaultAccount != null) & (configuration.DefaultProject != null))
            {
                AzureDevOpsAccount  defaultAccount  = null;
                AzureDevOpsPatToken defaultPatToken = null;

                try
                {
                    defaultAccount  = accountData.Accounts.First(a => a.FriendlyName == configuration.DefaultAccount);
                    defaultPatToken = accountData.PatTokens.First(a => defaultAccount.LinkedPatTokens.Contains(a.Id));
                }
                catch (InvalidOperationException ioe) when(ioe.Message == "Sequence contains no matching element")
                {
                    this.ResetUserDefaultSettings();

                    var warningParams = new Dictionary <string, string>()
                    {
                        {
                            "Message",
                            "Corruption encountered well loading user default settings!  Settings have been reset to default (empty) values and will need to be configured again."
                        }
                    };

                    this.InvokePsCommand("Write-Warning", warningParams);
                }

                if (defaultAccount != null && defaultPatToken != null)
                {
                    AzureDevOpsConfiguration.Config.CurrentConnection = new CurrentConnection(defaultAccount, defaultPatToken, configuration.DefaultProject);

                    var outputParams = new Dictionary <string, string>()
                    {
                        {
                            "Message",
                            $"Default Account Settings Loaded Successfully.\r\nAccount Name: {configuration.DefaultAccount}\r\nProject Name: {configuration.DefaultProject}"
                        }
                    };
                    this.InvokePsCommand("Write-Host", outputParams);
                }
            }

            if (!AzureDevOpsConfiguration.Config.Accounts.HasCompleted1905Upgrade)
            {
                foreach (var token in AzureDevOpsConfiguration.Config.Accounts.PatTokens)
                {
                    ModuleStartup.ProcessCredentials(token);
                }

                AzureDevOpsConfiguration.Config.Accounts.HasCompleted1905Upgrade = true;
                FileHelpers.WriteFileJson(FileNames.AccountData, AzureDevOpsConfiguration.Config.Accounts);
            }

            var setVariableParams = new Dictionary <string, object>()
            {
                { "Name", "AzureDevOpsConfiguration" },
                { "Value", AzureDevOpsConfiguration.Config }
            };

            this.InvokePsCommand("Set-Variable", setVariableParams);
        }