using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using QC.QsprShared.Trace;
using QC.QTMInterface;

namespace QC.QsprShared.Config;

[Serializable]
public class FeatureConfig
{
	private bool enableSaveAsQSPR2;

	private bool qdartInstall;

	private string logDirectory;

	private string configDirectory;

	private string pluginsDirectory;

	private string traceMsgDirectory;

	private string toolsDirectory;

	private string styleSheetsDirectory;

	private string samplesDirectory;

	private string xlTemplatesDirectory;

	private string tempDirectory;

	private string helpDirectory;

	private string defaultDockingPath;

	private bool disableLabView;

	private bool disableCTS;

	private string securityAccessLevel;

	public bool EnableSaveAsQSPR2
	{
		get
		{
			return enableSaveAsQSPR2;
		}
		set
		{
			enableSaveAsQSPR2 = value;
		}
	}

	public bool QdartInstall
	{
		get
		{
			return qdartInstall;
		}
		set
		{
			qdartInstall = value;
		}
	}

	public string LogDirectory
	{
		get
		{
			return logDirectory;
		}
		set
		{
			logDirectory = value;
		}
	}

	public string ConfigDirectory
	{
		get
		{
			return configDirectory;
		}
		set
		{
			configDirectory = value;
		}
	}

	public string PluginsDirectory
	{
		get
		{
			return pluginsDirectory;
		}
		set
		{
			pluginsDirectory = value;
		}
	}

	public string TraceMsgDirectory
	{
		get
		{
			return traceMsgDirectory;
		}
		set
		{
			traceMsgDirectory = value;
		}
	}

	public string ToolsDirectory
	{
		get
		{
			return toolsDirectory;
		}
		set
		{
			toolsDirectory = value;
		}
	}

	public string StyleSheetsDirectory
	{
		get
		{
			return styleSheetsDirectory;
		}
		set
		{
			styleSheetsDirectory = value;
		}
	}

	public string SamplesDirectory
	{
		get
		{
			return samplesDirectory;
		}
		set
		{
			samplesDirectory = value;
		}
	}

	public string XLTemplatesDirectory
	{
		get
		{
			return xlTemplatesDirectory;
		}
		set
		{
			xlTemplatesDirectory = value;
		}
	}

	public string TempDirectory
	{
		get
		{
			return tempDirectory;
		}
		set
		{
			tempDirectory = value;
		}
	}

	public string HelpDirectory
	{
		get
		{
			return helpDirectory;
		}
		set
		{
			helpDirectory = value;
		}
	}

	public string DefaultDockingPath
	{
		get
		{
			return defaultDockingPath;
		}
		set
		{
			defaultDockingPath = value;
		}
	}

	public bool DisableLabView
	{
		get
		{
			return disableLabView;
		}
		set
		{
			disableLabView = value;
		}
	}

	public bool DisableCTS
	{
		get
		{
			return disableCTS;
		}
		set
		{
			disableCTS = value;
		}
	}

	public string SecurityAccessLevel
	{
		get
		{
			return securityAccessLevel;
		}
		set
		{
			securityAccessLevel = value;
		}
	}

	public FeatureConfig()
	{
		SetDefaultValues();
	}

	public FeatureConfig(bool loadConfigFile)
	{
		try
		{
			SetDefaultValues();
			if (loadConfigFile)
			{
				string text = Path.GetDirectoryName(Application.ExecutablePath) + "\\Feature.config";
				if (!File.Exists(text))
				{
					throw new FieldAccessException("Feature.config doesn't exist.");
				}
				FeatureConfig featureConfig = (FeatureConfig)DeserializeBinObject(text);
				LogDirectory = featureConfig.LogDirectory;
				ConfigDirectory = featureConfig.ConfigDirectory;
				PluginsDirectory = featureConfig.PluginsDirectory;
				TraceMsgDirectory = featureConfig.TraceMsgDirectory;
				ToolsDirectory = featureConfig.ToolsDirectory;
				StyleSheetsDirectory = featureConfig.StyleSheetsDirectory;
				SamplesDirectory = featureConfig.SamplesDirectory;
				XLTemplatesDirectory = featureConfig.XLTemplatesDirectory;
				TempDirectory = featureConfig.TempDirectory;
				HelpDirectory = featureConfig.HelpDirectory;
				DefaultDockingPath = featureConfig.DefaultDockingPath;
				DisableLabView = featureConfig.DisableLabView;
				DisableCTS = featureConfig.DisableCTS;
				SecurityAccessLevel = featureConfig.SecurityAccessLevel;
				QdartInstall = featureConfig.QdartInstall;
				EnableSaveAsQSPR2 = featureConfig.EnableSaveAsQSPR2;
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "LoadFeatureConfig failed, loaded the default values " + ex.Message, TraceMessageType.Exception);
		}
	}

	private void SetDefaultValues()
	{
		LogDirectory = "C:\\Qualcomm\\QSPR\\TestLog\\";
		ConfigDirectory = "C:\\Qualcomm\\QSPR\\QSPRConfigurations\\";
		PluginsDirectory = "..\\QSPRPlugins\\";
		TraceMsgDirectory = "C:\\Qualcomm\\QSPR\\TraceLogs\\";
		ToolsDirectory = "..\\Tools\\";
		StyleSheetsDirectory = "..\\QSPRStyleSheets\\";
		SamplesDirectory = "C:\\Qualcomm\\QSPR\\Samples\\";
		XLTemplatesDirectory = "..\\QSPRXLTemplates\\";
		TempDirectory = "C:\\Qualcomm\\QSPR\\Temp\\";
		HelpDirectory = "..\\Help\\";
		DefaultDockingPath = "QC.QTMApplication.DefaultConfigurations.QTMDocking.config";
		disableLabView = false;
		disableCTS = false;
		SecurityAccessLevel = "";
		QdartInstall = false;
		EnableSaveAsQSPR2 = true;
	}

	public RunTimeConfigInfo ExportToStruct()
	{
		RunTimeConfigInfo result = default(RunTimeConfigInfo);
		result.configDirectory = ConfigDirectory;
		result.helpDirectory = HelpDirectory;
		result.logDirectory = LogDirectory;
		result.pluginsDirectory = PluginsDirectory;
		result.samplesDirectory = SamplesDirectory;
		result.styleSheetsDirectory = StyleSheetsDirectory;
		result.tempDirectory = TempDirectory;
		result.toolsDirectory = ToolsDirectory;
		result.traceMsgDirectory = TraceMsgDirectory;
		result.xlTemplatesDirectory = XLTemplatesDirectory;
		return result;
	}

	public static void SerializeBinObject(object obj, string fileName)
	{
		using Stream serializationStream = new FileStream(fileName, FileMode.Create);
		IFormatter formatter = new BinaryFormatter();
		formatter.Serialize(serializationStream, obj);
	}

	public static object DeserializeBinObject(string fileName)
	{
		using Stream serializationStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
		IFormatter formatter = new BinaryFormatter();
		formatter.Binder = new QtmToQsprBinder();
		return formatter.Deserialize(serializationStream);
	}
}
