using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Key;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.RT.ParameterCenter;
using Aitex.Core.RT.RecipeCenter;
using MECF.Framework.Common.CommonData;
using MECF.Framework.Common.Equipment;
namespace MECF.Framework.Common.ParameterCenter
{
    class ParameterService : IParameterService
    {
        /// 
        ///  Load a recipe from server to client for editing.
        /// 
        /// 
        /// 
        /// 
        public string LoadParameter(ModuleName chamId, string parameterName)
        {
            EV.PostInfoLog(chamId.ToString(), string.Format("Read {0} recipe {1}.", chamId, parameterName));
            return ParameterFileManager.Instance.LoadParameter(chamId.ToString(), parameterName, false);
        }
        /// 
        /// Delete a recipe by recipe name
        /// 
        /// 
        /// 
        /// 
        public bool DeleteParameter(ModuleName chamId, string parameterName)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            //if (!ChamberOperationService.IsSuperEnter()) return false;
            EV.PostInfoLog(chamId.ToString(), string.Format("Delete {0} recipe {1}.", chamId, parameterName));
            return ParameterFileManager.Instance.DeleteParameter(chamId.ToString(), parameterName);
        }
        /// 
        /// Get recipe list in xml format
        /// 
        /// 
        /// 
        /// 
        public string GetXmlParameterList(ModuleName chamId, bool includingUsedParameter)
        {
            return ParameterFileManager.Instance.GetXmlParameterList(chamId.ToString(), includingUsedParameter);
        }
        /// 
        /// Get recipe data by recipe name
        /// 
        /// 
        /// 
        /// 
        public IEnumerable GetParameters(ModuleName chamId, bool includingUsedParameter)
        {
            return ParameterFileManager.Instance.GetParameters(chamId.ToString(), includingUsedParameter);
        }
        /// 
        /// Rename recipe
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool RenameParameter(ModuleName chamId, string oldName, string newName)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            EV.PostInfoLog(chamId.ToString(), string.Format("Rename {0} recipe {1} to {2}.", chamId, oldName, newName));
            //if (!ChamberOperationService.IsSuperEnter()) return false;
            return ParameterFileManager.Instance.RenameParameter(chamId.ToString(), oldName, newName);
        }
        public bool BackupParameter(string fileOriginalPath, string fileDestinationPath, bool isSaveLinkParameter, List parameterNames)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            EV.PostInfoLog(fileOriginalPath.ToString(), string.Format("Backup {0} recipe .", fileOriginalPath));
            //if (!ChamberOperationService.IsSuperEnter()) return false;
            return ParameterFileManager.Instance.BackupParameter(fileOriginalPath, fileDestinationPath, isSaveLinkParameter, parameterNames);
        }
      public  bool CheckBackParameterIsLinkParameter(string fileOriginalPath, List parameterNames)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            EV.PostInfoLog(fileOriginalPath.ToString(), string.Format("Backup IsLinkRecipe {0} recipe .", fileOriginalPath));
            return false;// ParameterFileManager.Instance.CheckBackRecipeIsLinkParameter(fileOriginalPath,recipeNames);
        }
        public List RestoreParameterFolderList()
        {
            return ParameterFileManager.Instance.RestoreParameterFolderList();
        }
        public string GetXmlRestoreParameterList(string fileType, bool includingUsedRecipe)
        {
            return ParameterFileManager.Instance.GetXmlRestoreParameterList(fileType, includingUsedRecipe);
        }
        public string LoadRestoreParameter(string pathName, string parameterName)
        {
            EV.PostInfoLog(pathName, string.Format("Read {0} recipe {1}.", pathName, parameterName));
            return ParameterFileManager.Instance.LoadRestoreParameter(pathName, parameterName, false);
        }
        public bool RestoreParameter(string chamId,bool isSaveLink, List parameterNames)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            EV.PostInfoLog(chamId.ToString(), string.Format("Restore {0} recipe .", chamId));
            return ParameterFileManager.Instance.RestoreParameter(chamId.ToString(), isSaveLink,parameterNames);
        }
        public bool SigRestoreParameter(string chamId, List parameterNames)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            EV.PostInfoLog(chamId.ToString(), string.Format("SigRestoreRecipe {0} recipe .", chamId));
            return ParameterFileManager.Instance.SigRestoreParameter(chamId.ToString(), parameterNames);
        }
        /// 
        /// delete a recipe folder
        /// 
        /// 
        /// 
        /// 
        public bool DeleteFolder(ModuleName chamId, string folderName)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            //if (!ChamberOperationService.IsSuperEnter()) return false;
            EV.PostInfoLog(chamId.ToString(), string.Format("Delete {0} recipe folder {1}.", chamId, folderName));
            return ParameterFileManager.Instance.DeleteFolder(chamId.ToString(), folderName);
        }
        /// 
        /// save recipe content
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool SaveParameter(ModuleName chamId, string parameterName, string parameterContent)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            EV.PostInfoLog(chamId.ToString(), string.Format("Modify and save {0} recipe {1}.", chamId, parameterName));
            return ParameterFileManager.Instance.SaveParameter(chamId.ToString(), parameterName, parameterContent, false, false);
        }
        /// 
        /// save recipe content
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool SaveAsParameter(ModuleName chamId, string parameterName, string parameterContent)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            EV.PostInfoLog(chamId.ToString(), string.Format("Modify and save as {0} recipe {1}.", chamId, parameterName));
            return ParameterFileManager.Instance.SaveAsParameter(chamId.ToString(), parameterName, parameterContent);
        }
        /// 
        /// create a new recipe folder
        /// 
        /// 
        /// 
        /// 
        public bool CreateFolder(ModuleName chamId, string folderName)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            EV.PostInfoLog(chamId.ToString(), string.Format("Create {0} recipe foler {1}.", chamId, folderName));
            return ParameterFileManager.Instance.CreateFolder(chamId.ToString(), folderName);
        }
        /// 
        /// Rename recipe folder name
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool RenameFolder(ModuleName chamId, string oldName, string newName)
        {
            if (KeyManager.Instance.IsExpired)
            {
                EV.PostMessage("System", EventEnum.DefaultWarning, "Software is expired. Can not do the operation");
                return false;
            }
            EV.PostInfoLog(chamId.ToString(), string.Format("Rename {0} recipe folder {1} to {2}.", chamId, oldName, newName));
            return ParameterFileManager.Instance.RenameFolder(chamId.ToString(), oldName, newName);
        }
        /// 
        /// get reactor's recipe format define file
        /// 
        /// 
        /// 
        public string GetParameterFormatXml(string chamId)
        {
            return ParameterFileManager.Instance.GetParameterFormatXml(chamId);
        }
        public Dictionary> GetGroupParameterTemplate()
        {
            return ParameterFileManager.Instance.GetGroupParameterTemplate();
        }
        /// 
        /// get reactor's template recipe file
        /// 
        /// 
        /// 
        public string GetParameterTemplate(ModuleName chamId)
        {
            return ParameterFileManager.Instance.GetParameterTemplate(chamId.ToString());
        }
        /// 
        /// 获取当前反应腔正在运行的Recipe信息
        /// 
        /// 
        /// recipeName + recipeContent
        public Tuple LoadRunTimeParameterInfo(ModuleName chamId)
        {
            return null;
            //var pm = PmManager.GetReactor(chamId);
            //if (pm != null) return pm.LoadRunTimeRecipeInfo();
            //return Singleton.Instance.GetRecipeInfo();
        }
        public string GetParameterByBarcode(ModuleName chamId, string barcode)
        {
            return "";
            //return RecipeFileManager.Instance.GetRecipeByBarcode(chamId.ToString(), barcode);
        }
       
        #region extended recipe interface
        public string LoadParameterByPath(string pathName, string recipeName)
        {
            return ParameterFileManager.Instance.LoadParameter(pathName, recipeName, false);
        }
        public bool DeleteParameterByPath(string pathName, string recipeName)
        {
            return ParameterFileManager.Instance.DeleteParameter(pathName, recipeName);
        }
        public string GetXmlParameterListByPath(string pathName, bool includingUsedRecipe)
        {
            return ParameterFileManager.Instance.GetXmlParameterList(pathName, includingUsedRecipe);
        }
        public IEnumerable GetParametersByPath(string pathName, bool includingUsedRecipe)
        {
            return ParameterFileManager.Instance.GetParameters(pathName, includingUsedRecipe);
        }
        public bool RenameParameterByPath(string pathName, string oldName, string newName)
        {
            return ParameterFileManager.Instance.RenameParameter(pathName, oldName, newName);
        }
        public bool DeleteFolderByPath(string pathName, string folderName)
        {
            return ParameterFileManager.Instance.DeleteFolder(pathName, folderName);
        }
        public bool SaveParameterByPath(string pathName, string recipeName, string recipeContent)
        {
            return ParameterFileManager.Instance.SaveParameter(pathName, recipeName, recipeContent, false, false);
        }
        public bool SaveAsParameterByPath(string pathName, string recipeName, string recipeContent)
        {
            return ParameterFileManager.Instance.SaveAsParameter(pathName, recipeName, recipeContent);
        }
        public bool CreateFolderByPath(string pathName, string folderName)
        {
            return ParameterFileManager.Instance.CreateFolder(pathName, folderName);
        }
        public bool RenameFolderByPath(string pathName, string oldName, string newName)
        {
            return ParameterFileManager.Instance.RenameFolder(pathName, oldName, newName);
        }
        public string GetParameterFormatXmlByPath(string pathName)
        {
            return ParameterFileManager.Instance.GetParameterFormatXml(pathName);
        }
        public string GetParameterTemplateByPath(string pathName)
        {
            return ParameterFileManager.Instance.GetParameterTemplate(pathName);
        }
        public Tuple LoadRunTimeParameterInfoByPath(string pathName)
        {
            return null;
        }
        public string GetParameterByBarcodeByPath(string pathName, string barcode)
        {
            return "";
        }
        public List CheckParameter(string prefix, string parameterName)
        {
            ParameterFileManager.Instance.CheckParameter(prefix, parameterName, out List reasons);
            return reasons;
        }
        public bool CheckRecipe(string prefix, string parameterName, out List reasons)
        {
            throw new NotImplementedException();
        }
        public bool GetParameterChecked(string prefix, string parameterName)
        {
            return ParameterFileManager.Instance.GetParameterChecked(prefix, parameterName);
        }
        public List GetFileNodeParameterListByPath(string prefix)
        {
            return ParameterFileManager.Instance.GetFileNodeParameterListByPath(prefix);
        }
        #endregion
    }
}