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>
        ///// 保存数据
        ///// </summary>
        //public static void Save<T>(this T ot)
        //{
        //    string outjson = JsonConvert.SerializeObject(ot);
        //    var str = ot.GetType().GenericTypeArguments;
        //    if (str != null && str.Length > 0)
        //    {
        //        File.WriteAllText(LocaPath.GetInstance.Getpath(str[0].Name), outjson);
        //    }

        //}

        ///// <summary>
        ///// 获取保存的数据
        ///// </summary>
        //public static void Read(this object ot)
        //{
        //    var str = ot.GetType().GenericTypeArguments;
        //    if (str != null && str.Length > 0)
        //    {
        //        string pa = LocaPath.GetInstance.Getpath(str[0].Name);
        //        if (File.Exists(pa))
        //        {
        //            string JsonString = File.ReadAllText(pa);
        //            var result = JsonConvert.DeserializeObject<object>(JsonString);
        //            if (result != null) { Json<object>.Data = result; }
        //        }
        //    }
        //}
    }
}
