<commit_before>﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using MaterialDesignThemes.Wpf;
using Newtonsoft.Json;
using Slamby.SDK.Net;
using Slamby.SDK.Net.Managers;
using Slamby.SDK.Net.Models;
using Slamby.TAU.Enum;
using Slamby.TAU.Helper;
using Slamby.TAU.Model;
using Slamby.TAU.View;

namespace Slamby.TAU.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class ConnectViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the ConnectViewModel class.
        /// </summary>
        public ConnectViewModel()
        {
            _dialogHandler = new DialogHandler();
            Endpoints = new ObservableCollection<ConfigurationWithId>(GlobalStore.Endpoints);
            SelectedIndex = Endpoints.IndexOf(Endpoints.FirstOrDefault(ep => ep.Equals(GlobalStore.SelectedEndpoint)));
            SelectCommand = new RelayCommand(async () =>
            {
                if (SelectedIndex < 0) return;
                ClientResponseWithObject<Status> response = null;
                bool IsSuccessFul = false;
                await
                    _dialogHandler.Show(new ProgressDialog(), "ConnectDialog",
                        async (object s, DialogOpenedEventArgs arg) =>
                        {
                            try
                            {
                                var statusManager = new StatusManager(Endpoints[SelectedIndex]);
                                response = await statusManager.GetStatusAsync();
                                IsSuccessFul = response.IsSuccessful;
                            }
                            catch (Exception exception)
                            {
                                IsSuccessFul = false;
                            }
                            finally
                            {
                                arg.Session.Close();
                            }
                        });


                if (IsSuccessFul)
                {
                    var tauVersion = Assembly.GetExecutingAssembly().GetName().Version;
                    var apiVersion = Version.Parse(response.ApiVersion);
                    var isVersionMismatch = tauVersion.Major != apiVersion.Major || tauVersion.Minor != apiVersion.Minor;
                    object result = null;
                    if (isVersionMismatch)
                    {
                        result = await _dialogHandler.Show(new CommonDialog
                        {
                            DataContext =
                                    new CommonDialogViewModel
                                    {
                                        Buttons = ButtonsEnum.YesNo,
                                        Header = "Warning! Version mismatch.",
                                        Content = new Message($"Api version: {apiVersion}{Environment.NewLine}Tau version:{tauVersion}.{Environment.NewLine}Would you like to continue?")
                                    }
                        }, "ConnectDialog");
                    }
                    if (!isVersionMismatch || (CommonDialogResult)result == CommonDialogResult.Yes)
                    {
                        GlobalStore.SelectedEndpoint = Endpoints[SelectedIndex];
                        await ((ViewModelLocator)App.Current.Resources["Locator"]).EndpointUpdate();
                        var mainVindow = new MainWindow();
                        var connectWindow = App.Current.MainWindow;
                        App.Current.MainWindow = mainVindow;
                        mainVindow.Show();
                        connectWindow.Close();
                    }
                }
                else
                {
                    await
                        _dialogHandler.Show(
                            new CommonDialog
                            {
                                DataContext =
                                    new CommonDialogViewModel
                                    {
                                        Header = "Warning!",
                                        Content = new Message($"Faild to connect to selected endpoint.{Environment.NewLine}{string.Join(Environment.NewLine, response.Errors.Errors)}"),
                                        Buttons = ButtonsEnum.Ok<commit_msg>fixed not existing endpoint crash<commit_after>﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using MaterialDesignThemes.Wpf;
using Newtonsoft.Json;
using Slamby.SDK.Net;
using Slamby.SDK.Net.Managers;
using Slamby.SDK.Net.Models;
using Slamby.TAU.Enum;
using Slamby.TAU.Helper;
using Slamby.TAU.Model;
using Slamby.TAU.View;

namespace Slamby.TAU.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class ConnectViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the ConnectViewModel class.
        /// </summary>
        public ConnectViewModel()
        {
            _dialogHandler = new DialogHandler();
            Endpoints = new ObservableCollection<ConfigurationWithId>(GlobalStore.Endpoints);
            SelectedIndex = Endpoints.IndexOf(Endpoints.FirstOrDefault(ep => ep.Equals(GlobalStore.SelectedEndpoint)));
            SelectCommand = new RelayCommand(async () =>
            {
                if (SelectedIndex < 0) return;
                ClientResponseWithObject<Status> response = null;
                bool IsSuccessFul = false;
                await
                    _dialogHandler.Show(new ProgressDialog(), "ConnectDialog",
                        async (object s, DialogOpenedEventArgs arg) =>
                        {
                            try
                            {
                                var statusManager = new StatusManager(Endpoints[SelectedIndex]);
                                response = await statusManager.GetStatusAsync();
                                IsSuccessFul = response.IsSuccessful;
                            }
                            catch (Exception exception)
                            {
                                IsSuccessFul = false;
                            }
                            finally
                            {
                                arg.Session.Close();
                            }
                        });


                if (IsSuccessFul)
                {
                    var tauVersion = Assembly.GetExecutingAssembly().GetName().Version;
                    var apiVersion = Version.Parse(response.ApiVersion);
                    var isVersionMismatch = tauVersion.Major != apiVersion.Major || tauVersion.Minor != apiVersion.Minor;
                    object result = null;
                    if (isVersionMismatch)
                    {
                        result = await _dialogHandler.Show(new CommonDialog
                        {
                            DataContext =
                                    new CommonDialogViewModel
                                    {
                                        Buttons = ButtonsEnum.YesNo,
                                        Header = "Warning! Version mismatch.",
                                        Content = new Message($"Api version: {apiVersion}{Environment.NewLine}Tau version:{tauVersion}.{Environment.NewLine}Would you like to continue?")
                                    }
                        }, "ConnectDialog");
                    }
                    if (!isVersionMismatch || (CommonDialogResult)result == CommonDialogResult.Yes)
                    {
                        GlobalStore.SelectedEndpoint = Endpoints[SelectedIndex];
                        await ((ViewModelLocator)App.Current.Resources["Locator"]).EndpointUpdate();
                        var mainVindow = new MainWindow();
                        var connectWindow = App.Current.MainWindow;
                        App.Current.MainWindow = mainVindow;
                        mainVindow.Show();
                        connectWindow.Close();
                    }
                }
                else
                {
                    await
                        _dialogHandler.Show(
                            new CommonDialog
                            {
                                DataContext =
                                    new CommonDialogViewModel
                                    {
                                        Header = "Warning!",
                                        Content = new Message(
                                            $"Faild to connect to selected endpoint.{Environment.NewLine}{(response?.Errors == null ? "" : string.Join(Environment.NewLine, response.Errors.Errors))}"),
                                        Buttons = ButtonsEnum.Ok
{'fraction_non_alphanumeric': 0.07710894001470743, 'fraction_numerical': 0.00021010610358230907, 'mean_word_length': 1.1455938697318007, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5098109', 'n_tokens_mistral': 2098, 'n_tokens_neox': 1977, 'n_words': 410}