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();
            //if (action != null)
            //{
            //    action();
            //    if (callback != null) callback();
            //}
        }

        /// <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();
            //if (action != null)
            //{
            //    action(par);
            //    if (callback != null) callback();
            //}
        }

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


        /// <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;
        }
    }
}
