<commit_before>﻿using ApiTester.Models;
using Common;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ApiTester.Wpf.ViewModels
{
    public partial class MainViewModel: BaseModel
    {
        private static MainViewModel _viewModel = new MainViewModel();
        public static MainViewModel ViewModel {
            get { return _viewModel; }
        }

        public MainViewModel()
        {
            OnPropertyChanged("Configurations");
        }

        private Dictionary<string, Assembly> _assemblies = new Dictionary<string, Assembly>();
        private Dictionary<string, Type> _assemblyTypes = new Dictionary<string, Type>();

        public IEnumerable<apiConfiguration> Configurations
        {
            get
            {
                var files = Directory.GetFiles(ConfigurationManager.AppSettings["ConfigurationPath"], "*.xml")
                    .Where(f => f.EndsWith("apitester.xml"));
                var list = new List<apiConfiguration>();
                
                foreach(var file in files)
                {
                    var configuration = XmlHelper<apiConfiguration>.Load(file);
                    if(!list.Any(x => x.ToString() == configuration.ToString()))
                        list.Add(configuration);
                }

                SelectedConfiguration = list.FirstOrDefault();

                return list;
            }
        }

        private apiConfiguration _selectedConfiguration;
        public apiConfiguration SelectedConfiguration
        {
            get { return _selectedConfiguration; }
            set
            {
                _selectedConfiguration = value;
                if (_selectedConfiguration != null)
                {
                    SelectedHost = _selectedConfiguration.setup.host.FirstOrDefault();
                    SelectedWorkflow = _selectedConfiguration.setup.workflow.FirstOrDefault();
                    if(!_assemblies.ContainsKey(_selectedConfiguration.setup.source))
                    {
                        loadAssembly(_selectedConfiguration.setup.source);
                        foreach(var asm in _selectedConfiguration.assembly)
                        {
                            loadAssembly(asm.name);
                        }
                    }

                    if (string.IsNullOrEmpty(DocumentationPath))
                    {
                        var ofd = new OpenFileDialog
                        {
                            Title = "Select a Swagger File",
                            CheckFileExists = true,
                            DefaultExt = "yaml",
                            InitialDirectory = AppDomain.CurrentDomain.BaseDirectory,
                            Multiselect = false,
                            Filter = "Swagger Files|*.yaml"
                        };
                        if (ofd.ShowDialog().Value)
                        {
                            DocumentationPath = Path.GetDirectoryName(ofd.FileName);
                        }
                    }

                    if (!string.IsNullOrEmpty(DocumentationPath))
                    {
                        if (Directory.Exists(DocumentationPath))
                        {
                            try {
                                var swaggerProjectFile = Path.Combine(DocumentationPath, "swagger.yaml");
                                var swaggerDefPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                    ConfigurationManager.AppSettings["ConfigurationPath"]);
                                var swaggerFile = Path.Combine(swaggerDefPath,
                                   _selectedConfiguration.setup
                                    .name.Split('.').Last().Replace("Controller", string.Empty)
                                    + "-swagger.yaml");
                                if (File.Exists(swaggerFile))
                                {
                                    if (File.Exists(swaggerProjectFile)) File.Delete(swaggerProjectFile);

                                    File.Copy(swaggerFile, swaggerProjectFile);
                                }
                            } catch(Exception) {; }
                        }
                    }
                }
                OnPropertyChanged();
                OnPropertyChanged("HeadersTable");
                OnPropertyChanged("MethodsTable");
                _runTests.RaiseCanExecuteChanged();
            }
        }

        private void loadAssembly(string source)
        {
            _assemblies.Add(source, getAssembly(source));
        }

        private Assembly getAssembly(string source)
        {
            return Assembly.LoadFrom(source);
        }

        public DataTable HeadersTable
        {
            get
            {<commit_msg>Fixed bug on assembly loading<commit_after>﻿using ApiTester.Models;
using Common;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ApiTester.Wpf.ViewModels
{
    public partial class MainViewModel: BaseModel
    {
        private static MainViewModel _viewModel = new MainViewModel();
        public static MainViewModel ViewModel {
            get { return _viewModel; }
        }

        public MainViewModel()
        {
            OnPropertyChanged("Configurations");
        }

        private Dictionary<string, Assembly> _assemblies = new Dictionary<string, Assembly>();
        private Dictionary<string, Type> _assemblyTypes = new Dictionary<string, Type>();

        public IEnumerable<apiConfiguration> Configurations
        {
            get
            {
                var files = Directory.GetFiles(ConfigurationManager.AppSettings["ConfigurationPath"], "*.xml")
                    .Where(f => f.EndsWith("apitester.xml"));
                var list = new List<apiConfiguration>();
                
                foreach(var file in files)
                {
                    var configuration = XmlHelper<apiConfiguration>.Load(file);
                    if(!list.Any(x => x.ToString() == configuration.ToString()))
                        list.Add(configuration);
                }

                SelectedConfiguration = list.FirstOrDefault();

                return list;
            }
        }

        private apiConfiguration _selectedConfiguration;
        public apiConfiguration SelectedConfiguration
        {
            get { return _selectedConfiguration; }
            set
            {
                _selectedConfiguration = value;
                if (_selectedConfiguration != null)
                {
                    SelectedHost = _selectedConfiguration.setup.host.FirstOrDefault();
                    SelectedWorkflow = _selectedConfiguration.setup.workflow.FirstOrDefault();
                    if(!_assemblies.ContainsKey(_selectedConfiguration.setup.source))
                    {
                        loadAssembly(_selectedConfiguration.setup.source);
                        foreach(var asm in _selectedConfiguration.assembly)
                        {
                            loadAssembly(asm.name);
                        }
                    }

                    if (string.IsNullOrEmpty(DocumentationPath))
                    {
                        var ofd = new OpenFileDialog
                        {
                            Title = "Select a Swagger File",
                            CheckFileExists = true,
                            DefaultExt = "yaml",
                            InitialDirectory = AppDomain.CurrentDomain.BaseDirectory,
                            Multiselect = false,
                            Filter = "Swagger Files|*.yaml"
                        };
                        if (ofd.ShowDialog().Value)
                        {
                            DocumentationPath = Path.GetDirectoryName(ofd.FileName);
                        }
                    }

                    if (!string.IsNullOrEmpty(DocumentationPath))
                    {
                        if (Directory.Exists(DocumentationPath))
                        {
                            try {
                                var swaggerProjectFile = Path.Combine(DocumentationPath, "swagger.yaml");
                                var swaggerDefPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                    ConfigurationManager.AppSettings["ConfigurationPath"]);
                                var swaggerFile = Path.Combine(swaggerDefPath,
                                   _selectedConfiguration.setup
                                    .name.Split('.').Last().Replace("Controller", string.Empty)
                                    + "-swagger.yaml");
                                if (File.Exists(swaggerFile))
                                {
                                    if (File.Exists(swaggerProjectFile)) File.Delete(swaggerProjectFile);

                                    File.Copy(swaggerFile, swaggerProjectFile);
                                }
                            } catch(Exception) {; }
                        }
                    }
                }
                OnPropertyChanged();
                OnPropertyChanged("HeadersTable");
                OnPropertyChanged("MethodsTable");
                _runTests.RaiseCanExecuteChanged();
            }
        }

        private void loadAssembly(string source)
        {
            var asm = getAssembly(source);

            if(null != asm)
                _assemblies.Add(source, asm);
        }

        private Assembly getAssembly(string source)
        {
            try {
                return Assembly.LoadFrom(source);
            } catch(Exception)
            {
                return null;
            }
        }

        public DataTable HeadersTable
        {
            get
            {
{'fraction_non_alphanumeric': 0.0752698535080956, 'fraction_numerical': 0.00038550501156515033, 'mean_word_length': 1.2349773853112211, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3941140', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2104, 'n_words': 401}