using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XKRS.Common.Interface;
using XKRS.Common.Model.Helper;

namespace XKRS.Common.Factory
{
    public static class ProcessFactory
    {
        /// <summary>
        /// 获取项目中所有流程，即寻找所有带有[Process]的
        /// </summary>
        /// <returns>返回流程名称列表</returns>
        public static List<string> GetProcessCodes()
        {
            var attrs = FactoryHelper.GetAttributeType<ProcessAttribute>().Keys;
            List<string> processCodes = attrs.Select(u => u.ProcessCode).Distinct().ToList();
            return processCodes;
        }
        /// <summary>
        /// 获取StationProcess
        /// </summary>
        /// <param name="processCode">流程代码</param>
        /// <param name="productionCode"></param>
        /// <param name="msg">异常信息</param>
        /// <returns></returns>
        public static IProcess CreateStationProcess(string processCode,string productionCode,out string msg)
        {
            IProcess proc = null;
            msg = "";

            try
            {
                var typeDict = FactoryHelper.GetAttributeType<ProcessAttribute>();
                foreach(KeyValuePair<ProcessAttribute,Type>pair in typeDict)
                {
                    if (typeof(IProcess).IsAssignableFrom(pair.Value) && pair.Key.ProcessCode == processCode)
                    {
                        proc = Activator.CreateInstance(pair.Value, productionCode) as IProcess;
                        break;
                    }
                }
            }
            catch(Exception ex)
            {
                msg = ex.GetExceptionMessage();
            }
            return proc;
        }


    }
}
