using System;
using System.Collections.Generic;
using System.Linq;
using iSTP_Embedded;
using System.IO;
using iSTP_DebugConsole;

namespace iSTP_DataProcessing
{
    /// <summary>
    /// Class repesents compensatory mode. This class loads all files BatchX during app initialization phase. 
    /// </summary>
    static public class CompensatoryMode
    {
        /// <summary>
        /// Global reference to interface for Compensatory mode
        /// </summary>
        static private ICompensatoryMode BatchFile;
        /// <summary>
        /// The buffer for all csv and dll names
        /// </summary>
        static private List<string> BatchFilesNames = null;
        /// <summary>
        /// Represents user choice and which class will be alocated.
        /// </summary>
        static private BatchFilesNames_e SelectedBatchFile;
        /// <summary>
        /// Returns array filled with all csv and dll names
        /// </summary>
        static public string[] GetBatchFilesNames { get { return ((BatchFilesNames != null) ? BatchFilesNames.ToArray() : null); } }

        static private Object Locker = new object();
        
        public enum BatchFilesNames_e
        {
            SensorALUT_Batch1
        }

        /// <summary>
        /// Loads all BatchX files and put them in static buffer
        /// </summary>
        static CompensatoryMode()
        {
            if (BatchFilesNames == null)
            {
                BatchFilesNames = new List<string>();
                DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(Directory.GetCurrentDirectory());
                if (hdDirectoryInWhichToSearch.Exists)
                {
                    FileInfo[] filesInDir = null;
                    string csv = string.Empty;
                    string dll = string.Empty;
                    int _increment = 1;
                    do
                    {
                        string partialName = string.Format("Batch{0}", _increment++);
                        filesInDir = hdDirectoryInWhichToSearch.GetFiles(string.Format("*{0}*.csv", partialName));

                        if (filesInDir.Length > 0)
                            csv = filesInDir[0].Name;
                        else
                            csv = string.Empty;
                        filesInDir = hdDirectoryInWhichToSearch.GetFiles(string.Format("*{0}*.dll", partialName));

                        if (filesInDir.Length > 0)
                            dll = filesInDir[0].Name;
                        else
                            dll = string.Empty;

                        if (csv != string.Empty && dll != string.Empty)
                        {
                            BatchFilesNames.Add(string.Format("{0},{1}", csv, dll));
                        }
                        else if (csv != string.Empty)
                        {
                            BatchFilesNames.Add(csv);
                        }
                        else if (dll != string.Empty)
                        {
                            BatchFilesNames.Add(dll);
                        }

                    }
                    while (csv != string.Empty && dll != string.Empty);
                }
            }
        }

        /// <summary>
        /// Saves User choice.
        /// </summary>
        /// <param name="newChoice">repesents which batchX files app will use</param>
        public static void ChangeSelected(string newChoice)
        {
            if (newChoice.Contains("Batch1"))
                SelectedBatchFile = BatchFilesNames_e.SensorALUT_Batch1;
        }

        /// <summary>
        /// Allocates correct class due to previous User choice.
        /// </summary>
        /// <param name="conf">Information about configuration</param>
        public static bool[] LoadFile(ISTPConfiguration conf)
        {
            switch (SelectedBatchFile)
            { 
                case BatchFilesNames_e.SensorALUT_Batch1:
                    BatchFile = new SensorACompBatch1(conf);
                    break;
            }

            return BatchFile.SensorsVerification(conf);
        }

        /// <summary>
        /// Calculates new values due to actual packet
        /// </summary>
        /// <param name="ipacket">Actual packet</param>
        /// <returns>The packet with new values</returns>
        public static EmbeddedPacket Recalculate(EmbeddedPacket ipacket)
        {
            lock (Locker)
            {
                EmbeddedPacket _res = new EmbeddedPacket();
                ipacket.CloneTo(_res);

                try
                {
                    float[] _buff = BatchFile.GetCompValues(ipacket);
 
                    for (int i = 0; i < _buff.Length; i++)
                    {
                        _res.SetData(i, _buff[i]);
                    }

                }
                catch (Exception ex)
                {
                    DebugConsole.WriteLine(ex.ToString());
                }

                return _res;
            }
        }
    }
}
