using Plane.Protocols;
using System;

namespace Plane.Copters
{
    partial class PlaneCopter
    {
        public delegate void ErrorDataEventHandle(uint itimes, ulong iDataCount, byte sCommand);

        public delegate void GetLogData(string log);

        public delegate void MissionResultHandle(MAVLink.MAV_MISSION_RESULT missionresult);

        public delegate void MyEvent(object sender, String ParamID, int ParamIndex, int ParamCount);

        public delegate void MyEventHandler(object sender, uint iError, ulong iCount);

        public delegate void MyRevEvent(object sender, byte StreamType);

        public delegate void MyRevScaleImu2Event(object sender);

        public delegate void MyRevSensorEvent(object sender);

        public delegate void MyRevSysEvent(object sender, byte StreamType, bool GPSBadHealth, bool GyroBadHealth, bool AccelBadHealth, bool CompassBadHealth, bool BarometerBadHealth, bool canTakeOff);

        public event EventHandler ConnectionBroken;

        public event ErrorDataEventHandle ErrorDataEvent;

        public event GetLogData GetLogDataEvent;

        public event EventHandler<MessageCreatedEventArgs> MessageCreated;

        /// <summary>
        /// 在数据包被解析后引发。
        /// </summary>
        public event EventHandler<PacketReceivedEventArgs> PacketHandled;

        /// <summary>
        /// 在接收到数据包但未解析后引发。
        /// </summary>
        public event EventHandler<PacketReceivedEventArgs> PacketReceived;

        public event MyRevEvent ReceiveDataStreamEvent;

        public event MyEventHandler ReceiveHeartBearEvent;

        public event MyEvent ReceiveParamEvent;

        public event MyRevSensorEvent ReceiveScaleImu2Event;

        public event MyRevSensorEvent ReceiveSensorEvent;

        public event MyRevSysEvent ReceiveSysStatusEvent;

        /// <summary>
        /// 在尝试解析数据包但未找到对应的解析方法时引发。
        /// </summary>
        public event EventHandler<PacketReceivedEventArgs> UnhandledPacketReceived;

        private void RaiseReceiveDataStreamEventOnUIThread(byte messageId)
        {
            var handler = ReceiveDataStreamEvent;
            if (handler != null)
            {
                RunOnUIThread(() => handler(this, messageId));
            }
        }

        public class PacketReceivedEventArgs : EventArgs
        {
            public PacketReceivedEventArgs(byte[] packet)
            {
                this.Packet = packet;
            }

            public byte[] Packet { get; set; }
        }
    }
}
