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

namespace Sln.Iot.CFX.CFXConnect
{
    /// <summary>
    /// 提升机1连接
    /// </summary>
    public class CFXConnectOven : CFXHelper
    {
        private static readonly Lazy<CFXConnectOven> lazy = new Lazy<CFXConnectOven>(() => new CFXConnectOven());

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

        AreYouThereRequestReceived2 areYouThereRequestReceived2 = new AreYouThereRequestReceived2();
        GetEndpointInformationRequestReceived2 getEndpointInformationRequestReceived2 = new GetEndpointInformationRequestReceived2();
        WhoIsThereRequestReceived2 whoIsThereRequestReceived2 = new WhoIsThereRequestReceived2();
        GetActiveRecipeRequestReceived2 getActiveRecipeRequestReceived2 = new GetActiveRecipeRequestReceived2();
        GetActiveFaultsRequestReceived2 getActiveFaultsRequestReceived2 = new GetActiveFaultsRequestReceived2();
        HandleFaultRequestReceived2 handleFaultRequestReceived2 = new HandleFaultRequestReceived2();
        ModifyStationParametersRequestReceived2 modifyStationParametersRequestReceived2 = new ModifyStationParametersRequestReceived2();

        //HeartbeatEvent heartbeatEvent = new HeartbeatEvent();

        CFXEventTools cFXEventTools = CFXEventTools.Instance;

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

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

            if (request.MessageBody is AreYouThereRequest) //存在性检测请求
            {
                areYouThereRequestReceived2.Handle(request.MessageBody as AreYouThereRequest, out response);
            }
            else if (request.MessageBody is GetEndpointInformationRequest) //获取终端节点信息请求
            {
                getEndpointInformationRequestReceived2.Handle(request.MessageBody as GetEndpointInformationRequest, out response);
            }
            else if (request.MessageBody is WhoIsThereRequest) //设备存在性查询请求
            {
                whoIsThereRequestReceived2.Handle(request.MessageBody as WhoIsThereRequest, out response);
            }
            else if (request.MessageBody is GetActiveRecipeRequest) //获取当前激活配方请求
            {
                getActiveRecipeRequestReceived2.Handle(request.MessageBody as GetActiveRecipeRequest, out response);
            }
            else if (request.MessageBody is GetActiveFaultsRequest) //获取当前错误状态请求
            {
                getActiveFaultsRequestReceived2.Handle(request.MessageBody as GetActiveFaultsRequest, out response);
            }
            else if (request.MessageBody is HandleFaultRequest) //处理错误请求
            {
                handleFaultRequestReceived2.Handle(request.MessageBody as HandleFaultRequest, out response);
            }
            else if (request.MessageBody is ModifyStationParametersRequest) //设备修改参数请求
            {
                modifyStationParametersRequestReceived2.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)
            {

            }
        }
    }
}
