using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Entity;
using PTMedicalInsurance.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PTMedicalInsurance.Common
{
    public static class AppExtension
    {
        /// <summary>
        /// 获取交易枚举对于的code信息
        /// </summary>
        public static string GetCode(this TradeEnum tradeEnum)
        {
            Type type = tradeEnum.GetType();
            FieldInfo fd = type.GetField(tradeEnum.ToString());
            return getFieldCode(fd);
        }
        /// <summary>
        /// 获取交易描述信息
        /// </summary>
        /// <param name="tradeEnum"></param>
        /// <returns></returns>
        public static string GetName(this TradeEnum tradeEnum)
        {
            Type type = tradeEnum.GetType();
            FieldInfo fd = type.GetField(tradeEnum.ToString());
            return getFieldName(fd);
        }

        public static ModeEnum GetMode(this TradeEnum tradeEnum)
        {
            Type type = tradeEnum.GetType();
            FieldInfo fd = type.GetField(tradeEnum.ToString());
            TradeAttribute attribute = getAttribte(fd);
            if (attribute != null) return attribute.Mode;
            // 默认值
            return ModeEnum.REST;
        }

        public static TradeEnum GetByCode(this TradeEnum tradeEnum,string code)
        {
            Type type = tradeEnum.GetType();
            FieldInfo[] fds = type.GetFields();
            foreach (FieldInfo fd in fds)
            {
                TradeAttribute attribute = getAttribte(fd);
                if (code.Equals(attribute?.Code))
                {
                    return (TradeEnum)fd.GetValue(tradeEnum);
                }
            }
            return TradeEnum.DEFAULT;
        }

        private static TradeAttribute getAttribte(FieldInfo fd)
        {
            TradeAttribute attribute = null;
            if (fd == null)
            {
                return attribute;
            }
            var attributes = fd.GetCustomAttributes(typeof(TradeAttribute), false);
            if (attributes != null && attributes.Length == 1)
            {
                attribute = attributes.Single() as TradeAttribute;
                return attribute;
            }
            return attribute;
        }

        public static string Text(this JToken jtoken)
        {
            if (jtoken == null) return string.Empty;
            return jtoken.ToString();
        }
        /// <summary>
        /// 转换为前端需要的对象字符串
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ParseReturnObject(this string text)
        {
            if (string.IsNullOrEmpty(text) || !text.StartsWith("{")) {
                return JsonHelper.setIrisReturnValue(-1, text, null).ToString();
            }
            JObject joRtn = JObject.Parse(text);
            // irisReturn格式
            if (joRtn.ContainsKey("errorCode"))
            {
                return text;
            }
            return JsonHelper.setIrisReturnValue(0, "", joRtn).ToString();
        }

        public static int Int(this JToken jtoken)
        {
            if (jtoken == null) return 0;
            return int.Parse(jtoken.ToString());
        }

        private static string getFieldCode(FieldInfo fd)
        {
            TradeAttribute attribute = getAttribte(fd);
            return attribute?.Code;
        }

        private static string getFieldName(FieldInfo fd)
        {
            TradeAttribute attribute = getAttribte(fd);
            return attribute?.Name;
        }

        public static void Extend(this JObject jObject,string key,object value)
        {
            if (jObject != null && !jObject.ContainsKey(key))
            {
                jObject.Add(key, new JValue(value));
            }
        }
    }
}