using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HBLConsole.Interface;
using HBLConsole.Communication;
using System.Threading;
using System.Collections.Concurrent;
using HBLConsole.Model;
using HBLConsole.Attributes;

namespace HBLConsole.MORKD
{
    public class GVL_MORKD : IGvl
    {
        #region 上位机到 --> PLC 控制
        /// <summary>
        /// 初始化控制
        /// </summary>
        public async void InitControl()
        {
            ModbusTcpHelper.GetInstance.Write(320, WriteType.Coils, true);
            await Task.Delay(1000);
            ModbusTcpHelper.GetInstance.Write(320, WriteType.Coils, false);
        }

        /// <summary>
        /// 机器人取面启动
        /// </summary>
        /// <param name="CookNoodleLoc">煮面篮位置</param>
        /// /// <param name="TurntableLoc">转台位置</param>
        public void RBTakeNoodleStart(ushort CookNoodleLoc, ushort TurntableLoc)
        {
            ModbusTcpHelper.GetInstance.Write(101, WriteType.HoldingRegisters, CookNoodleLoc);
            ModbusTcpHelper.GetInstance.Write(100, WriteType.HoldingRegisters, TurntableLoc);
            ModbusTcpHelper.GetInstance.Write(321, WriteType.Coils, true);
        }

        /// <summary>
        /// 机器人出面启动
        /// </summary>
        /// <param name="OutNoodleLoc">出面位置</param>
        /// <param name="BowlLoc">倒面至碗位置</param>
        public void RBOutNoodleStart(ushort OutNoodleLoc, ushort BowlLoc)
        {
            ModbusTcpHelper.GetInstance.Write(102, WriteType.HoldingRegisters, OutNoodleLoc);
            ModbusTcpHelper.GetInstance.Write(103, WriteType.HoldingRegisters, BowlLoc);
            ModbusTcpHelper.GetInstance.Write(322, WriteType.Coils, true);
        }

        /// <summary>
        /// 机器人取浇头启动
        /// </summary>
        /// <param name="SoupLoc">浇头位置</param>
        /// <param name="CutLoc">破口位置</param>
        public void RBTakeSoupStart(ushort SoupLoc, ushort CutLoc)
        {
            AllowTakeSoup = true;
            ModbusTcpHelper.GetInstance.Write(105, WriteType.HoldingRegisters, SoupLoc);
            ModbusTcpHelper.GetInstance.Write(106, WriteType.HoldingRegisters, CutLoc);
            ModbusTcpHelper.GetInstance.Write(323, WriteType.Coils, true);
        }

        /// <summary>
        /// 加热完成取浇头启动
        /// </summary>
        /// <param name="SoupLoc">取浇头完成</param>
        /// <param name="BowlLoc">倒浇头至碗位置</param>
        /// <param name="OutMealLoc">放面至出餐口位置</param>
        public void HeatCompleteTakeSoupStart(ushort SoupLoc, ushort BowlLoc, ushort OutMealLoc)
        {
            AllowTakeSoup = true;
            ModbusTcpHelper.GetInstance.Write(108, WriteType.HoldingRegisters, SoupLoc);
            ModbusTcpHelper.GetInstance.Write(109, WriteType.HoldingRegisters, BowlLoc);
            ModbusTcpHelper.GetInstance.Write(110, WriteType.HoldingRegisters, OutMealLoc);
            ModbusTcpHelper.GetInstance.Write(324, WriteType.Coils, true);
        }

        /// <summary>
        /// 转台启动
        /// </summary>
        /// <param name="TurntableLoc">转台位置</param>
        public void TurntableStart(ushort TurntableLoc)
        {
            this.TurntableLoc = TurntableLoc;
            TurntableInterlock = true;
            TurntableLocLists.Add(TurntableLoc);
            ModbusTcpHelper.GetInstance.Write(100, WriteType.HoldingRegisters, TurntableLoc);
            ModbusTcpHelper.GetInstance.Write(325, WriteType.Coils, true);
        }

        /// <summary>
        /// 下碗启动
        /// </summary>
        /// <param name="AxisLoc">启动轴位置</param>
        /// <param name="BowlType">碗类型位置</param>
        public void DropBowlStart(ushort AxisLoc, ushort BowlType)
        {
            ModbusTcpHelper.GetInstance.Write(113, WriteType.HoldingRegisters, AxisLoc);
            ModbusTcpHelper.GetInstance.Write(114, WriteType.HoldingRegisters, BowlType);
        }

