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

using Microsoft.Win32;
using System.IO.Ports;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
namespace WpfApplication19
{
  
    class Utils
    {
        
        static public Dictionary<string, string> BuildPortNameHash(string[] portsToMap)
        {
            Dictionary<string, string> oReturnTable = new Dictionary<string, string>();
            MineRegistryForPortName("SYSTEM\\CurrentControlSet\\Enum", oReturnTable, portsToMap);
            return oReturnTable;
        }
        static void MineRegistryForPortName(string startKeyPath, Dictionary<string, string> targetMap,
            string[] portsToMap)
        {
            if (targetMap.Count >= portsToMap.Length)
                return;
            using (RegistryKey currentKey = Registry.LocalMachine)
            {
                try
                {
                    using (RegistryKey currentSubKey = currentKey.OpenSubKey(startKeyPath))
                    {
                        string[] currentSubkeys = currentSubKey.GetSubKeyNames();
                        if (currentSubkeys.Contains("Device Parameters") &&
                            startKeyPath != "SYSTEM\\CurrentControlSet\\Enum")
                        {
                            object portName = Registry.GetValue("HKEY_LOCAL_MACHINE\\" +
                                startKeyPath + "\\Device Parameters", "PortName", null);
                            if (portName == null ||
                                portsToMap.Contains(portName.ToString()) == false)
                                return;
                            object friendlyPortName = Registry.GetValue("HKEY_LOCAL_MACHINE\\" +
                                startKeyPath, "FriendlyName", null);
                            string friendlyName = "N/A";
                            if (friendlyPortName != null)
                                friendlyName = friendlyPortName.ToString();
                            if (friendlyName.Contains(portName.ToString()) == false)
                                friendlyName = string.Format("{0} ({1})", friendlyName, portName);
                            targetMap[portName.ToString()] = friendlyName;
                        }
                        else
                        {
                            foreach (string strSubKey in currentSubkeys)
                                MineRegistryForPortName(startKeyPath + "\\" + strSubKey, targetMap, portsToMap);
                        }
                    }
                }
                catch (Exception)
                {
                    //Console.WriteLine("Error accessing key '{0}'.. Skipping..", startKeyPath);
                }
            }
        }
        public static void UpdateSetting(string key, string value)
        {
            Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (oConfig.AppSettings.Settings[key] == null)
            {
                oConfig.AppSettings.Settings.Add(key, value);
            }
            else
            {
                oConfig.AppSettings.Settings[key].Value = value;
            }

            oConfig.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");

        }

        public static string getSetting(string key)
        {
            Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            return oConfig.AppSettings.Settings[key].Value;
        }

        public static SqlConnection createSqlConnection(string configType)
        {
          SqlConnection sqlConn = new SqlConnection();
         
          string cs = Utils.getSetting(configType);
           

          sqlConn.ConnectionString = cs;
          sqlConn.Open();
          return sqlConn;
          
        }
		public static SqlCeConnection createSqlCeConnection(string configType)
		{
			SqlCeConnection sqlConn = new SqlCeConnection();

			string cs = Utils.getSetting(configType);


			sqlConn.ConnectionString = cs;
			sqlConn.Open();
			return sqlConn;

		}
        public static SqlDataReader Query(SqlConnection connection, string sql)
        {
            SqlCommand catCMD = connection.CreateCommand();
            catCMD.CommandText = sql;
            Console.WriteLine(sql);


			
            SqlDataReader reader = catCMD.ExecuteReader();
            
            return reader;
        }
		public static SqlCeDataReader Query(SqlCeConnection connection, string sql)
		{
			SqlCeCommand catCMD = connection.CreateCommand();
			catCMD.CommandText = sql;
			Console.WriteLine(sql);



			SqlCeDataReader reader = catCMD.ExecuteReader();

			return reader;
		}
    }
}
