using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.Helper
{
    public static class ExpandMethod
    {
        /// <summary>
        /// 获取布尔数组指定值得索引
        /// </summary>
        /// <param name="obj">要获取索引的数组</param>
        /// <param name="value">要获取索引的值</param>
        /// <returns></returns>
        public static int GetIndex(this bool[] obj, bool value)
        {
            if (obj == null) return -1;
            return Array.FindIndex(obj, p => p == value);
        }

        /// <summary>
        /// 获取字符串数组指定值得索引
        /// </summary>
        /// <param name="obj">要获取索引的数组</param>
        /// <param name="value">要获取索引的值</param>
        /// <returns></returns>
        public static int GetIndex(this string[] obj, string value)
        {
            if (obj == null || value == null) return -1;
            return Array.FindIndex(obj, p => p == value && p.Length > 0);
        }

        /// <summary>
        /// 委托回调
        /// </summary>
        /// <param name="action">要执行的委托</param>
        /// <param name="callback">委托回调</param>
        public static void Invoke(this Action action, Action callback)
        {
            action?.Invoke();
            callback?.Invoke();
        }

        /// <summary>
        /// 委托回调
        /// </summary>
        /// <param name="action">要执行的委托</param>
        /// <param name="par">要执行的委托的参数</param>
        /// <param name="callback">委托回调</param>
        public static void Invoke(this Action<object> action, object par, Action callback)
        {
            action?.Invoke(par);
            callback?.Invoke();
        }

        public static void Invokes(this Action<object[]> action, object[] par, Action callback)
        {
            action?.Invoke(par);
            callback?.Invoke();
        }


        /// <summary>
        /// 字节数组转换成32位整数
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static int BytesToInt(this byte[] bytes)
        {
            if (bytes.Length > 4) return -1;
            int ReturnVlaue = 0;
            for (int i = 0; i < bytes.Length; i++)
            {
                ReturnVlaue += (int)(bytes[i] << (i * 8));
            }
            return ReturnVlaue;
        }

        /// <summary>
        /// 字节数组转换成 ushort 数组
        /// </summary>
        /// <param name="bytes">要转换的字节数组</param>
        /// <param name="reverse">字节高度顺序控制</param>
        /// <returns></returns>
        public static ushort[] BytesToUshorts(this byte[] bytes, bool reverse = false)
        {
            int len = bytes.Length;

            byte[] srcPlus = new byte[len + 1];
            bytes.CopyTo(srcPlus, 0);
            int count = len >> 1;

            if (len % 2 != 0)
            {
                count += 1;
            }

            ushort[] dest = new ushort[count];
            if (reverse)
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
                }
            }

            return dest;
        }

        /// <summary>
        /// ushort 数组转换成字节数组
        /// </summary>
        /// <param name="src">需要转换的 ushort数组</param>
        /// <param name="reverse">高低字节的设置</param>
        /// <returns></returns>
        public static byte[] UshortsToBytes(this ushort[] src, bool reverse = false)
        {

            int count = src.Length;
            byte[] dest = new byte[count << 1];
            if (reverse)
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i * 2] = (byte)(src[i] >> 8);
                    dest[i * 2 + 1] = (byte)(src[i] >> 0);
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i * 2] = (byte)(src[i] >> 0);
                    dest[i * 2 + 1] = (byte)(src[i] >> 8);
                }
            }
            return dest;
        }








    }
}
