using System;
using System.Collections.Generic;
using System.ServiceModel;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Key;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.RT.RecipeCenter;
using MECF.Framework.Common.Equipment;
using Aitex.Core.RT.Log;
namespace MECF.Framework.Common.RecipeCenter
{
    public class RecipeService : IRecipeService
    {
        /// 
        ///  Load a recipe from server to client for editing.
        /// 
        /// 
        /// 
        /// 
        public string LoadRecipe(ModuleName chamId, string recipeName)
        {
            LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format("Read {0} Recipe: {1}", chamId, recipeName));
            return RecipeFileManager.Instance.LoadRecipe(chamId.ToString(), recipeName, false);
        }
        /// 
        /// Delete a recipe by recipe name
        /// 
        /// 
        /// 
        /// 
        public bool DeleteRecipe(ModuleName chamId, string recipeName)
        {
            if (CheckSoftwareExpired())
                return false;
            //if (!ChamberOperationService.IsSuperEnter()) return false;
            LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format("Delete {0} Recipe: {1}", chamId, recipeName));
            return RecipeFileManager.Instance.DeleteRecipe(chamId.ToString(), recipeName);
        }
        /// 
        /// Get recipe list in xml format
        /// 
        /// 
        /// 
        /// 
        public string GetXmlRecipeList(ModuleName chamId, bool includingUsedRecipe)
        {
            return RecipeFileManager.Instance.GetXmlRecipeList(chamId.ToString(), includingUsedRecipe);
        }
        /// 
        /// Get recipe data by recipe name
        /// 
        /// 
        /// 
        /// 
        public IEnumerable GetRecipes(ModuleName chamId, bool includingUsedRecipe)
        {
            return RecipeFileManager.Instance.GetRecipes(chamId.ToString(), includingUsedRecipe);
        }
        /// 
        /// Rename recipe
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool RenameRecipe(ModuleName chamId, string oldName, string newName)
        {
            if (CheckSoftwareExpired())
                return false;
            LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format("Rename {0} Recipe {1}为{2}", chamId, oldName, newName));
            //if (!ChamberOperationService.IsSuperEnter()) return false;
            return RecipeFileManager.Instance.RenameRecipe(chamId.ToString(), oldName, newName);
        }
        /// 
        /// delete a recipe folder
        /// 
        /// 
        /// 
        /// 
        public bool DeleteFolder(ModuleName chamId, string folderName)
        {
            if (CheckSoftwareExpired())
                return false;
            //if (!ChamberOperationService.IsSuperEnter()) return false;
            LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format("Delete {0} Recipe Folder: {1}", chamId, folderName));
            return RecipeFileManager.Instance.DeleteFolder(chamId.ToString(), folderName);
        }
        /// 
        /// save recipe content
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool SaveRecipe(ModuleName chamId, string recipeName, string recipeContent)
        {
            if (CheckSoftwareExpired())
                return false;
            LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format("Modify and save {0} Recipe: {1}", chamId, recipeName));
            return RecipeFileManager.Instance.SaveRecipe(chamId.ToString(), recipeName, recipeContent, false, false);
        }
        /// 
        /// move recipe content
        /// 
        /// 
        /// 
        /// 
        public bool MoveRecipeFile(ModuleName chamId, string recipeName, string tragetFolderName)
        {
            if (CheckSoftwareExpired())
                return false;
            LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format("Move {0} recipe :{1}", chamId, recipeName));
            return RecipeFileManager.Instance.MoveRecipeFile(chamId.ToString(), recipeName, tragetFolderName, false, false);
        }
        /// 
        /// save recipe content
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool SaveAsRecipe(ModuleName chamId, string recipeName, string recipeContent)
        {
            if (CheckSoftwareExpired())
                return false;
            LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format("Modify and save as {0} recipe {1}", chamId, recipeName));
            return RecipeFileManager.Instance.SaveAsRecipe(chamId.ToString(), recipeName, recipeContent);
        }
        /// 
        /// create a new recipe folder
        /// 
        /// 
        /// 
        /// 
        public bool CreateFolder(ModuleName chamId, string folderName)
        {
            if (CheckSoftwareExpired())
                return false;
            LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format("Create {0} recipe foler {1}", chamId, folderName));
            return RecipeFileManager.Instance.CreateFolder(chamId.ToString(), folderName);
        }
        /// 
        /// Rename recipe folder name
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool RenameFolder(ModuleName chamId, string oldName, string newName)
        {
            if (CheckSoftwareExpired())
                return false;
            LOG.Write(eEvent.EV_SEQUENCE, ModuleName.System, string.Format("Rename {0} recipe folder {1} to {2}", chamId, oldName, newName));
            return RecipeFileManager.Instance.RenameFolder(chamId.ToString(), oldName, newName);
        }
        private bool CheckSoftwareExpired()
        {
            bool isExpired = KeyManager.Instance.IsExpired;
            if (isExpired)
                LOG.Write(eEvent.WARN_SEQUENCE,ModuleName.System, "Software is expired. Can not do the operation");
            return isExpired;
        }
        /// 
        /// get reactor's recipe format define file
        /// 
        /// 
        /// 
        public string GetRecipeFormatXml(ModuleName chamId)
        {
            return RecipeFileManager.Instance.GetRecipeFormatXml(chamId.ToString());
        }
        /// 
        /// get reactor's template recipe file
        /// 
        /// 
        /// 
        public string GetRecipeTemplate(ModuleName chamId)
        {
            return RecipeFileManager.Instance.GetRecipeTemplate(chamId.ToString());
        }
        /// 
        /// 获取当前反应腔正在运行的Recipe信息
        /// 
        /// 
        /// recipeName + recipeContent
        public Tuple LoadRunTimeRecipeInfo(ModuleName chamId)
        {
            return null;
            //var pm = PmManager.GetReactor(chamId);
            //if (pm != null) return pm.LoadRunTimeRecipeInfo();
            //return Singleton.Instance.GetRecipeInfo();
        }
        public string GetRecipeByBarcode(ModuleName chamId, string barcode)
        {
            return "";
            //return RecipeFileManager.Instance.GetRecipeByBarcode(chamId.ToString(), barcode);
        }
        public string GetXmlSequenceList(ModuleName chamId )
        {
            return RecipeFileManager.Instance.GetXmlSequenceList(chamId.ToString() );
        }
        public string GetSequence(string sequenceName)
        {
            return RecipeFileManager.Instance.GetSequence(sequenceName, true);
        }
        public List GetSequenceNameList()
        {
            return RecipeFileManager.Instance.GetSequenceNameList();
        }
        public bool DeleteSequence(string sequenceName)
        {
            return RecipeFileManager.Instance.DeleteSequence(sequenceName);
        }
        public bool SaveSequence(string sequenceName, string sequenceContent)
        {
            return RecipeFileManager.Instance.SaveSequence(sequenceName, sequenceContent, false);
        }
        public bool SaveAsSequence(string sequenceName, string sequenceContent)
        {
            return RecipeFileManager.Instance.SaveAsSequence(sequenceName, sequenceContent);
        }
        public bool RenameSequence(string oldName, string newName)
        {
            return RecipeFileManager.Instance.RenameSequence(oldName, newName);
        }
        public string GetSequenceFormatXml()
        {
            return RecipeFileManager.Instance.GetSequenceFormatXml();
        }
        public bool RenameSequenceFolder(string oldName, string newName)
        {
            return RecipeFileManager.Instance.RenameSequenceFolder(oldName, newName);
        }
        public bool CreateSequenceFolder(string folderName)
        {
            return RecipeFileManager.Instance.CreateSequenceFolder(folderName);
        }
        public bool DeleteSequenceFolder(string folderName)
        {
            return RecipeFileManager.Instance.DeleteSequenceFolder(folderName);
        }
        #region extended recipe interface
        public string LoadRecipeByPath(string pathName, string recipeName)
        {
            return RecipeFileManager.Instance.LoadRecipe(pathName, recipeName, false);
        }
        public bool DeleteRecipeByPath(string pathName, string recipeName)
        {
            return RecipeFileManager.Instance.DeleteRecipe(pathName, recipeName);
        }
        public string GetXmlRecipeListByPath(string pathName, bool includingUsedRecipe)
        {
            return RecipeFileManager.Instance.GetXmlRecipeList(pathName, includingUsedRecipe);
        }
        public IEnumerable GetRecipesByPath(string pathName, bool includingUsedRecipe)
        {
            return RecipeFileManager.Instance.GetRecipes(pathName, includingUsedRecipe);
        }
        public bool RenameRecipeByPath(string pathName, string oldName, string newName)
        {
            return RecipeFileManager.Instance.RenameRecipe(pathName, oldName, newName);
        }
        public bool DeleteFolderByPath(string pathName, string folderName)
        {
            return RecipeFileManager.Instance.DeleteFolder(pathName, folderName);
        }
        public bool SaveRecipeByPath(string pathName, string recipeName, string recipeContent)
        {
            return RecipeFileManager.Instance.SaveRecipe(pathName, recipeName, recipeContent, false, false);
        }
        public bool SaveAsRecipeByPath(string pathName, string recipeName, string recipeContent)
        {
            return RecipeFileManager.Instance.SaveAsRecipe(pathName, recipeName, recipeContent);
        }
        public bool CreateFolderByPath(string pathName, string folderName)
        {
            return RecipeFileManager.Instance.CreateFolder(pathName, folderName);
        }
        public bool RenameFolderByPath(string pathName, string oldName, string newName)
        {
            return RecipeFileManager.Instance.RenameFolder(pathName, oldName, newName);
        }
        public string GetRecipeFormatXmlByPath(string pathName)
        {
            return RecipeFileManager.Instance.GetRecipeFormatXml(pathName);
        }
        public string GetRecipeTemplateByPath(string pathName)
        {
            return RecipeFileManager.Instance.GetRecipeTemplate(pathName);
        }
        public Tuple LoadRunTimeRecipeInfoByPath(string pathName)
        {
            return null;
        }
        public string GetRecipeByBarcodeByPath(string pathName, string barcode)
        {
            return "";
        }
        #endregion
    }
}