using System;

namespace Plane.Copters
{
    partial class CopterImplSharedPart
    {
        internal uint TimebootMs
        {
            get { return _TimebootMs; }
            set
            {
                _TimebootMs = value;
                AnalizeTimeBootMs(_TimebootMs);
            }
        }

        private float _moto1SpeedPercent;

        /// <summary>
        /// 1号马达转速百分比（0~1.0）
        /// </summary>
        public float Motor1SpeedPercent
        {
            get { return _moto1SpeedPercent; }
            set { Set(nameof(Motor1SpeedPercent), ref _moto1SpeedPercent, value); }
        }

        private float _moto2SpeedPercent;

        /// <summary>
        /// 2号马达转速百分比（0~1.0）
        /// </summary>
        public float Motor2SpeedPercent
        {
            get { return _moto2SpeedPercent; }
            set { Set(nameof(Motor2SpeedPercent), ref _moto2SpeedPercent, value); }
        }

        private float _moto3SpeedPercent;

        /// <summary>
        /// 3号马达转速百分比（0~1.0）
        /// </summary>
        public float Motor3SpeedPercent
        {
            get { return _moto3SpeedPercent; }
            set { Set(nameof(Motor3SpeedPercent), ref _moto3SpeedPercent, value); }
        }

        private float _moto4SpeedPercent;

        /// <summary>
        /// 4号马达转速百分比（0~1.0）
        /// </summary>
        public float Motor4SpeedPercent
        {
            get { return _moto4SpeedPercent; }
            set { Set(nameof(Motor4SpeedPercent), ref _moto4SpeedPercent, value); }
        }

        private float _moto5SpeedPercent;

        /// <summary>
        /// 5号马达转速百分比（0~1.0）
        /// </summary>
        public float Motor5SpeedPercent
        {
            get { return _moto5SpeedPercent; }
            set { Set(nameof(Motor5SpeedPercent), ref _moto5SpeedPercent, value); }
        }

        private float _moto6SpeedPercent;

        /// <summary>
        /// 6号马达转速百分比（0~1.0）
        /// </summary>
        public float Motor6SpeedPercent
        {
            get { return _moto6SpeedPercent; }
            set { Set(nameof(Motor6SpeedPercent), ref _moto6SpeedPercent, value); }
        }

        private float _moto7SpeedPercent;

        /// <summary>
        /// 7号马达转速百分比（0~1.0）
        /// </summary>
        public float Motor7SpeedPercent
        {
            get { return _moto7SpeedPercent; }
            set { Set(nameof(Motor7SpeedPercent), ref _moto7SpeedPercent, value); }
        }

        private float _moto8SpeedPercent;

        /// <summary>
        /// 8号马达转速百分比（0~1.0）
        /// </summary>
        public float Motor8SpeedPercent
        {
            get { return _moto8SpeedPercent; }
            set { Set(nameof(Motor8SpeedPercent), ref _moto8SpeedPercent, value); }
        }


        /// <summary>
        /// 将16进制映射为0~1.0的数值。
        /// 其中，0转为0，1~15映射为1~100。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private float Converter1_15to1_100(int value)
        {
            if (value == 0f)
            {
                return 0;
            }
            else if (value == 1f)
            {
                return 0.01f;
            }
            else
            {
                return (value - 1) / 14f + 0.01f;
            }
        }

        private void AnalizeTimeBootMs(uint value)
        {
            var str = value.ToString("x8");//转成8位的16进制字符，共8个ASCII字符，每个字符对应一个马达转速（16进制）
            Motor1SpeedPercent = Converter1_15to1_100(Convert.ToInt32(str.Substring(str.Length - 1, 1), 16));
            Motor2SpeedPercent = Converter1_15to1_100(Convert.ToInt32(str.Substring(str.Length - 2, 1), 16));
            Motor3SpeedPercent = Converter1_15to1_100(Convert.ToInt32(str.Substring(str.Length - 3, 1), 16));
            Motor4SpeedPercent = Converter1_15to1_100(Convert.ToInt32(str.Substring(str.Length - 4, 1), 16));
            Motor5SpeedPercent = Converter1_15to1_100(Convert.ToInt32(str.Substring(str.Length - 5, 1), 16));
            Motor6SpeedPercent = Converter1_15to1_100(Convert.ToInt32(str.Substring(str.Length - 6, 1), 16));
            Motor7SpeedPercent = Converter1_15to1_100(Convert.ToInt32(str.Substring(str.Length - 7, 1), 16));
            Motor8SpeedPercent = Converter1_15to1_100(Convert.ToInt32(str.Substring(str.Length - 8, 1), 16));
        }
    }
}
