using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.CSV;
using Tango.FSE.Common;
using Tango.FSE.Common.Notifications;
using Tango.FSE.Procedures.CSV;
using Tango.FSE.Procedures.Dialogs;
namespace Tango.FSE.Procedures.ViewModels
{
public class ResultsViewVM : FSEViewModel
{
private List<Result> _results;
public List<Result> Results
{
get { return _results; }
set { _results = value; RaisePropertyChangedAuto(); }
}
public RelayCommand<Result> DisplayResultGridCommand { get; set; }
public RelayCommand<Result> ExportResultGridCommand { get; set; }
public ResultsViewVM()
{
TangoIOC.Default.Inject(this);
Results = new List<Result>();
DisplayResultGridCommand = new RelayCommand<Result>(DisplayResultGrid);
ExportResultGridCommand = new RelayCommand<Result>(ExportResultGrid);
}
private void DisplayResultGrid(Result result)
{
if (result != null)
{
ResultGridViewVM vm = new ResultGridViewVM(result);
NotificationProvider.ShowDialog(vm);
}
}
private async void ExportResultGrid(Result result)
{
if (result != null && result.IsValueArray && (result.Value as IList).Count > 0)
{
var r = await StorageProvider.SaveFile("Export procedure result", "CSV Files|*.csv", result.Name + ".csv", ".csv");
if (r.Confirmed)
{
using (NotificationProvider.PushTaskItem("Exporting csv file..."))
{
try
{
await Task.Factory.StartNew(() =>
{
List<Object> values = (result.Value as IEnumerable).Cast<Object>().ToList();
var model = values.First();
if (model.GetType().IsValueTypeOrString())
{
CsvFile<CsvRow> csvFile = new CsvFile<CsvRow>(new CsvDestination(r.SelectedItem), new CsvDefinition() { Columns = new List<String>() { "Values" } });
foreach (var value in values)
{
csvFile.Append(new CsvRow()
{
V0 = value
});
}
csvFile.Dispose();
}
else
{
List<CsvColumn> columns = new List<CsvColumn>();
//prop columns
foreach (var prop in model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
var att = prop.GetCustomAttribute<DescriptionAttribute>();
columns.Add(new CsvColumn()
{
Name = prop.Name,
Description = att != null ? att.Description : prop.Name,
IsProperty = true,
PropertyInfo = prop
});
}
//field columns
foreach (var field in model.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
{
var att = field.GetCustomAttribute<DescriptionAttribute>();
columns.Add(new CsvColumn()
{
Name = field.Name,
Description = att != null ? att.Description : field.Name,
FieldInfo = field
});
}
CsvFile<CsvRow> csvFile = new CsvFile<CsvRow>(new CsvDestination(r.SelectedItem), new CsvDefinition() { Columns = columns.Select(x => x.Description).ToList() });
List<PropertyInfo> rowProps = typeof(CsvRow).GetProperties().ToList();
foreach (var value in values)
{
CsvRow row = new CsvRow();
for (int i = 0; i < columns.Count; i++)
{
var column = columns[i];
rowProps[i].SetValue(row, column.GetValue(value));
}
csvFile.Append(row);
}
csvFile.Dispose();
}
});
NotificationProvider.PushSnackbarItem(MessageType.Success, "CSV file exported", true, "Procedure result was exported to file.\nTap to browse the file location.", TimeSpan.FromSeconds(5), null, () =>
{
StorageProvider.ShowInExplorer(r.SelectedItem);
});
}
catch (Exception ex)
{
LogManager.Log(ex, "Error exporting csv file.");
await NotificationProvider.ShowError($"Error occurred while exporting the csv file.\n{ex.FlattenMessage()}");
}
}
}
}
}
}
}