using FasdCockpitCommunicationDemo;
using System;
using System.Collections.Generic;
using System.Reflection;
using static C4IT.Logging.cLogManager;

namespace C4IT.FASD.Base
{
    public class cF4SDHealthCardJsonRawData
    {
        public class cHealthCardTableColumn
        {
            public string ColumnName { get; set; }
            public object[] Values { get; set; }
        }

        public class cHealthCardTable
        {
            public string Name { get; set; }
            public bool IsStatic { get; set; }
            public DateTime[,] TimeFrames { get; set; }
            public cHealthCardTableColumn[] Columns { get; set; }
        }

        public Guid SampleDataId { get; set; }
        public string SampleDataName { get; set; }
        public cHealthCardTable[] Tables { get; set; }

        public List<cF4SDHealthCardRawData.cHealthCardDetailsTable> DetailsTables { get; set; }

        public List<cF4SDTicket> Tickets { get; set; } = new List<cF4SDTicket>();

        public cF4SDHealthCardRawData GetHealthCardData()
        {
            var CM = MethodBase.GetCurrentMethod();
            LogMethodBegin(CM);

            var output = new cF4SDHealthCardRawData() { Tables = new Dictionary<string, cF4SDHealthCardRawData.cHealthCardTable>() };
            try
            {
                foreach (var table in Tables)
                {
                    if (output.Tables.ContainsKey(table.Name))
                        continue;

                    var outputTable = new cF4SDHealthCardRawData.cHealthCardTable()
                    {
                        Name = table.Name,
                        IsStatic = table.IsStatic,
                        TimeFrames = table.TimeFrames,
                    };

                    outputTable.Columns = new Dictionary<string, cF4SDHealthCardRawData.cHealthCardTableColumn>();

                    foreach (var column in table.Columns)
                    {
                        if (outputTable.Columns.ContainsKey(column.ColumnName))
                            continue;

                        var values = new List<object>();
                        values.AddRange(column.Values);
                        outputTable.Columns.Add(column.ColumnName, new cF4SDHealthCardRawData.cHealthCardTableColumn(outputTable) { ColumnName = column.ColumnName, Values = values });
                    }

                    output.Tables.Add(table.Name, outputTable);
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                LogMethodEnd(CM);
            }

            return output;
        }

        public static cF4SDHealthCardJsonRawData GetHealthCardJsonRawData(cF4SDHealthCardRawData rawData) // simplifies process for excel converter
        {
            var CM = MethodBase.GetCurrentMethod();
            LogMethodBegin(CM);

            var output = new cF4SDHealthCardJsonRawData();

            try
            {
                output.Tables = new cHealthCardTable[rawData.Tables.Count];
                for (int i = 0; i < output.Tables.Length; i++)
                {
                    output.Tables[i] = new cHealthCardTable();
                }

                var outputTableEnumerator = output.Tables.GetEnumerator();

                foreach (var table in rawData.Tables)
                {
                    if (!outputTableEnumerator.MoveNext())
                        continue;

                    var outputTable = outputTableEnumerator.Current as cHealthCardTable;
                    outputTable.Name = table.Key;

                    int tableValueColumnsCount = table.Value.Columns.Count;
                    outputTable.Columns = new cHealthCardTableColumn[tableValueColumnsCount];
                    for (int i = 0; i < outputTable.Columns.Length; i++)
                    {
                        outputTable.Columns[i] = new cHealthCardTableColumn();
                    }

                    var outputTableColumnsEnumerator = outputTable.Columns.GetEnumerator();

                    int maxValueCount = 1;
                    foreach (var column in table.Value.Columns)
                    {
                        if (!outputTableColumnsEnumerator.MoveNext())
                            continue;

                        var outputTableColumn = outputTableColumnsEnumerator.Current as cHealthCardTableColumn;
                        outputTableColumn.ColumnName = column.Key;
                        outputTableColumn.Values = column.Value.Values.ToArray();

                        int? outputTableColumnValuesCount = outputTableColumn.Values?.Length;
                        if (outputTableColumnValuesCount != null)
                            maxValueCount = Math.Max(maxValueCount, outputTableColumnValuesCount.Value);
                    }

                    if (maxValueCount == 1)
                        outputTable.IsStatic = true;

                    outputTable.TimeFrames = new DateTime[maxValueCount, 2];
                    outputTable.TimeFrames[0, 0] = DateTime.UtcNow;
                    outputTable.TimeFrames[0, 1] = DateTime.UtcNow.Date;

                    for (int i = 1; i < outputTable.TimeFrames.GetLength(0); i++)
                    {
                        outputTable.TimeFrames[i, 0] = DateTime.UtcNow.Date.AddDays(-i);
                        outputTable.TimeFrames[i, 1] = DateTime.UtcNow.Date.AddDays(-i - 1);
                    }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
                LogMethodEnd(CM);
            }

            return output;
        }
    }
}