using System;

namespace MECF.Framework.Common.Device.Bases
{
    /// <summary>
    /// 预设的信号灯塔元件动作。
    /// </summary>
    public class STAction : IComparable, ICloneable
    {
        #region Constructors

        /// <summary>
        /// 信号灯塔元件动作构造函数。
        /// </summary>
        /// <param name="eventName">动作名称。</param>
        /// <param name="light">信号塔组件对象的实例。</param>
        /// <param name="action">信号塔组件输出状态。</param>
        /// <param name="blinkPattern">信号塔组件工作模式。如果传入空值，则自动调用
        /// <see cref="STBlinkPattern.GetDefaultPattern"/>以创建默认工作模式。</param>
        public STAction(string eventName, SignalTowerLightBase light, SignalTowerActions action, STBlinkPattern blinkPattern = null)
        {
            EventName = eventName;
            Light = light;
            Output = action;
            BlinkPattern = blinkPattern ?? STBlinkPattern.GetDefaultPattern();
            IsCycleDone = false;
        }


        #endregion

        #region Properties

        /// <summary>
        /// 动作对应的事件名称。
        /// </summary>
        public string EventName { get; }

        /// <summary>
        /// 返回执行当前动作的信号灯。
        /// </summary>
        public STLightTypes LightType => Light.Type;

        /// <summary>
        /// 返回信号灯实例。
        /// </summary>
        public SignalTowerLightBase Light { get; }

        /// <summary>
        /// 设置或返回信号灯的输出状态。
        /// 支持的状态请参考<see cref="SignalTowerActions"/>枚举。
        /// </summary>
        public SignalTowerActions Output { get; set; }
        
        /// <summary>
        /// 设置或返回信号灯闪烁模式。
        /// </summary>
        public STBlinkPattern BlinkPattern { get; set; }

        /// <summary>
        /// 设置或返回是否循环结束。
        /// </summary>
        public bool IsCycleDone { get; set; }

        #endregion

        #region Methods

        /// <inheritdoc />
        public int CompareTo(object obj)
        {
            if (obj is not STAction target)
                return -1;

            return (BlinkPattern.CompareTo(target.BlinkPattern) == 0 
                    && Output == target.Output
                    && Light == target.Light) 
                ? 0 
                : -1;
        }
        
        /// <inheritdoc />
        public object Clone()
        {
            return new STAction(EventName, Light, Output, (STBlinkPattern)BlinkPattern.Clone());
        }

        /// <inheritdoc/>
        public override string ToString()
        {
            return $"{Light}, {Output}";    
        }

        #endregion

    }
}