        /// <summary>
        /// 取面完成信号复位
        /// </summary>
        public void TakeNoodleCompleteReset()
        {
            ModbusTcpHelper.GetInstance.Write(1122, WriteType.Coils, false);
        }

        /// <summary>
        /// 出面完成信号复位
        /// </summary>
        public void PutNoodleTakeMealCompleteReset()
        {
            ModbusTcpHelper.GetInstance.Write(1148, WriteType.Coils, false);
        }

        /// <summary>
        /// 取浇头完成信号复位
        /// </summary>
        public void TakeSoupCompleteReset()
        {
            ModbusTcpHelper.GetInstance.Write(1147, WriteType.Coils, false);
        }
        #endregion

        #region PLC --> 上位机
        /// <summary>
        /// 初始化完成
        /// PLC --> M100.0
        /// ModbusTcp --> 1120
        /// </summary>
        public bool InitComplete { get; set; }

        /// <summary>
        /// 转台到位
        /// PLC --> M100.1
        /// ModbusTcp --> 1121
        /// </summary>
        public bool TurntableInPlace { get; set; }

        /// <summary>
        /// 机器人取面完成
        /// PLC --> M100.2
        /// ModbusTcp --> 1122
        /// </summary>
        public bool RBTakeNoodleComplete { get; set; }

        /// <summary>
        /// 轴空闲
        /// PLC --> M100.3 -- M100.04
        /// ModbusTcp --> 1123 -- 1124
        /// </summary>
        public bool[] AxisIdle { get; set; } = new bool[2] { false, false };

        /// <summary>
        /// 轴允许倒面
        /// PLC --> M100.5 -- M100.06
        /// ModbusTcp --> 1125 -- 1126
        /// </summary>
        public bool[] AxisAllowInvertedNoodle { get; set; } = new bool[2] { false, false };

        /// <summary>
        /// 轴允许倒浇头
        /// PLC --> M100.7 -- M101.00
        /// ModbusTcp --> 1127 -- 1128
        /// </summary>
        public bool[] AxisAllowInvertedSoup { get; set; } = new bool[2] { false, false };

        /// <summary>
        /// 浇头加热完成
        /// PLC --> M101.1 -- M101.02
        /// ModbusTcp --> 1129 -- 1130
        /// </summary>
        public bool[] SoupHeatComplete { get; set; } = new bool[2] { false, false };

        /// <summary>
        /// 煮面篮空闲
        /// PLC --> M101.3 -- M102.0
        /// ModbusTcp --> 1131 -- 1136
        /// </summary>
        public bool[] CookNoodleBasketIdle { get; set; } = new bool[6] { false, false, false, false, false, false };

        /// <summary>
        /// 煮面完成
        /// PLC --> M102.1 -- M102.6
        /// ModbusTcp --> 1137 -- 1142
        /// </summary>
        public bool[] CookNoodleComplete { get; set; } = new bool[6] { false, false, false, false, false, false };

        /// <summary>
        /// 取面机器人空闲状态
        /// PLC --> M102.7
        /// ModbusTcp --> 1143
        /// </summary>
        public bool TakeNoodleRobotIdle { get; set; }

        /// <summary>
        /// 取浇头机器人空闲状态
        /// PLC --> M103.0
        /// ModbusTcp --> 1144
        /// </summary>
        public bool TakeSoupRobotIdle { get; set; }

        /// <summary>
        /// 破口机构空闲
        /// PLC --> M103.1 -- M103.02
        /// ModbusTcp --> 1145 -- 1146
        /// </summary>
        public bool[] BreakMechanismIdle { get; set; } = new bool[2] { false, false };

        /// <summary>
        /// 取浇头完成
        /// PLC --> M103.3
        /// ModbusTcp --> 1147
        /// </summary>
        public bool TakeSoupComplete { get; set; }

        /// <summary>
        /// 放面至取餐口完成
        /// PLC --> M103.4
        /// ModbusTcp --> 1148
        /// </summary>
        public bool PutNoodleTakeMealComplete { get; set; }


        #endregion

        #region 传感器状态
        /// <summary>
        /// 转台下限位
        /// PLC --> M120.0
        /// ModbusTcp --> 1200
        /// </summary>
        public bool TurntableLowerLimit { get; set; }

        /// <summary>
        /// 转台上限位
        /// PLC --> M120.1
        /// ModbusTcp --> 1201
        /// </summary>
        public bool TurntableUpLimit { get; set; }

