using ServiceCenter.Logs;
using System.Collections.Concurrent;
using System.ComponentModel;
using WCS.Core;
using LogInfo = ServiceCenter.Logs.LogInfo;

namespace WCS.WorkEngineering.Worlds
{
    /// <summary>
    /// 主世界，所有的系统（交互点）默认在该世界下执行。
    /// 如有系统需独立，请自行增加对应世界
    /// 新增世界应当继承此世界，而不是直接继承World
    /// </summary>
    [Description("主世界")]
    public class MainWorld : World
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWorld()
        {
        }

        /// <summary>
        ///  日志队列
        /// </summary>
        protected ConcurrentQueue<KeyLog> Logs = new ConcurrentQueue<KeyLog>();

        /// <summary>
        ///  世界执行周期间隔
        ///  单位：毫秒
        /// </summary>
        protected override int Interval => 500;

        /// <summary>
        ///  更新前执行,重写改方法后请自行添加执行内容
        ///  执行内容:清空日志队列
        /// </summary>
        protected override void BeforeUpdate()
        {
            // 清空日志队列,确保日志队列中只会有当前周期日志
            Logs.Clear();
        }

        /// <summary>
        /// 更新后执行,重写改方法后请自行添加执行内容
        /// 执行内容:清空日志队列
        /// </summary>
        protected override void AfterUpdate()
        {
            LogHub.WorldPublish(Logs, this.GetType().Name);
        }

        /// <summary>
        ///  异常处理,重写改方法后请自行添加执行内容
        ///  执行内容:Exception as KnownException并添加至日志队列
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="exception"></param>
        /// <exception cref="NotImplementedException"></exception>
        protected override void OnError(Channel channel, Exception exception)
        {
            if (exception is KnownException)
            {
                var ex = exception as KnownException;
                var log = new LogInfo { Level = ex.Level, Type = ErrorTypeEnum.Kown, LogUpLoad = ex.logUpLoad, Message = ex.Message };
                Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
            }
            else
            {
                var log = new LogInfo { Level = LogLevelEnum.High, Type = ErrorTypeEnum.Unkown, LogUpLoad = LogUpLoadEnum.UpLoadWMS, Message = exception.Message };
                Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
            }
        }

        /// <summary>
        ///  日志处理,重写改方法后请自行添加执行内容
        ///  执行内容:LogInfo as KeyLog并添加至日志队列
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="logObj"></param>
        /// <exception cref="NotImplementedException"></exception>
        protected override void OnLog(Channel channel, object logObj)
        {
            var log = (LogInfo)logObj;
            Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
        }

        /// <summary>
        ///  日志处理,重写改方法后请自行添加执行内容
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="msg"></param>
        /// <exception cref="NotImplementedException"></exception>
        protected override void OnInternalLog(Channel channel, string msg)
        {
            var log = new LogInfo { Level = LogLevelEnum.Low, Message = msg };
            if (msg != "开始" && msg != "结束")
            {
                Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
            }
        }

        /// <summary>
        ///  获取日志,重写改方法后请自行添加执行内容
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        protected override IEnumerable<string> GetChannelMsg(Channel channel)
        {
            return Logs.Where(v => v.Channel.ToString() == channel.ToString()).Select(v => v.Log.ToString());
        }
    }
}
