using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Server
{
    /// <summary>
    /// 设备模式， 
    /// 如：
    /// GM_Origin 归原点
    /// GM_Stop 停止
    /// GM_Backw 后退
    /// GM_Forw 前进
    /// GM_Goto 执行运行指令
    /// GM_Fix 定点
    /// GM_Scan 扫描
    /// GM_ScanCorr 机架修正
    /// GM_SyncScan 同步
    /// GM_GetSample 样品取样
    /// GM_RenZiJiaScan 吹膜.扫描
    /// GM_RenZiJiaFix 吹膜.定点
    /// </summary>
    public interface IGageMode:INotifyPropertyChanged
    {
        /// <summary>
        /// 对应的运行模式枚举
        /// </summary>
        CTRL_STATE GMState { get; }
        /// <summary>
        ///  运行中
        /// </summary>
        bool IsRunning { get; }
        /// <summary>
        /// 开始
        /// </summary>
        /// <returns></returns>
        void Start();
        /// <summary>
        /// 停止
        /// </summary>
        void Stop();
    }
    /// <summary>
    /// 设备模式管理器
    /// 当 GM_XXX.Start() 。后，  GageModeManager 收到通知，
    /// GageModeManager再通知其它GM_XXXn.Stop()
    /// 当 GM_XXX 自己停下来 Stop(), 
    /// GageModeManager 要确保 有一个 设备模式 在 运行
    /// 
    /// </summary>
    public class GageModeManager:INotifyPropertyChanged 
    {
        /// <summary>
        /// 当前动作模式
        /// </summary>
        public CTRL_STATE State { get; set; } = CTRL_STATE.FIX;

        /// <summary>
        /// 第1个GM，就是默认值
        /// </summary>
        List<IGageMode> GMs = new List< IGageMode>();

        /// <summary>
        /// 正在运行的GM
        /// </summary>
        IGageMode GMofRunning=null;

        /// <summary>
        /// 运行模式管理器
        /// </summary>
        public GageModeManager() 
        {

        }
        /// <summary>
        /// 添加到运行模式队列
        /// </summary>
        /// <param name="gm"></param>
        public void AddGM(IGageMode gm) 
        {
            GMs.Add(gm);
            gm.PropertyChanged += gm_PropertyChanged;
        }

        void gm_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            IGageMode gm = sender as IGageMode;

            if (e.PropertyName == "IsRunning") 
            {
                if (gm.IsRunning == true)
                {
                    //GM 启动
                    if (GMofRunning != null)//其中一个GM启动，停止上一次运行的GM
                    {
                        IGageMode g = GMofRunning;
                        GMofRunning = gm;
                        g.Stop();
                    }
                    else
                    {
                       
                        GMofRunning = gm;
                    }
                    //更新当前状态
                    State = gm.GMState;
                }
                else 
                {
                    //GM 停止
                    if (gm == GMofRunning) 
                    {
                        GMofRunning = null;//当前没有GM在运行

                        GMs[0].Start();//停止的GM，是当前运行的，启动默认GM
                    }
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
