using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assets.Scripts.Devices.Ant.Pages
{
    /// <summary>
    /// Crank Torque-Frequency Message 曲柄功率计的消息
    /// </summary>
    public class CtfPageHandler : IPageHandler
    {
        private readonly CrankTorqueFrequencyData _crankTorqueFrequency = new CrankTorqueFrequencyData();

        public bool CanHandle(byte pageNumber)
        {
            return (int)pageNumber == 32;
        }

        public void Handle(byte[] dataPayload, AbstractAntDevice device)
        {
            var powerDevice = device as PowerDevice;
            if (powerDevice == null)
            {
                return;
            }

            byte num1 = dataPayload[1];
            ushort num2 = (ushort)((uint)dataPayload[3] | (uint)dataPayload[2] << 8);
            ushort num3 = (ushort)((uint)dataPayload[5] | (uint)dataPayload[4] << 8);
            ushort num4 = (ushort)((uint)dataPayload[7] | (uint)dataPayload[6] << 8);

            if (powerDevice.Offset.HasValue)
            {
                int offset = powerDevice.Offset.GetValueOrDefault(0);

                CrankTorqueFrequencyData torqueFrequencyData = this._crankTorqueFrequency;
                int num5 = (int)num1;
                int num6 = (int)num3;
                int num7 = (int)num4;
                int num8 = (int)num2;

                torqueFrequencyData.Update((byte)num5, (ushort)num6, (ushort)num7, (ushort)num8, offset);

                Console.WriteLine("offset:" + offset);
                powerDevice.Power = this._crankTorqueFrequency.Power.GetValueOrDefault();
            }
            powerDevice.Cadence = this._crankTorqueFrequency.Cadence;
        }
    }
}
