using System;
using System.Data;
using System.Data.SqlClient;
using QC.QDM.Libraries.DataAccessLibrary;

namespace QC.QDM.Libraries.DBAccessLibrary;

public sealed class DataAccessManager
{
	private const string USERNAME = "QDMAppUser";

	private const string PASSWORD = "Qual1234$";

	private static object syncRoot = new object();

	private static volatile DataAccess dataAccessor;

	private static DBProperties databaseProperties;

	public static DataAccess DataAccessor
	{
		get
		{
			if (dataAccessor == null)
			{
				lock (syncRoot)
				{
					if (dataAccessor == null)
					{
						dataAccessor = new DataAccess();
						dataAccessor.UserName = "QDMAppUser";
						dataAccessor.Password = "Qual1234$";
					}
				}
			}
			return dataAccessor;
		}
	}

	public static DBProperties DatabaseProperties
	{
		get
		{
			if (databaseProperties == null)
			{
				lock (syncRoot)
				{
					if (databaseProperties == null)
					{
						databaseProperties = new DBProperties();
					}
				}
			}
			return databaseProperties;
		}
	}

	private DataAccessManager()
	{
	}

	private static void ResetDatabaseProperties()
	{
		databaseProperties = new DBProperties();
	}

	public static DataAccess GetDataAccessor(string serverName, string databaseName)
	{
		DataAccess dataAccess = new DataAccess();
		dataAccess.ServerName = serverName;
		dataAccess.DatabaseName = databaseName;
		dataAccess.UserName = "QDMAppUser";
		dataAccess.Password = "Qual1234$";
		return dataAccess;
	}

	public static bool TestConnection(string dataBaseServer, string database)
	{
		SqlConnection sqlConnection = new SqlConnection();
		try
		{
			sqlConnection.ConnectionString = "workstation id=" + dataBaseServer + ";packet size=4096;user id=" + DataAccessor.UserName + ";data source=" + dataBaseServer + ";persist security info=True;initial catalog=" + database + ";password=" + DataAccessor.Password;
			sqlConnection.Open();
		}
		catch (Exception ex)
		{
			sqlConnection?.Close();
			throw new Exception(ex.Message);
		}
		finally
		{
			sqlConnection?.Close();
		}
		return true;
	}

	public static bool TestConnection(string dataBaseServer, string database, string userName, string password)
	{
		SqlConnection sqlConnection = new SqlConnection();
		try
		{
			sqlConnection.ConnectionString = "workstation id=" + dataBaseServer + ";packet size=4096;user id=" + userName + ";data source=" + dataBaseServer + ";persist security info=True;initial catalog=" + database + ";password=" + password;
			sqlConnection.Open();
		}
		catch (Exception ex)
		{
			sqlConnection?.Close();
			throw new Exception(ex.Message);
		}
		finally
		{
			sqlConnection?.Close();
		}
		return true;
	}

	public static string GetValidColumnName(string colName)
	{
		string text = colName;
		try
		{
			text = colName.Replace("]", "\\]");
		}
		catch
		{
		}
		finally
		{
			text = "[" + text + "]";
		}
		return text;
	}

	public static void GetDatabaseProperties()
	{
		ResetDatabaseProperties();
		try
		{
			DataSet dataSet = DataAccessor.RunStoredProcedure("uspGetDatabaseProperties");
			if (dataSet.Tables[0].Rows.Count <= 0)
			{
				return;
			}
			foreach (DataRow row in dataSet.Tables[0].Rows)
			{
				if (row["NAME".ToUpper()].ToString().ToUpper() == "CLRVERSION")
				{
					try
					{
						DatabaseProperties.DatabaseCLRVersion = row["VALUE".ToUpper()].ToString();
					}
					catch
					{
						DatabaseProperties.DatabaseCLRVersion = "NA";
					}
				}
				if (row["NAME".ToUpper()].ToString().ToUpper() == "SYSTEMNAME")
				{
					try
					{
						DatabaseProperties.SystemName = row["VALUE".ToUpper()].ToString();
					}
					catch
					{
						DatabaseProperties.SystemName = "NA";
					}
				}
				if (row["NAME".ToUpper()].ToString().ToUpper() == "PARAMRESULTEXIST")
				{
					try
					{
						DatabaseProperties.ParamResultExist = Convert.ToBoolean(row["VALUE".ToUpper()]);
					}
					catch
					{
						DatabaseProperties.ParamResultExist = false;
					}
				}
				if (row["NAME".ToUpper()].ToString().ToUpper() == "ISSAMPLINGAVAILABLE")
				{
					try
					{
						DatabaseProperties.IsSamplingAvailable = Convert.ToBoolean(row["VALUE".ToUpper()]);
					}
					catch
					{
						DatabaseProperties.IsSamplingAvailable = false;
					}
				}
				if (row["NAME".ToUpper()].ToString().ToUpper() == "ISROWCOUNTAVAILABLE")
				{
					try
					{
						DatabaseProperties.IsRowCountAvailable = Convert.ToBoolean(row["VALUE".ToUpper()]);
					}
					catch
					{
						DatabaseProperties.IsRowCountAvailable = false;
					}
				}
				if (row["NAME".ToUpper()].ToString().ToUpper() == "ISQMT")
				{
					try
					{
						DatabaseProperties.IsQMT = Convert.ToBoolean(row["VALUE".ToUpper()]);
					}
					catch
					{
						DatabaseProperties.IsQMT = false;
					}
				}
				if (row["NAME".ToUpper()].ToString().ToUpper() == "HASAKEYDATA")
				{
					try
					{
						DatabaseProperties.HasAkeyData = Convert.ToBoolean(row["VALUE".ToUpper()]);
					}
					catch
					{
						DatabaseProperties.HasAkeyData = false;
					}
				}
				if (row["NAME".ToUpper()].ToString().ToUpper() == "ISNEWSCHEMA")
				{
					try
					{
						DatabaseProperties.IsNewSchema = Convert.ToBoolean(row["VALUE".ToUpper()]);
					}
					catch
					{
						DatabaseProperties.IsNewSchema = false;
					}
				}
				if (row["NAME".ToUpper()].ToString().ToUpper() == "SimpleRegistration")
				{
					try
					{
						DatabaseProperties.IsSimpleRegistration = Convert.ToBoolean(row["VALUE".ToUpper()]);
					}
					catch
					{
						DatabaseProperties.IsSimpleRegistration = false;
					}
				}
				if (row["NAME".ToUpper()].ToString().ToUpper() == "StoreXmlFile")
				{
					try
					{
						DatabaseProperties.StoreXmlFile = Convert.ToBoolean(row["VALUE".ToUpper()]);
					}
					catch
					{
						DatabaseProperties.StoreXmlFile = false;
					}
				}
				if (row["NAME".ToUpper()].ToString().ToUpper() == "AUTOFORMATDESCRIPTOR")
				{
					try
					{
						DatabaseProperties.AutoFormatDescriptor = Convert.ToBoolean(row["VALUE".ToUpper()]);
					}
					catch
					{
						DatabaseProperties.AutoFormatDescriptor = false;
					}
				}
			}
		}
		catch
		{
		}
	}
}
