using CFX;
using CFX.InformationSystem.UnitValidation;
using CFX.Production;
using CFX.ResourcePerformance;
using CFX.Structures;
using Sln.Iot.CFX.CFXBusiness;
using Sln.Iot.CFX.CFXRequestes;
using Sln.Iot.CFX.Event;
using Sln.Iot.CFX.RequestReceived;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sln.Iot.CFX.CFXConnect
{
    /// <summary>
    /// 真空箱连接
    /// </summary>
    public class CFXConnectVacuum : CFXHelper
    {
        private static readonly Lazy<CFXConnectVacuum> lazy = new Lazy<CFXConnectVacuum>(() => new CFXConnectVacuum());

        public static CFXConnectVacuum Instance
        {
            get
            {
                return lazy.Value;
            }
        }

        AreYouThereRequestReceived1 areYouThereRequestReceived1 = new AreYouThereRequestReceived1();
        GetEndpointInformationRequestReceived1 getEndpointInformationRequestReceived1 = new GetEndpointInformationRequestReceived1();
        WhoIsThereRequestReceived1 whoIsThereRequestReceived1 = new WhoIsThereRequestReceived1();
        GetActiveRecipeRequestReceived1 getActiveRecipeRequestReceived1 = new GetActiveRecipeRequestReceived1();
        GetActiveFaultsRequestReceived1 getActiveFaultsRequestReceived1 = new GetActiveFaultsRequestReceived1();
        HandleFaultRequestReceived1 handleFaultRequestReceived1 = new HandleFaultRequestReceived1();
        ModifyStationParametersRequestReceived1 modifyStationParametersRequestReceived1 = new ModifyStationParametersRequestReceived1();

        CFXEventTools cFXEventTools = CFXEventTools.Instance;

        public string CFXHandle { get; set; } = "CFX.A00.OT00470018";

        protected override CFXMessage HandleRequest(CFXEnvelope request)
        {
            CFXMessage response = null;

            if (request.MessageBody is AreYouThereRequest) //存在性检测请求
            {
                areYouThereRequestReceived1.Handle(request.MessageBody as AreYouThereRequest, out response);
            }
            else if (request.MessageBody is GetEndpointInformationRequest) //获取终端节点信息请求
            {
                getEndpointInformationRequestReceived1.Handle(request.MessageBody as GetEndpointInformationRequest, out response);
            }
            else if (request.MessageBody is WhoIsThereRequest) //设备存在性查询请求
            {
                whoIsThereRequestReceived1.Handle(request.MessageBody as WhoIsThereRequest, out response);
            }
            else if (request.MessageBody is GetActiveRecipeRequest) //获取当前激活配方请求
            {
                getActiveRecipeRequestReceived1.Handle(request.MessageBody as GetActiveRecipeRequest, out response);
            }
            else if (request.MessageBody is GetActiveFaultsRequest) //获取当前错误状态请求
            {
                getActiveFaultsRequestReceived1.Handle(request.MessageBody as GetActiveFaultsRequest, out response);
            }
            else if (request.MessageBody is HandleFaultRequest) //处理错误请求
            {
                handleFaultRequestReceived1.Handle(request.MessageBody as HandleFaultRequest, out response);
            }
            else if (request.MessageBody is ModifyStationParametersRequest) //设备修改参数请求
            {
                modifyStationParametersRequestReceived1.Handle(request.MessageBody as ModifyStationParametersRequest, out response);
            }
            else //不支持的请求类型
            {
                response = new NotSupportedResponse()
                {
                    RequestResult = new RequestResult()
                    {
                        Result = StatusResult.Failed,
                        ResultCode = 0,
                        Message = $"不支持的请求类型:{request.MessageBody.GetType().Name}"
                    }
                };
            }

            return response;
        }

        /// <summary>
        /// 发送生产单元验证请求接收通知
        /// </summary>
        public void SendValidateUnitsRequest(string uri, string primaryIdentifier, string[] unitsIdentifier)
        {
            try
            {
                ValidateUnitsRequest msg = new ValidateUnitsRequest()
                {
                    Validations = new List<ValidationType>()
                    {
                        ValidationType.UnitTraceValidation,
                    },
                    PrimaryIdentifier = primaryIdentifier,
                    Units = cFXEventTools.UnitPositionTrans(unitsIdentifier)
                };

                var validateresult = Endpoint.ExecuteRequest(uri, new CFXEnvelope(msg)
                {
                    Source = Endpoint.CFXHandle,
                    Target = "inline-control"
                });

                if (validateresult == null)
                {
                    return;
                }

                var response = validateresult.MessageBody as ValidateUnitsResponse;
                //响应逻辑
            }
            catch (Exception ex)
            {

            }
        }
    }
}
