using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Aitex.Common.Util;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.RecipeCenter;
using Aitex.Core.RT.SCCore;
using MECF.Framework.Common.Equipment;

namespace MECF.Framework.Common.RecipeCenter
{
	public class DefaultRecipeFileContext : IRecipeFileContext
	{
		public string GetRecipeDefiniton(string chamberType, string module)
		{
			try
			{
                var recipeFormatNme = module.Contains("PM") ? GetRecipeFormat(module) : module;
                string filename = PathManager.GetCfgDir() + $"\\Recipe\\{chamberType}\\{recipeFormatNme}.xml";
                XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(filename);
				return xmlDocument.OuterXml;
			}
			catch (Exception ex)
			{
				LOG.Write(ex);
				return "";
			}
		}

        private string GetRecipeFormat(string module)
        {
            var rName = "PN2";

            var scValue = SC.GetStringValue($"PM.{module}.OperationView");
            if (String.IsNullOrEmpty(scValue))
                return rName;

            return scValue;
        }

		public IEnumerable<string> GetRecipes(string path, bool includingUsedRecipe)
		{
			try
			{
				List<string> list = new List<string>();
				string path2 = PathManager.GetRecipeDir() + path + "\\";
				if (!Directory.Exists(path2))
				{
					return list;
				}
				DirectoryInfo directoryInfo = new DirectoryInfo(path2);
				FileInfo[] files = directoryInfo.GetFiles("*.rcp", SearchOption.AllDirectories);
				FileInfo[] array = files;
				foreach (FileInfo fileInfo in array)
				{
					string text = fileInfo.FullName.Substring(directoryInfo.FullName.Length);
					text = text.Substring(0, text.LastIndexOf('.'));
					if (includingUsedRecipe || (!includingUsedRecipe && !text.Contains("HistoryRecipe\\")))
					{
						list.Add(text);
					}
				}
				return list;
			}
			catch (Exception ex)
			{
				LOG.Write(ex);
				return new List<string>();
			}
		}

		public void PostInfoEvent(string message)
		{
			EV.PostMessage(ModuleName.System.ToString(), EventEnum.GeneralInfo, message);
		}

		public void PostWarningEvent(string message)
		{
			EV.PostMessage(ModuleName.System.ToString(), EventEnum.DefaultWarning, message);
		}

		public void PostAlarmEvent(string message)
		{
			EV.PostMessage(ModuleName.System.ToString(), EventEnum.DefaultAlarm, message);
		}

		public void PostDialogEvent(string message)
		{
			EV.PostNotificationMessage(message);
		}

		public void PostInfoDialogMessage(string message)
		{
			EV.PostMessage(ModuleName.System.ToString(), EventEnum.GeneralInfo, message);
			EV.PostPopDialogMessage(EventLevel.Information, "System Information", message);
		}

		public void PostWarningDialogMessage(string message)
		{
			EV.PostMessage(ModuleName.System.ToString(), EventEnum.GeneralInfo, message);
			EV.PostPopDialogMessage(EventLevel.Warning, "System Warning", message);
		}

		public void PostAlarmDialogMessage(string message)
		{
			EV.PostMessage(ModuleName.System.ToString(), EventEnum.GeneralInfo, message);
			EV.PostPopDialogMessage(EventLevel.Alarm, "System Alarm", message);
		}

		public string GetRecipeTemplate(string chamberId, string module)
        {
			string recipeDefiniton = GetRecipeDefiniton(chamberId, module);
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(recipeDefiniton);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("/Aitex/TableRecipeData");
			return xmlNode.OuterXml;
		}
	}
}
