using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;
using System.IO;

using LFP_Manager.DataStructure;

namespace LFP_Manager.Function
{
    class csIniControlFunction
    {
        #region [Function] INI File Read Function(Section Setting)
        public static string[] GetIniValue(string Section, string path)
        {
            byte[] ba = new byte[5000];
            uint Flag = GetPrivateProfileSection(Section, ba, 5000, path);
            return Encoding.Default.GetString(ba).Split(new char[1] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
        }
        #endregion

        #region [Function] INI File Read Function(Section and Key Setting)
        public static string GetIniValue(string Section, string key, string path)
        {
            StringBuilder sb = new StringBuilder(500);
            int Flag = GetPrivateProfileString(Section, key, "", sb, 500, path);
            return sb.ToString();
        }
        #endregion

        #region [Function] INI File Read Function(Section, Key, Value, Address Setting)
        public bool SetIniValue(string Section, string Key, string Value, string path)
        {
            return (WritePrivateProfileString(Section, Key, Value, path));
        }
        #endregion

        #region [DLL Function] INI DLL Load
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
        [DllImport("kernel32")]
        public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
        [DllImport("kernel32")]
        public static extern uint GetPrivateProfileInt(string lpAppName, string lpKeName, int nDefault, string lpFileName);
        [DllImport("kernel32")]
        public static extern uint GetPrivateProfileSection(string lpAppName, byte[] lpPairValues, uint nSize, string lpFileName);
        [DllImport("kernel32")]
        public static extern uint GetPrivateProfileSectionNames(byte[] lpSections, uint nSize, string lpFileName);
        #endregion

        public static DateTime Delay(int MS)
        {
            DateTime thisMoment = DateTime.Now;
            TimeSpan duration = new TimeSpan(0, 0, 0, 0, MS);
            DateTime afterMoment = thisMoment.Add(duration);

            while (afterMoment >= thisMoment)
            {
                System.Windows.Forms.Application.DoEvents();

                thisMoment = DateTime.Now;
            }

            return DateTime.Now;
        }

        public static CommConfig IniLoad(string AppPath, CommConfig aConfig)
        {
            string path;
            path = Path.GetDirectoryName(AppPath);

            if (aConfig == null)
            {
                aConfig = new CommConfig();
            }
            if (File.Exists(String.Format("{0}\\CommSet.ini", path)))
            {
                StringBuilder ret = new StringBuilder();
                string InIPath = String.Format("{0}\\CommSet.ini", path);

                // CommType = 0: USBCAN, 1: UARTCAN
                aConfig.CommType = (int)csIniControlFunction.GetPrivateProfileInt("COMM TYPE", "TYPE", 0, InIPath);

                // SNMP Config
                    // IP
                ret.Clear();
                csIniControlFunction.GetPrivateProfileString("SNMP", "IP", "(NONE)", ret, 32, InIPath);
                aConfig.SnmpIP = ret.ToString();
                // Model
                aConfig.SnmpModelIndex = (int)csIniControlFunction.GetPrivateProfileInt("SNMP", "MODEL", 2, InIPath);
                // Module Quantity
                aConfig.SnmpMdQty = (int)csIniControlFunction.GetPrivateProfileInt("SNMP", "MD_QTY", 2, InIPath);

                // CAN Config
                    // DEVICE
                ret.Clear();
                aConfig.CanDevice = (int)csIniControlFunction.GetPrivateProfileInt("CAN", "DEVICE", 0, InIPath);
                    // INDEX
                aConfig.CanIndex = (int)csIniControlFunction.GetPrivateProfileInt("CAN", "INDEX", 0, InIPath);
                    // CAN NO
                aConfig.CanNo = (int)csIniControlFunction.GetPrivateProfileInt("CAN", "CAN_NO", 0, InIPath);
                    // SPEED
                aConfig.CanBaudrate = (int)csIniControlFunction.GetPrivateProfileInt("CAN", "BAUDRATE", 0, InIPath);
                    // MODEL
                aConfig.TargetModelIndex = (int)csIniControlFunction.GetPrivateProfileInt("CAN", "MODEL", 0, InIPath);

                // UART Config
                // PORT
                ret.Clear();
                csIniControlFunction.GetPrivateProfileString("UART", "PORT", "(NONE)", ret, 32, InIPath);
                aConfig.UartPort = ret.ToString();
                // SPEED
                aConfig.UartBaudrate = (int)csIniControlFunction.GetPrivateProfileInt("UART", "BAUDRATE", 0, InIPath);

                // Database Config
                ret.Clear();
                    // Log Period
                aConfig.DbLogPeriod = (int)csIniControlFunction.GetPrivateProfileInt("DATABASE", "LOG_PERIOD", 5, InIPath);

                // CB Test Config
                aConfig.CbTestGap = (int)csIniControlFunction.GetPrivateProfileInt("CB_TEST", "MAX_GAP", 1500, InIPath);
                // CB Test Config
                aConfig.CbTestTime = (int)csIniControlFunction.GetPrivateProfileInt("CB_TEST", "TIME", 3000, InIPath);
            }
            else
            {
                aConfig.CommType = csConstData.CommType.COMM_USBCAN;

                // MODEL
                aConfig.TargetModelIndex = 0;

                // CAN DEVICE
                aConfig.CanDevice = 0;
                // CAN INDEX
                aConfig.CanIndex = 0;
                // CAN NO
                aConfig.CanNo = 0;
                // SPEED
                aConfig.CanBaudrate = 0;

                // UART PORT
                aConfig.UartPort = "";
                // SPEED
                aConfig.UartBaudrate = 0;

                aConfig.DbLogPeriod = 5;

                aConfig.CbTestGap = 1500;
                aConfig.CbTestTime = 3000;
            }

            return aConfig;
        }

        public static void IniSave(string AppPath, CommConfig aConfig)
        {
            string path = System.IO.Path.GetDirectoryName(AppPath);
            string InIPath = String.Format("{0}\\CommSet.ini", path);

            // write ini

            // CommType = 0: SNMP, 1: UARTCAN, 2: USBCAN
            csIniControlFunction.WritePrivateProfileString("COMM TYPE", "TYPE", aConfig.CommType.ToString(), InIPath);

            // SNMP Config
            // IP
            csIniControlFunction.WritePrivateProfileString("SNMP", "IP", aConfig.SnmpIP, InIPath);
            // Model
            csIniControlFunction.WritePrivateProfileString("SNMP", "MODEL", aConfig.SnmpModelIndex.ToString(), InIPath);
            // Module Quantity
            csIniControlFunction.WritePrivateProfileString("SNMP", "MD_QTY", aConfig.SnmpMdQty.ToString(), InIPath);

            // CAN Config
            // DEVICE
            csIniControlFunction.WritePrivateProfileString("CAN", "DEVICE", aConfig.CanDevice.ToString(), InIPath);
            // INDEX
            csIniControlFunction.WritePrivateProfileString("CAN", "INDEX", aConfig.CanIndex.ToString(), InIPath);
            // CAN NO
            csIniControlFunction.WritePrivateProfileString("CAN", "CAN_NO", aConfig.CanNo.ToString(), InIPath);
            // SPEED
            csIniControlFunction.WritePrivateProfileString("CAN", "BAUDRATE", aConfig.CanBaudrate.ToString(), InIPath);
            // MODEL
            csIniControlFunction.WritePrivateProfileString("CAN", "MODEL", aConfig.TargetModelIndex.ToString(), InIPath);

            // UART Config
            // PORT
            csIniControlFunction.WritePrivateProfileString("UART", "PORT", aConfig.UartPort.ToString(), InIPath);
            // SPEED
            csIniControlFunction.WritePrivateProfileString("UART", "BAUDRATE", aConfig.UartBaudrate.ToString(), InIPath);

            // Database Config
            // Log Period
            csIniControlFunction.WritePrivateProfileString("DATABASE", "LOG_PERIOD", aConfig.DbLogPeriod.ToString(), InIPath);

            // CB Test Config
            // MAX GAP
            csIniControlFunction.WritePrivateProfileString("CB_TEST", "MAX_GAP", aConfig.CbTestGap.ToString(), InIPath);
            // TIME
            csIniControlFunction.WritePrivateProfileString("CB_TEST", "TIME", aConfig.CbTestTime.ToString(), InIPath);
        }
    }
}
