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

namespace QC.QsprShared.QDARTLogging;

public class LoggingConfigurations
{
	private string logFilePath = "C:\\Qualcomm\\QDART\\LogConfig";

	private string logFileName = "QDART_Logging_Configuration.xml";

	public bool UseGlobalConfiguration()
	{
		if (!File.Exists(Path.Combine(logFilePath, logFileName)))
		{
			return false;
		}
		try
		{
			XDocument xDocument = XDocument.Load(Path.Combine(logFilePath, logFileName));
			string value = xDocument.Root.Element("LogSettings").Element("UseGlobalSettings").Value;
			if (Convert.ToBoolean(value))
			{
				return true;
			}
			return false;
		}
		catch (Exception ex)
		{
			Console.WriteLine(ex.Message);
			return false;
		}
	}

	public string GetLogFilePath(LogConfigInput input)
	{
		string folderPath = GetFolderPath(input);
		string fileName = GetFileName(input);
		return Path.Combine(folderPath, fileName);
	}

	private string GetFolderPath(LogConfigInput input)
	{
		try
		{
			XDocument xDocument = XDocument.Load(Path.Combine(logFilePath, logFileName));
			string value = xDocument.Root.Element("LogSettings").Element("FolderSettings").Element("BaseFolder")
				.Value;
			string value2 = xDocument.Root.Element("LogSettings").Element("FolderSettings").Element("FolderNameTemplate")
				.Value;
			string subFolder = GetSubFolder(value2, input);
			string path = "";
			if (Convert.ToBoolean(xDocument.Root.Element("LogSettings").Element("FolderSettings").Element("CreateFolderForEachLogFlag")
				.Value))
				{
					IEnumerable<XElement> source = xDocument.Root.Element("LogSettings").Element("LogFlags").Elements("LogFlag");
					IEnumerable<XElement> source2 = source.Where((XElement _flag) => _flag.Attribute("name").Value == input.LOG_TYPE);
					if (Convert.ToBoolean(source2.First().Attribute("value").Value))
					{
						path = input.LOG_TYPE;
					}
				}
				return Path.Combine(value, subFolder, path);
			}
			catch
			{
				return "";
			}
		}

		private string GetSubFolder(string subFolderTemplate, LogConfigInput input)
		{
			StringBuilder template = new StringBuilder(subFolderTemplate);
			template = GetReplacedTemplateValue(input, template);
			return template.ToString();
		}

		private string GetFileName(LogConfigInput input)
		{
			try
			{
				XDocument xDocument = XDocument.Load(Path.Combine(logFilePath, logFileName));
				string value = xDocument.Root.Element("LogSettings").Element("FileNameSettings").Element("FileNameTemplate")
					.Value;
				StringBuilder template = new StringBuilder(value);
				template = GetReplacedTemplateValue(input, template);
				return template.ToString();
			}
			catch
			{
				return "";
			}
		}

		public StringBuilder GetReplacedTemplateValue(LogConfigInput input, StringBuilder template)
		{
			template.Replace("%DATE%", input.DATE);
			template.Replace("%DATE_TIME%", input.DATE_TIME);
			template.Replace("%DUT_ID%", input.DUT_INDEX);
			template.Replace("%PCNAME%", input.PCNAME);
			template.Replace("%SN%", input.SN);
			template.Replace("%TECHNOLOGIES%", input.TECHNOLOGIES);
			template.Replace("%TREENAME%", input.TREENAME);
			return template;
		}
	}
