using System;
using System.ComponentModel;

namespace FlyADBase
{
    /// <summary>
    /// 同步指令状态
    /// </summary>
    public enum SyncOrderState
    {
        /// <summary>
        /// 等待开始
        /// </summary>
        Waiting,
        /// <summary>
        /// 运行中
        /// </summary>
        Doing,
        /// <summary>
        /// 动作结束
        /// </summary>
        Finish
    }
    /// <summary>
    /// 同步指令基类
    /// </summary>
    public abstract class SyncOrder : INotifyPropertyChanged
    {
        /// <summary>
        /// 状态
        /// </summary>
        public SyncOrderState State { get; set; } = SyncOrderState.Waiting;
        /// <summary>
        /// 标识
        /// </summary>
        public Int32 Marker { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
    }
    /// <summary>
    /// 同步指令-同步运行至逻辑位置
    /// </summary>
    public class SyncOrder_SyncRunAtLC : SyncOrder
    {
        /// <summary>
        /// 
        /// </summary>
        public int Pos2Begin { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int Pos2End { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int Pos1LC { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public bool HasDataGrid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string s;
            if (State == SyncOrderState.Waiting)
                s = "_ ";
            else if (State == SyncOrderState.Doing)
                s = "√ ";
            else// if (State == SyncOrderState.Finish)
                s = "x ";

            return s + "SyncRunAtLC( " +
                "Pos2Begin=" + Pos2Begin.ToString() + ", " +
                "Pos2End=" + Pos2End.ToString() + ", " +
                "Pos1LC=" + Pos1LC.ToString() + ", " +
                "HasDataGrid=" + HasDataGrid.ToString() + ", " +
                "Marker=" + Marker.ToString() + ")";
        }
    }
    /// <summary>
    /// 同步指令-归0
    /// </summary>
    public class SyncOrder_SyncOrigin : SyncOrder
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string s;
            if (State == SyncOrderState.Waiting)
                s = "_ ";
            else if (State == SyncOrderState.Doing)
                s = "√ ";
            else// if (State == SyncOrderState.Finish)
                s = "x ";

            return s + "SyncOrigin( " +
                "Marker=" + Marker.ToString() + ")";
        }

    }
    /// <summary>
    /// 同步指令-运行至
    /// </summary>
    public class SyncOrder_SyncRunTo : SyncOrder
    {
        public int Pos1 { get; set; }
        public UInt32 Velocity { get; set; }
        public bool HasDataGrid { get; set; }
        public override string ToString()
        {
            string s;
            if (State == SyncOrderState.Waiting)
                s = "_ ";
            else if (State == SyncOrderState.Doing)
                s = "√ ";
            else// if (State == SyncOrderState.Finish)
                s = "x ";

            return s + "SyncRunTo( " +
                "Pos1=" + Pos1.ToString() + ", " +
                "Velocity=" + Velocity.ToString() + ", " +
                "HasDataGrid=" + HasDataGrid.ToString() + ", " +
                "Marker=" + Marker.ToString() + ")";
        }

    }

    /// <summary>
    /// 同步指令-运行至逻辑位置
    /// </summary>
    public class SyncOrder_SyncRunToLC : SyncOrder
    {
        /// <summary>
        /// 
        /// </summary>
        public int Pos1 { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public UInt32 Velocity { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public bool HasDataGrid { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string s;
            if (State == SyncOrderState.Waiting)
                s = "_ ";
            else if (State == SyncOrderState.Doing)
                s = "√ ";
            else// if (State == SyncOrderState.Finish)
                s = "x ";

            return s + "SyncRunToLC( " +
                "Pos1=" + Pos1.ToString() + ", " +
                "Velocity=" + Velocity.ToString() + ", " +
                "HasDataGrid=" + HasDataGrid.ToString() + ", " +
                "Marker=" + Marker.ToString() + ")";
        }
    }

    /// <summary>
    /// 同步指令-等待
    /// </summary>
    public class SyncOrder_SyncWait : SyncOrder
    {
        /// <summary>
        /// 
        /// </summary>
        public int MS { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string s;
            if (State == SyncOrderState.Waiting)
                s = "_ ";
            else if (State == SyncOrderState.Doing)
                s = "√ ";
            else// if (State == SyncOrderState.Finish)
                s = "x ";

            return s + "SyncWait( " +
                "MS=" + MS.ToString() + ", " +
                "Marker=" + Marker.ToString() + ")";
        }
    }

    /// <summary>
    /// 同步指令扩展功能
    /// </summary>
    public static class SyncOrderExtention
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <param name="flyad"></param>
        public static void Add(this SyncOrder order, IFlyAD flyad)
        {
            if (order is SyncOrder_SyncRunAtLC)
            {
                ((SyncOrder_SyncRunAtLC)order).Add(flyad);
            }
            else if (order is SyncOrder_SyncOrigin)
            {
                ((SyncOrder_SyncOrigin)order).Add(flyad);
            }
            else if (order is SyncOrder_SyncRunTo)
            {
                ((SyncOrder_SyncRunTo)order).Add(flyad);
            }
            else if (order is SyncOrder_SyncRunToLC)
            {
                ((SyncOrder_SyncRunToLC)order).Add(flyad);
            }
            else if (order is SyncOrder_SyncWait)
            {
                ((SyncOrder_SyncWait)order).Add(flyad);
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <param name="flyad"></param>
        public static void Add(this SyncOrder_SyncRunAtLC order, IFlyAD flyad)
        {
            flyad.SyncRunAtLC(order.Pos2Begin, order.Pos2End, order.Pos1LC, order.HasDataGrid, order.Marker);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <param name="flyad"></param>
        public static void Add(this SyncOrder_SyncOrigin order, IFlyAD flyad)
        {
            flyad.SyncOrigin(order.Marker);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <param name="flyad"></param>
        public static void Add(this SyncOrder_SyncRunTo order, IFlyAD flyad)
        {

            flyad.SyncRunTo(
                order.Pos1, order.Velocity, order.HasDataGrid, order.Marker);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <param name="flyad"></param>
        public static void Add(this SyncOrder_SyncRunToLC order, IFlyAD flyad)
        {

            flyad.SyncRunToLC(
                order.Pos1, order.Velocity, order.HasDataGrid, order.Marker);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <param name="flyad"></param>
        public static void Add(this SyncOrder_SyncWait order, IFlyAD flyad)
        {
            flyad.SyncWait(order.MS, order.Marker);
        }
    }
}