        /// <summary>
        /// 浇头缺料
        /// PLC --> M120.2 -- M120.6
        /// ModbusTcp --> 1202 -- 1206
        /// </summary>
        public bool[] SoupMaterialShortage { get; set; } = new bool[5] { false, false, false, false, false };

        /// <summary>
        /// 出餐口检测
        /// PLC --> M120.7 -- M121.1
        /// ModbusTcp --> 1207 -- 1209
        /// </summary>
        public bool[] OutMealDetect { get; set; } = new bool[3] { false, false, false };

        /// <summary>
        /// 温度到达
        /// PLC --> M121.2
        /// ModbusTcp --> 1210
        /// </summary>
        public bool TemperatureReached { get; set; }
        #endregion

        #region 内部变量

        /// <summary>
        /// 允许倒浇头
        /// </summary>
        public bool AllowPutSoup { get; set; }
        /// <summary>
        /// 允许取浇头
        /// </summary>
        public bool AllowTakeSoup { get; set; }

        /// <summary>
        /// 放面位置
        /// </summary>
        public int PutNoodleLoc { get; set; }

        /// <summary>
        /// 轴空闲位置
        /// </summary>
        public int AxisIdleIndex { get; set; }

        /// <summary>
        /// 轴空闲互锁位置
        /// </summary>
        public int AxisIdleLockIndex { get; set; }

        /// <summary>
        /// 转台位置
        /// </summary>
        public ushort TurntableLoc { get; set; }

        /// <summary>
        /// 机器人任务互锁信号
        /// </summary>
        public bool RobotTaskInterlock { get; set; }

        /// <summary>
        /// 取碗互锁信号
        /// </summary>
        public bool TakeBowlInterlock { get; set; }

        /// <summary>
        /// 取面互锁信号
        /// </summary>
        public bool TakeNoodleInterlock { get; set; }

        /// <summary>
        /// 出面中
        /// </summary>
        public bool OutNoodleing { get; set; }

        /// <summary>
        /// 允许取面
        /// </summary>
        public bool AllowTakeNoodle { get; set; }

        /// <summary>
        /// 转台互锁信号
        /// </summary>
        public bool TurntableInterlock { get; set; }

        /// <summary>
        /// 轴空闲互锁
        /// </summary>
        public bool[] AxisIdleLock { get; set; } = new bool[2] { false, false };

        /// <summary>
        /// 转台位置轮询
        /// </summary>
        public List<ushort> TurntableLocLists = new List<ushort>();

        /// <summary>
        /// 允许运行
        /// </summary>
        public bool AllowRun { get; set; }

        /// <summary>
        /// 取面位置队列
        /// </summary>
        public ConcurrentQueue<OrderLocInfo> RBTakeNoodleTask { get; set; } = new ConcurrentQueue<OrderLocInfo>();

        /// <summary>
        /// 取浇头任务队列
        /// </summary>
        public ConcurrentQueue<OrderLocInfo> TakeSoupTask { get; set; } = new ConcurrentQueue<OrderLocInfo>();

        /// <summary>
        ///取碗任务队列
        /// </summary>
        public ConcurrentQueue<OrderLocInfo> TakeBowlTask { get; set; } = new ConcurrentQueue<OrderLocInfo>();

        /// <summary>
        /// 煮面完成任务队列
        /// </summary>
        public ConcurrentQueue<ushort> CookNoodleCompleteTask { get; set; } = new ConcurrentQueue<ushort>();
        #endregion

        #region 内部变量 --> 订单ID记录

        /// <summary>
        /// 煮面口对应的订单ID
        /// </summary>
        public string[] CookNodelId { get; set; } = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, };

        /// <summary>
        /// 出餐口订单ID
        /// </summary>
        public string[] OutMealId { get; set; } = new string[3] { string.Empty, string.Empty, string.Empty };

        /// <summary>
        /// 轴允许倒面 id
        /// </summary>
        public string[] AxisAllowInvertedNoodleID { get; set; } = new string[2] { string.Empty, string.Empty };

        /// <summary>
        /// 轴允许倒浇头 id 
        /// </summary>
        public string[] AxisAllowInvertedSoupID { get; set; } = new string[2] { string.Empty, string.Empty };

        /// <summary>
        /// 浇头加热完成 id
        /// </summary>
        public string[] SoupHeatCompleteID { get; set; } = new string[2] { string.Empty, string.Empty };
        #endregion

    }
}
