using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericWpfApp.Model
{
   public partial class PassthroughData
   {
      public static int _s_datagridColumnCount = 0;
      // this dictionary stores variable and the info needed to locate it in the passthroughdatamodel dictionary which contains a 2-d array
      private Dictionary<Variables, VariableInfo> _variableEnumToVariableInfoDict = new Dictionary<Variables, VariableInfo>();

      // this dictionary stores variable and its corresponding data model
      private Dictionary<int, ObservableCollection<string>> _rowNumberToPassthroughDataDict = new Dictionary<int, ObservableCollection<string>>();

      public PassthroughData()
      {
         VariableInfo variableInfo = new VariableInfo();

         int dictIndex = 0;
         int colIndex = 0;
         // contruct a 2-d array of strings based on the list of Variables
         foreach (Variables enumVar in Enum.GetValues(typeof(Variables)))
         {
            if (_rowNumberToPassthroughDataDict.Count == 0 || colIndex == _s_datagridColumnCount)
            {
               dictIndex++;
               _rowNumberToPassthroughDataDict[dictIndex] = new ObservableCollection<string>(new string[_s_datagridColumnCount]);
               colIndex = 0;
            }

            // name of the variable
            _rowNumberToPassthroughDataDict[dictIndex][colIndex] = String.Empty;
            variableInfo.passthroughDataModelItemIndex = colIndex++;

            // value of the variable
            _rowNumberToPassthroughDataDict[dictIndex][colIndex++] = String.Empty;

            // save the row index so we can refer to this item
            variableInfo.passthroughDataModelDictIndex = dictIndex;

            // save the column index of this item so we can refer to this item
            _variableEnumToVariableInfoDict[enumVar] = variableInfo;
         }
      }

      public Dictionary<int, ObservableCollection<string>> GetPassthroughDataModelDict()
      {
         return _rowNumberToPassthroughDataDict;
      }

      public void SetValue(Variables variableName, string val)
      {
         App.Current.Dispatcher.Invoke((Action)delegate
         {
            _rowNumberToPassthroughDataDict[_variableEnumToVariableInfoDict[variableName].passthroughDataModelDictIndex][_variableEnumToVariableInfoDict[variableName].passthroughDataModelItemIndex] = _variableEnumToDescriptionDict[variableName];
            _rowNumberToPassthroughDataDict[_variableEnumToVariableInfoDict[variableName].passthroughDataModelDictIndex][_variableEnumToVariableInfoDict[variableName].passthroughDataModelItemIndex + 1] = val;
         });
      }
   }
}
