using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Winsock_Orcas;
using System.Management;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
using System.Net;
using System.Data.SqlClient;
using System.Data;

namespace Project
{
    public static class Pub
    {
        //Variable
        public static Device.Barcode barcode;

        public static UserSetting uSetting;         //user setting
        public static Setting setting;              //global setting
      
        public static arUtil.Log log;               //global logging system
   
        public static DateTime LastInputTime = DateTime.Now;
        public static CResult Result = new CResult();

      
       public static void init()
        {
            FCOMMON.info.Path = Util.CurrentPath;
            if (Util.isLocalApplication())
            {
                FCOMMON.info.Path = @"c:\Amkor\GroupWare\";
                if (!System.IO.Directory.Exists(FCOMMON.info.Path))
                    System.IO.Directory.CreateDirectory(FCOMMON.info.Path);
            }
            FCOMMON.info.CS = Properties.Settings.Default.gwcs;

            //setting
            setting = new Setting(FCOMMON.info.Path.MakeFilePath("Setting", "Setting.xml"));
            setting.Load();

            uSetting = new UserSetting(FCOMMON.info.Path.MakeFilePath("Setting", "UserSet.xml"));
            uSetting.Load();

            //log
            log = new arUtil.Log();

           //clear login info
            FCOMMON.info.Login = new FCOMMON.info.sUserInfo();

            //language
            Lang.Loading(Pub.setting.Language + ".ini");
        }

        public static string MakePasswordEnc(string data)
       {
           var sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
           var buffer = System.Text.Encoding.Default.GetBytes(data);
           var hashbuf = sha1.ComputeHash(buffer);
           var encpass = hashbuf.GetHexString();
           var ta = new dsMSSQLTableAdapters.UsersTableAdapter();
           encpass = encpass.Replace(" ", "");
           return encpass;
       }
       public static void SetFormStatus(ref System.Windows.Forms.Form f, string formid,Boolean read)
       {
           var fi = new System.IO.FileInfo(Util.CurrentPath + "formSetting\\" + formid + ".xml");
           if (fi.Directory.Exists == false) fi.Directory.Create();
           arUtil.XMLHelper xml = new arUtil.XMLHelper(fi.FullName);
           if (!xml.Exist())
           {
               xml.CreateFile();
               if (read) return; //읽기인데 파일이 없으므로 넘어간다.
           }
           if (read)
           {
               var leftStr = xml.get_Data("position", "left");
               var topStr = xml.get_Data("position", "top");
               int l = 0;
               int t = 0;
               if (!int.TryParse(leftStr, out l)) l = 0;
               if (!int.TryParse(topStr, out t)) t = 0;
               if (l != 0 || t != 0)
               {
                   f.Location = new System.Drawing.Point(l, t);
               }

               var wStr = xml.get_Data("size", "width");
               var hStr = xml.get_Data("size", "height");
               int w = 0;
               int h = 0;
               if (!int.TryParse(wStr, out w)) w = 0;
               if (!int.TryParse(hStr, out h)) h = 0;
               if (w != 0 || h != 0)
               {
                   f.Size = new System.Drawing.Size(w, h);
               }
           }
           else
           {
               xml.set_Data("position", "left", f.Left.ToString());
               xml.set_Data("position", "top", f.Top.ToString());
               xml.set_Data("size", "width", f.Width.ToString());
               xml.set_Data("size", "height", f.Height.ToString());
               xml.Save();
           }

       }
        public static void CheckNRegister2(string prgmName, string develop, string prgmVersion)
        {
            if (prgmName.Length > 50) prgmName = prgmName.Substring(0, 50); //길이제한
            var task = Task.Factory.StartNew(() =>
            {
                try
                {
                    string ip = "";
                    string mac = "";
                    //      string prgmName = Application.ProductName;

                    var nif = NetworkInterface.GetAllNetworkInterfaces();
                    var host = Dns.GetHostEntry(Dns.GetHostName());
                    foreach (IPAddress r in host.AddressList)
                    {
                        string str = r.ToString();

                        if (str != "" && str.Substring(0, 3) == "10.")
                        {
                            ip = str;
                            break;
                        }
                    }

                    string rtn = string.Empty;
                    ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'");
                    ManagementObjectSearcher query1 = new ManagementObjectSearcher(oq);
                    foreach (ManagementObject mo in query1.Get())
                    {
                        string[] address = (string[])mo["IPAddress"];
                        if (address[0] == ip && mo["MACAddress"] != null)
                        {
                            mac = mo["MACAddress"].ToString();
                            break;
                        }
                    }

                    if (ip == "" || mac == "")
                    {
                        return;
                    }


                    SqlConnection conn = new SqlConnection("Data Source=10.131.15.18;Initial Catalog=EE;Persist Security Info=True;User ID=eeuser;Password=Amkor123!");
                    conn.Open();
                    string ProcName = "AddPrgmUser2";
                    SqlCommand cmd = new SqlCommand(ProcName, conn);
                    cmd.CommandType = CommandType.StoredProcedure;

                    SqlParameter param = cmd.Parameters.Add("@mac", SqlDbType.NVarChar, 50);
                    param.Value = mac;

                    param = cmd.Parameters.Add("@ip", SqlDbType.NVarChar, 50);
                    param.Value = ip;

                    param = cmd.Parameters.Add("@pgrm", SqlDbType.NVarChar, 50);
                    param.Value = prgmName;

                    param = cmd.Parameters.Add("@develop", SqlDbType.NVarChar, 50);
                    param.Value = develop;

                    param = cmd.Parameters.Add("@pgver", SqlDbType.NVarChar, 50);
                    param.Value = prgmVersion;

                    param = cmd.Parameters.Add("@prgmLogin", SqlDbType.VarChar, 20);
                    param.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                    param = cmd.Parameters.Add("@account", SqlDbType.NVarChar, 50);
                    param.Value = System.Environment.UserName;




                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    Pub.log.AddE(ex.Message);
                }

            });
        }
    }
}
