using Assets.Scripts.Devices.Ant.Interfaces;
using Assets.Scripts.Devices.Ant.LegacyPages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assets.Scripts.Devices.Ant
{
    public class CadenceDevice : AbstractAntDevice, ICadenceDevice
    {
        public const int MAX_NO_EVENT_STOP_COUNT = 12;

        private int _Cadence;
        public int Cadence
        {
            get
            {
                return _Cadence;
            }
            set
            {
                _Cadence = DeviceValueFilter.Cadence(value);
            }
        }

        private DateTime _now = DateTime.Now;
        private RotationData _rotationData = new RotationData();

        public CadenceDevice()
            : base("Ant+ Cadence", racerSportType.Biking, SensorType.Cadence)
        {
            Priority = 2;
            //if (speedSensor.isInUse)
            //    throw new ArgumentException("Can't use a Speed Sensor that is already in use.");

            //speedSensor.customSourceName = "Spd used by Cad w/Spd";
            //speedSensor.isInUse = true;
            //this.speedSensor = speedSensor;
            //speedSensor.start(newSpeedSensorPacket);    //Ensure speed sensor is always running so we can always get the values
        }

        //public override void start(Action<DataSourcePacket> distanceUpdateHandler)
        //{
        //    //Reset the speed sensor first so we get back to a zero distance 
        //    //speedSensor.stop();
        //    //lastDistance = 0;
        //    //speedSensor.reset();            
        //    //speedSensor.start(newSpeedSensorPacket);

        //    base.start(distanceUpdateHandler);
        //}

        //public void newSpeedSensorPacket(DataSourcePacket pckt)
        //{
        //    double distDiff = pckt.distance - lastDistance;
        //    if(distDiff > 0)
        //        incrementDistanceAndUpdate(distDiff, speedMs: pckt.speed_ms, cadence: calculatedCadence);
        //    lastDistance = pckt.distance;
        //}

        protected override AntChannelProfile getDefaultSearchProfile()
        {
            return new AntChannelProfile()
            {
                rfOffset = 57,
                transType = 0,
                deviceType = 122,
                deviceNumber = 0,
                messagePeriod = 8102,
                pairingEnabled = false,
            };
        }

        public override void handleChannelResponse(ANT_Managed_Library.ANT_Response response)
        {
            //商品Manufacturer Information
            if (response.messageContents[1] == 2)
            {
                //Console.WriteLine(string.Join(",",response.messageContents));
                base.ManufacturerId = response.messageContents[2];
            }

            if (response.responseID == (byte)ANT_Managed_Library.ANT_ReferenceLibrary.ANTMessageID.BROADCAST_DATA_0x4E)
            {
                //In this decode we ignore page change toggle and page type, since the info we need is transmitted on every page

                var page = response.messageContents.Skip(1).ToArray();
                var cadence = this._rotationData.CalculateRpm(_now, (int)page[4] | (int)page[5] << 8, (int)page[6] | (int)page[7] << 8);

                Cadence = cadence.GetValueOrDefault(0);
            }
        }
    }
}
