using System;
using System.Runtime.Serialization;
using Aitex.Core.Account;
using Aitex.Core.Util;

namespace Aitex.Core.RT.Event
{
	[Serializable]
	[DataContract]
	[KnownType(typeof(Credential))]
	public class EventItem
	{
        #region Constructors

        /// <summary>
        /// 构造事件对象的实例。
        /// </summary>
        public EventItem()
        {
        }

        /// <summary>
        /// 构造事件对象的实例。
        /// </summary>
        /// <param name="name">事件名称。</param>
        public EventItem(string name)
            : this(0, "System", name, "", EventLevel.Information, EventType.EventUI_Notify)
        {
        }


        /// <summary>
        /// 构造事件对象的实例。
        /// </summary>
        /// <param name="name">事件名称。</param>
        /// <param name="description">事件描述。</param>
        public EventItem(string name, string description)
            : this(0, "System", name, description, EventLevel.Information, EventType.EventUI_Notify)
        {
        }

        /// <summary>
        /// 构造事件对象的实例。
        /// </summary>
        /// <param name="source">事件来源。</param>
        /// <param name="name">事件名称。</param>
        /// <param name="description">事件描述。</param>
        public EventItem(string source, string name, string description)
            : this(0, source, name, description, EventLevel.Information, EventType.EventUI_Notify)
        {
        }

        /// <summary>
        /// 构造事件对象的实例。
        /// </summary>
        /// <param name="name">事件名称。</param>
        /// <param name="type">事件类型，请参考<see cref="EventType"/>。</param>
        /// <param name="level">事件等级，请参考<see cref="EventLevel"/>。</param>
        public EventItem(string name, EventType type, EventLevel level)
            : this(0, "System", name, "", level, type)
        {
        }

        /// <summary>
        /// 构造事件对象的实例。
        /// </summary>
        /// <param name="source">事件来源。</param>
        /// <param name="name">事件名称。</param>
        /// <param name="description">事件描述。</param>
        /// <param name="level">事件等级，请参考<see cref="EventLevel"/>。</param>
        /// <param name="type">事件类型，请参考<see cref="EventType"/>。</param>
        public EventItem(string source, string name, string description, EventLevel level, EventType type)
            : this(0, source, name, description, level, type)
        {
        }

        /// <summary>
        /// 构造事件对象的实例。
        /// </summary>
        /// <param name="id">事件编号。</param>
        /// <param name="source">事件来源。</param>
        /// <param name="name">事件名称。</param>
        /// <param name="description">事件描述。</param>
        /// <param name="level">事件等级，请参考<see cref="EventLevel"/>。</param>
        /// <param name="type">事件类型，请参考<see cref="EventType"/>。</param>
        public EventItem(int id, string source, string name, string description, EventLevel level, EventType type)
        {
            EventEnum = name;
            Type = type;
            Level = level;
            Source = source;
            Description = description;
            Id = id;
        }


		#endregion

        #region Properties
		
        /// <summary>
        /// 设置或返回事件编号。
        /// </summary>
		[DataMember]
		public int Id { get; set; }

        /// <summary>
        /// 设置或返回事件名称。
        /// </summary>
		[DataMember]
		public string EventEnum { get; set; }

        /// <summary>
        /// 设置或返回事件类型。
        /// </summary>
		[DataMember]
		public EventType Type { get; set; }

        /// <summary>
        /// 设置或返回事件等级。
        /// </summary>
		[DataMember]
		public EventLevel Level { get; set; }

        /// <summary>
        /// 设置或返回事件来源。
        /// </summary>
		[DataMember]
		public string Source { get; set; }

        /// <summary>
        /// 设置或返回事件描述。
        /// </summary>
		[DataMember]
		public string Description { get; set; }

        /// <summary>
        /// 设置或返回事件发生时间。
        /// </summary>
		[DataMember]
		public DateTime OccuringTime { get; set; }

        /// <summary>
        /// 设置或返回事件是否已被响应和处理。
        /// </summary>
		[DataMember]
		public bool IsAcknowledged { get; set; }

        /// <summary>
        /// 设置或返回事件被响应的事件。
        /// </summary>
        [DataMember]
        public DateTime AcknowledgedTime { get; set; }
        
        /// <summary>
        /// 设置或返回事件的详细解释。
        /// </summary>
		[DataMember]
		public string Explaination { get; set; }

        /// <summary>
        /// 设置或返回事件的处理方法描述。
        /// </summary>
		[DataMember]
		public string Solution { get; set; }

        /// <summary>
        /// 设置或返回事件描述的英文版本。
        /// </summary>
		[DataMember]
		public string GlobalDescription_en { get; set; }

        /// <summary>
        /// 设置或返回事件描述的中文版本。
        /// </summary>
        [DataMember]
		public string GlobalDescription_zh { get; set; }

		[DataMember]
		public SerializableDictionary<string, string> DVID { get; set; }

		public SerializableDictionary<string, object> ObjDVID { get; set; }
		
		[DataMember]
		public object Tag { get; set; }

        /// <summary>
        /// 返回事件是否触发。
        /// </summary>
		public bool IsTriggered => !IsAcknowledged;

        #endregion

        #region Methods

        /// <summary>
        /// 创建事件的副本。
        /// </summary>
        /// <returns></returns>
        public EventItem Clone()
		{
			var eventItem = new EventItem
            {
                Description = Description,
                EventEnum = EventEnum,
                Explaination = Explaination,
                Id = Id,
                IsAcknowledged = IsAcknowledged,
                AcknowledgedTime = AcknowledgedTime,
                Level = Level,
                OccuringTime = OccuringTime,
                Solution = Solution,
                Source = Source,
                Type = Type,
                GlobalDescription_en = GlobalDescription_en,
                GlobalDescription_zh = GlobalDescription_zh,
                DVID = DVID,
                ObjDVID = ObjDVID,
                Tag = Tag
            };
            return eventItem;
		}

		/// <summary>
		/// 创建事件的副本。
		/// </summary>
		/// <param name="objDvid"></param>
		/// <returns></returns>
		public EventItem Clone(SerializableDictionary<string, object> objDvid)
		{
			var eventItem = new EventItem
            {
                Description = Description,
                EventEnum = EventEnum,
                Explaination = Explaination,
                Id = Id,
                IsAcknowledged = IsAcknowledged,
                AcknowledgedTime = AcknowledgedTime,
                Level = Level,
                OccuringTime = OccuringTime,
                Solution = Solution,
                Source = Source,
                Type = Type,
                GlobalDescription_en = GlobalDescription_en,
                GlobalDescription_zh = GlobalDescription_zh,
                DVID = DVID,
                ObjDVID = objDvid,
                Tag = Tag
            };
            return eventItem;
		}

        /// <summary>
        /// <inheritdoc cref="Object.ToString"/>
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return $"{Level}, {EventEnum}, {Source}";
        }

        #endregion
    }
}
