using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using SystemX.Net.Platform.Common.ExtensionMethods;

using static SystemX.Product.ALIS.UI.Commons;

namespace SystemX.Product.ALIS.UI
{
    public class AccessLevelAlarm : EventArgs
    {
        public AccessLevelAlarm(LoginAccessLevel CurrentAccessLevel, bool bLoginState)
        {
            this.CurrentLevel = CurrentAccessLevel;

            this.GetLoginState = bLoginState;
        }

        public LoginAccessLevel CurrentLevel { get; private set; }

        public bool GetLoginState { get; private set; }
    }

    public static class Commons
    {
        static public bool DEBUG_MODE = false;

        public enum eLookUpOption
        {
            CompareToSame = 0,
            StartWith,
            EndWith,
            ContainWith
        }

        [Flags]
        public enum LoginAccessLevel
        {
            None = 0x01,
            Basic = 0x02,
            Admin = 0x04
        }

        public static bool IsValidEmail(string email)
        {
            bool valid = Regex.IsMatch(email, @"[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?");

            return valid;
        }

        public static bool CheckGateLetter(char letter)
        {
            Regex engRegex = new Regex(@"[A-Z]");

            return engRegex.IsMatch(letter.ToString());
        }

        public static byte[] ConvertHexStringToByte(string convertString)
        {
            byte[] convertArr = new byte[convertString.Length / 2];

            for (int i = 0; i < convertArr.Length; i++)
            {
                convertArr[i] = Convert.ToByte(convertString.Substring(i * 2, 2), 16);
            }

            return convertArr;
        }

        [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int memcmp(byte[] b1, byte[] b2, long count);

        public static bool ByteArrayCompare(byte[] b1, byte[] b2)
        {
            if (b1 == null || b2 == null)
                return false;

            // Validate buffers are the same length.
            // This also ensures that the count does not exceed the length of either buffer. 
            return b1.Length == b2.Length && memcmp(b1, b2, b1.Length) == 0;
        }

        public static T ConvertTextToTryValue<T>(string strText, object objFailValue)
        {
            object obj;
            obj = typeof(T);

            int iGetValue = 0;
            uint uiGetValue = 0;
            double dGetValue = 0;

            if (obj.ToString().IndexOf("Int") >= 0)
            {
                if (!int.TryParse(strText, out iGetValue))
                    obj = objFailValue;
                else
                    obj = iGetValue;
            }
            if (obj.ToString().IndexOf("UInt") >= 0)
            {
                if (!uint.TryParse(strText, out uiGetValue))
                    obj = objFailValue;
                else
                    obj = uiGetValue;
            }
            else if (obj.ToString().IndexOf("Double") >= 0)
            {
                if (!double.TryParse(strText, out dGetValue))
                    obj = objFailValue;
                else
                    obj = dGetValue;
            }

            return (T)Convert.ChangeType(obj, typeof(T));
        }

        public class INICtrl
        {
            public static int MAX_INFORMATION = 10;

            protected static int FILE_ATTRIBUTE_HIDDEN = 2;

            [DllImport("kernel32")]
            protected static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
            [DllImport("kernel32")]
            protected static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
            [DllImport("kernel32")]
            protected static extern int SetFileAttributes(string lpFileName, int dwFileAttributes);

            public virtual void SetValue(string Section, string Key, string Value, string path = "")
            {
                WritePrivateProfileString(Section, Key, Value, path);
            }

            public virtual string GetValue(string Section, string Key, string Default, string path = "")
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, Default, temp, 255, path);
                if (temp != null && temp.Length > 0) return temp.ToString();
                else return Default;
            }
        }

        public class ConnectInfoINICtrl : INICtrl
        {
            private static string ConnectHistoryINIPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\ConnectConfig_PTS.ini"; //@"C:\ConnectConfig_PTS.ini"; //Application.StartupPath + @"\ConnectConfig_PTS.ini";

