namespace PlcSiemens.Core.Extension
{
    /// <summary>
    ///     位运算操作
    /// </summary>
    public static class BitExtension
    {
        #region ushort 

        /// <summary>
        /// 设置指定位置的位值的值
        /// </summary>
        /// <param name="value">ushort对象</param>
        /// <param name="position">指定位置</param>
        /// <param name="flag">值</param>
        /// <returns></returns>
        public static ushort SetBit(this ushort value, int position, bool flag)
        {
            return SetBits(value, position, 1, flag ? (byte) 1 : (byte) 0);
        }

        /// <summary>
        /// 批量设置指定位置的位值的值
        /// </summary>
        /// <param name="value">ushort对象</param>
        /// <param name="position">开始位置</param>
        /// <param name="length">长度</param>
        /// <param name="bits">值</param>
        /// <returns></returns>
        public static ushort SetBits(this ushort value, int position, int length, ushort bits)
        {
            if (length <= 0 || position >= 16) return value;

            var mask = (2 << (length - 1)) - 1;

            value &= (ushort) ~(mask << position);
            value |= (ushort) ((bits & mask) << position);

            return value;
        }

        /// <summary>
        /// 获取指定位置的值
        /// </summary>
        /// <param name="value">ushort对象</param>
        /// <param name="position">指定位置</param>
        /// <returns></returns>
        public static bool GetBit(this ushort value, int position)
        {
            return GetBits(value, position, 1) == 1;
        }

        /// <summary>
        /// 批量获取指定位置的值
        /// </summary>
        /// <param name="value">ushort对象</param>
        /// <param name="position">开始位值</param>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static ushort GetBits(this ushort value, int position, int length)
        {
            if (length <= 0 || position >= 16) return 0;

            var mask = (2 << (length - 1)) - 1;

            return (ushort) ((value >> position) & mask);
        }

        #endregion

        #region byte

        /// <summary>
        /// 设置指定位置的值
        /// </summary>
        /// <param name="value">byte对象</param>
        /// <param name="position">指定位置</param>
        /// <param name="flag">设置值</param>
        /// <returns></returns>
        public static byte SetBit(this byte value, int position, bool flag)
        {
            if (position >= 8) return value;

            var mask = (2 << (1 - 1)) - 1;

            value &= (byte) ~(mask << position);
            value |= (byte) (((flag ? 1 : 0) & mask) << position);

            return value;
        }

        /// <summary>
        /// 获取指定位置的值
        /// </summary>
        /// <param name="value">byte对象</param>
        /// <param name="position">指定位置</param>
        /// <returns></returns>
        public static bool GetBit(this byte value, int position)
        {
            if (position >= 8) return false;

            var mask = (2 << (1 - 1)) - 1;

            return (byte) ((value >> position) & mask) == 1;
        }

        #endregion

        #region uint

        /// <summary>
        /// 设置指定位置的位值的值
        /// </summary>
        /// <param name="value">uint对象</param>
        /// <param name="position">指定位置</param>
        /// <param name="flag">值</param>
        /// <returns></returns>
        public static uint SetBit(this uint value, int position, bool flag)
        {
            return SetBits(value, position, 1, flag ? (byte) 1 : (byte) 0);
        }

        /// <summary>
        /// 批量设置指定位置的位值的值
        /// </summary>
        /// <param name="value">uint对象</param>
        /// <param name="position">开始位置</param>
        /// <param name="length">长度</param>
        /// <param name="bits">值</param>
        /// <returns></returns>
        public static uint SetBits(this uint value, int position, int length, uint bits)
        {
            if (length <= 0 || position >= 32) return value;

            var mask = (2 << (length - 1)) - 1;

            value &= (uint) ~(mask << position);
            value |= (uint) ((bits & mask) << position);

            return value;
        }

        /// <summary>
        /// 获取指定位置的值
        /// </summary>
        /// <param name="value">uint对象</param>
        /// <param name="position">指定位置</param>
        /// <returns></returns>
        public static bool GetBit(this uint value, int position)
        {
            return GetBits(value, position, 1) == 1;
        }

        /// <summary>
        /// 批量获取指定位置的值
        /// </summary>
        /// <param name="value">uint对象</param>
        /// <param name="position">开始位值</param>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static uint GetBits(this uint value, int position, int length)
        {
            if (length <= 0 || position >= 32) return 0;

            var mask = (2 << (length - 1)) - 1;

            return (uint) ((value >> position) & mask);
        }

        #endregion

        #region ulong

        /// <summary>
        /// 设置指定位置的位值的值
        /// </summary>
        /// <param name="value">ulong对象</param>
        /// <param name="position">指定位置</param>
        /// <param name="flag">值</param>
        /// <returns></returns>
        public static ulong SetBit(this ulong value, int position, bool flag)
        {
            return SetBits(value, position, 1, flag ? (byte) 1 : (byte) 0);
        }

        /// <summary>
        /// 批量设置指定位置的位值的值
        /// </summary>
        /// <param name="value">ulong对象</param>
        /// <param name="position">开始位置</param>
        /// <param name="length">长度</param>
        /// <param name="bits">值</param>
        /// <returns></returns>
        public static ulong SetBits(this ulong value, int position, int length, ulong bits)
        {
            if (length <= 0 || position >= 64) return value;

            var mask = (ulong) (2 << (length - 1)) - 1;

            value &= ~(mask << position);
            value |= (bits & mask) << position;

            return value;
        }

        /// <summary>
        /// 获取指定位置的值
        /// </summary>
        /// <param name="value">ulong对象</param>
        /// <param name="position">指定位置</param>
        /// <returns></returns>
        public static bool GetBit(this ulong value, int position)
        {
            return GetBits(value, position, 1) == 1;
        }

        /// <summary>
        /// 批量获取指定位置的值
        /// </summary>
        /// <param name="value">ulong对象</param>
        /// <param name="position">开始位值</param>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static ulong GetBits(this ulong value, int position, int length)
        {
            if (length <= 0 || position >= 64) return 0;

            var mask = (ulong) (2 << (length - 1)) - 1;

            return (value >> position) & mask;
        }

        #endregion
    }
}