using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using iSTP_Embedded;
using System.IO;

namespace iSTP_DataProcessing
{
    /// <summary>
    /// Class repesents compensatory mod. This class loads all files BatchX during app initialization phase. 
    /// </summary>
    static public class CompensatoryMod
    {
        /// <summary>
        /// Global reference to interface for Compensatory mod
        /// </summary>
        static private ICompensatoryMod 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 Selected;
        /// <summary>
        /// Returns array filled with all csv and dll names
        /// </summary>
        static public string[] GetBatchFilesNames { get { return ((BatchFilesNames != null) ? BatchFilesNames.ToArray() : null); } }
        
        public enum BatchFilesNames_e
        {
            BoschLUT_Batch1
        }

        /// <summary>
        /// Loads all BatchX files and put them in static buffer
        /// </summary>
        static CompensatoryMod()
        {
            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 = "Batch" + (_increment++).ToString();
                        filesInDir = hdDirectoryInWhichToSearch.GetFiles("*" + partialName + "*.csv");

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

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

                        if (csv != string.Empty && dll != string.Empty)
                        {
                            BatchFilesNames.Add(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"))
                Selected = BatchFilesNames_e.BoschLUT_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 (Selected)
            { 
                case BatchFilesNames_e.BoschLUT_Batch1:
                    BatchFile = new BoschCompBatch1(conf);
                    break;
            }

            return BatchFile.SensorsVerification(conf);
        }

        /// <summary>
        /// Calculates new values due to actual packet
        /// </summary>
        /// <param name="rxpck">Actual packet</param>
        /// <returns>The packet with new values</returns>
        public static EmbeddedPacket Recalculate(EmbeddedPacket rxpck)
        {
            float[] _buff = BatchFile.GetCompValues(rxpck);
            EmbeddedPacket _res = new EmbeddedPacket();
            rxpck.CloneTo(_res);

            for (int i = 0; i < _buff.Length; i++)
            {
                _res.SetData(i, _buff[i]);
            }
            _res.CalcCRC32();

            return _res;
        }
    }
}
