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

namespace ryCommon
{
    /// <summary>
    /// Json扩展
    /// </summary>
    public static class JsonExt
    {
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static string GetValue(this JToken name, string id, string defValue)
        {
            var item = name[id];
            if (item == null) { return defValue; }
            return item.ToString();
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static bool GetValue(this JToken name, string id, bool defValue)
        {
            var item = name[id];
            if (item == null) { return defValue; }
            return item.ToBool();
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static int GetValue(this JToken name, string id, int defValue)
        {
            var item = name[id];
            if (item == null) { return defValue; }
            return item.ToInt(defValue);
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static int GetValue(this JToken name, string id,int min,int max, int defValue)
        {
            var value = GetValue(name, id, defValue);
            if (value >= min && value <= max) { return value; }
            return defValue;
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static long GetValue(this JToken name, string id, long defValue)
        {
            var item = name[id];
            if (item == null) { return defValue; }
            return item.ToInt64(defValue);
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static decimal GetValue(this JToken name, string id, decimal defValue)
        {
            var item = name[id];
            if (item == null) { return defValue; }
            return item.ToDecimal(defValue);
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static double GetValue(this JToken name, string id, double defValue)
        {
            var item = name[id];
            if (item == null) { return defValue; }
            return item.ToDouble(defValue);
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static JArray GetValue(this JToken name, string id, JArray defValue)
        {
            var item = name[id];
            if (item == null) { return defValue; }
            if(item is JArray jarr1)
            {
                return jarr1;
            }
            return defValue;
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static JObject GetValue(this JToken name, string id, JObject defValue)
        {
            var item = name[id];
            if (item == null) { return defValue; }
            if (item is JObject jo1)
            {
                return jo1;
            }
            return defValue;
        }
    }
}
