using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using Ion.Sdk.Ddr;
using Ion.Sdk.DemoApp.Framework;
using Ion.Sdk.Idi;

namespace Ion.Sdk.DemoApp.Views.ViewModels.Graph
{
    public class TableGraphViewModel : BindableObject
    {
        public MeasurementGraph Model { get; }

        public DataTable Data { get; }

        public DataView View => Data.AsDataView();

        public TableGraphViewModel(MeasurementGraph model)
        {
            Model = model;

            var data = new DataTable();

            var tableData = new MeasurementGraph.TableData().From(model);

            // add columns
            data.Columns.AddRange((from column in tableData.Columns
                                   select new DataColumn(column.Text)).ToArray());
            // add rows
            foreach (var idiRow in tableData.Rows)
            {
                var row = new object[data.Columns.Count];
                for (int colIndex = 0; colIndex < data.Columns.Count; colIndex++)
                    row[colIndex] = idiRow[colIndex].GetText(Idi.Configuration.DefaultLanguage);

                data.Rows.Add(row);
            }
            Data = data;
        }

    }

    public class MeasurementGraphToTableGraphViewModelConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is MeasurementGraph graph)
                return new TableGraphViewModel(graph);
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is TableGraphViewModel viewModel)
                return viewModel.Model;
            return value;
        }
    }

}