            public ConnectInfoINICtrl()
            {
                if (File.Exists(ConnectHistoryINIPath) == false)
                {
                    using (File.Create(ConnectHistoryINIPath)) { }

                    SetFileAttributes(ConnectHistoryINIPath, FILE_ATTRIBUTE_HIDDEN);

                    SetValue("LastestConnect", "Info", "");
                }
            }

            public void SetLastestConnectInfo(string strSuccessInfo)
            {
                string strSetUpperText = strSuccessInfo.ToUpper();

                if (strSuccessInfo.CompareTo("") == 0 ||
                   strSuccessInfo.CompareTo("127.0.0.1") == 0 ||
                   strSetUpperText.CompareTo("LOCALHOST") == 0 ||
                   strSuccessInfo.IndexOf("If you do not enter the ip, will be connected to the local area.") >= 0)
                    return;

                SetValue("LastestConnect", "Info", strSuccessInfo);

                string[] strInfoSet = new string[MAX_INFORMATION];

                for (int i = 0; i < MAX_INFORMATION; i++)
                    strInfoSet[i] = GetValue("HistoryConnect" + i.ToString(), "Info", "");

                int? findIdx = strInfoSet.FindIndex(x => x == strSuccessInfo);

                if (findIdx == null)
                {
                    for (int i = MAX_INFORMATION - 1; i > 0; i--)
                    {
                        if (i > 0)
                            strInfoSet[i] = strInfoSet[i - 1];
                    }

                    strInfoSet[0] = strSuccessInfo;

                    for (int i = 0; i < MAX_INFORMATION; i++)
                        SetValue("HistoryConnect" + i.ToString(), "Info", strInfoSet[i]);
                }
            }

            public override void SetValue(string Section, string Key, string Value, string path = "")
            {
                if (path.Length <= 0)
                    path = ConnectHistoryINIPath;

                WritePrivateProfileString(Section, Key, Value, path);
            }

            public override string GetValue(string Section, string Key, string Default, string path = "")
            {
                if (path.Length <= 0)
                    path = ConnectHistoryINIPath;

                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, Default, temp, 255, path);
                if (temp != null && temp.Length > 0) return temp.ToString();
                else return Default;
            }
        }

        public class LoginInfoINICtrl : INICtrl
        {
            private static string LoginHistoryINIPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\LoginConfig.ini";

            public LoginInfoINICtrl()
            {
                if (File.Exists(LoginHistoryINIPath) == false)
                {
                    using (File.Create(LoginHistoryINIPath)) { }

                    SetFileAttributes(LoginHistoryINIPath, FILE_ATTRIBUTE_HIDDEN);

                    SetValue("LastestLogin", "Info", "");
                }
            }

            public void SetLastestLoginInfo(string strSuccessInfo)
            {
                SetValue("LastestLogin", "Info", strSuccessInfo);

                string[] strInfoSet = new string[MAX_INFORMATION];

                for (int i = 0; i < MAX_INFORMATION; i++)
                    strInfoSet[i] = GetValue("HistoryLogin" + i.ToString(), "Info", "");

                int? findIdx = strInfoSet.FindIndex(x => x == strSuccessInfo);

                if (findIdx == null)
                {
                    for (int i = MAX_INFORMATION - 1; i > 0; i--)
                    {
                        if (i > 0)
                            strInfoSet[i] = strInfoSet[i - 1];
                    }

                    strInfoSet[0] = strSuccessInfo;

                    for (int i = 0; i < MAX_INFORMATION; i++)
                        SetValue("HistoryLogin" + i.ToString(), "Info", strInfoSet[i]);
                }
            }

            public override void SetValue(string Section, string Key, string Value, string path = "")
            {
                if (path.Length <= 0)
                    path = LoginHistoryINIPath;

                WritePrivateProfileString(Section, Key, Value, path);
            }

            public override string GetValue(string Section, string Key, string Default, string path = "")
            {
                if (path.Length <= 0)
                    path = LoginHistoryINIPath;

                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, Default, temp, 255, path);
                if (temp != null && temp.Length > 0) return temp.ToString();
                else return Default;
            }
        }
    }
}
