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;
///
/// Gets or sets the name of the thread. Using in print
///
///
/// The name of the thread.
///
public string ThreadName
{
get { return _threadName; }
set
{
_threadName = value;
RaisePropertyChangedAuto();
}
}
private bool _isSelected;
///
/// Gets or sets a value indicating whether this instance is selected.
///
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 _colorsToMaterialCollection;
public ObservableCollection ColorsToMaterialCollection
{
get { return _colorsToMaterialCollection; }
set { _colorsToMaterialCollection = value;
RaisePropertyChangedAuto();
}
}
public List TestResultsFiles
{
get
{
return TestResult.RmlExtensionTestResultsFiles.ToList();
}
}
public RelayCommand DeleteCommand { get; set; }
public RelayCommand 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(DownLoadFile);
DeleteCommand = new RelayCommand(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
}
}