aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.IDE/Themes/Generic.xaml
blob: 6c016ae846c39e98113092805785701947a6a4e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Data.Entity;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.ActionLogs;
using Tango.BL.DTO;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.BL.ValueObjects;
using Tango.Core.Commands;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.ThreadExtensions.Models;
using Tango.SharedUI;
using System.Collections.ObjectModel;

namespace Tango.MachineStudio.ThreadExtensions.ViewModels
{
    public class TestResultViewVM : ViewModel
    {
        private INotificationProvider _notification;
        private IActionLogManager _actionLogManager;

        #region Properties

        private string _threadName;
        /// <summary>
        /// Gets or sets the name of the thread. Using in print
        /// </summary>
        /// <value>
        /// The name of the thread.
        /// </value>
        public string ThreadName
        {
            get { return _threadName; }
            set
            {
                _threadName = value;
                RaisePropertyChangedAuto();
            }
        }


        private bool _isSelected;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is selected.
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; RaisePropertyChangedAuto(); }
        }

        private RmlExtensionTestResult _testResult;

        public RmlExtensionTestResult TestResult
        {
            get { return _testResult; }
            set { _testResult = value;
                RaisePropertyChangedAuto();
                RaisePropertyChanged(nameof(TestResultsFiles));
            }
        }
        
        private ObservableCollection<WashingMaterialColorModel> _colorsToMaterialCollection;

        public ObservableCollection<WashingMaterialColorModel> ColorsToMaterialCollection
        {
            get { return _colorsToMaterialCollection; }
            set { _colorsToMaterialCollection = value;
                    RaisePropertyChangedAuto();
            }
        }



        public List<RmlExtensionTestResultsFile> TestResultsFiles
        {
            get
            {
                return TestResult.RmlExtensionTestResultsFiles.ToList();
            }
        }
        
        public RelayCommand<RmlExtensionTestResultsFile> DeleteCommand { get; set; }
        public RelayCommand<RmlExtensionTestResultsFile> DownLoadFileCommand { get; set; }
        public RelayCommand UploadCommand { get; set; }
        public RelayCommand DownLoadAllCommand { get; set; }
        #endregion


        public TestResultViewVM(INotificationProvider notification, IActionLogManager actionLogManager)
        {
            _notification = notification;
            _actionLogManager = actionLogManager;
            
            UploadCommand = new RelayCommand(UploadFiles);
            DownLoadFileCommand = new RelayCommand<RmlExtensionTestResultsFile>(DownLoadFile);
            DeleteCommand = new RelayCommand<RmlExtensionTestResultsFile>(DeleteFile);
            DownLoadAllCommand = new RelayCommand(DownLoadAllFiles);
            
        }

        #region TestResultsFiles
        
        private async void UploadFiles(object obj)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Select data file";
            dlg.Filter = "CSV Files|*.csv";
            dlg.Multiselect = true;
            if (dlg.ShowDialog().Value)
            {
                try
                {
                    var files = dlg.FileNames.ToList();

                    using (ObservablesContext db = ObservablesContext.CreateDefault())
                    {
                        var testResult = await db.RmlExtensionTestResults.Where(x => x.Guid == TestResult.Guid).Include(t1 => t1.RmlExtensionTestResultsFiles).FirstOrDefaultAsync();
                        foreach (var strpath in files)
                        {
                            var testResultfile = new RmlExtensionTestResultsFile();
                            testResultfile.FileName = Path.GetFileName(strpath);
                            //temporary!!!
                            testResultfile.FilePath = strpath;
                            
                           // TestResult.RmlExtensionTestResultsFiles.Add(testResultfile);
                            testResult.RmlExtensionTestResultsFiles.Add(testResultfile);
                        }
                        if (testResult != null)
                        {
                            await db.SaveChangesAsync();
                        }
                        TestResult.RmlExtensionTestResultsFiles = testResult.RmlExtensionTestResultsFiles;///?????
                        RaisePropertyChanged(nameof(TestResultsFiles));
                    }
                    _notification.ShowInfo("File successfully loaded.");
                }
                catch (Exception ex)
                {
                    _notification.ShowError($"An error occurred while trying to import the file.\n{ex.FlattenMessage()}");
                }
            }
           
        }
        
        private void DownLoadFile(RmlExtensionTestResultsFile file)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Save the csv file";
            dlg.Filter = "CSV Files|*.csv";
            dlg.DefaultExt = ".csv";
            dlg.FileName = file.FileName;
            if (dlg.ShowDialog().Value)
            {
                ///
            }
        }

        private void DownLoadAllFiles()
        {
            CommonOpenFileDialog dlg = new CommonOpenFileDialog();
            dlg.Title = "Select folder.";
            dlg.IsFolderPicker = true;
            if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
            {
                var filesPath = TestResult.RmlExtensionTestResultsFiles.Select( x=>x.FilePath).ToList();
                /////
            }
        }

        private async void DeleteFile(RmlExtensionTestResultsFile file)
        {
            if (_notification.ShowQuestion("Are you sure you want to delete the selected file?"))
            {
                using (ObservablesContext db = ObservablesContext.CreateDefault())
                {
                    var deletefile = db.RmlExtensionTestResultsFiles.FirstOrDefault(x => x.Guid == file.Guid);
                    if(deletefile != null)
                    {
                        db.RmlExtensionTestResultsFiles.Remove(deletefile);
                        await db.SaveChangesAsync();
                    }
                }
                TestResult.RmlExtensionTestResultsFiles.Remove(file);
                RaisePropertyChanged(nameof(TestResultsFiles));
            }
        }
        
        #endregion

    }
}