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

namespace PTMedicalInsurance.Common
{
    class ExpressionEvaluator
    {

        /// <summary>
        /// 转字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ConvertToString(object value)
        {
            return string.Format("{0}", value);
        }

        /// <summary>
        /// 将long转为yyyy-MM-dd
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static string LongToDate(object timestamp)
        {
            string strValue = timestamp?.ToString();
            return Utils.ConvertLongToDate(strValue);
        }

        /// <summary>
        /// 标准化为yyyy-MM-dd
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ShortDate(object dateTime)
        {
            string strValue = (dateTime??"").ToString();
            return Utils.ConvertShortDate(strValue);
        }

        public static string NoSplitDate(object dateTime)
        {
            string strValue = (dateTime ?? "").ToString();
            return Utils.ConvertNoSplitDate(strValue);
        }
        /// <summary>
        /// 保存到扩展节点
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static void SaveToExpand(string key,object value)
        {
            if (key == null) return;
            Utils.ExpandData(key, value);
        }

        /// <summary>
        /// 获取全局变量
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GlobaVariables(object value)
        {
            string key = value?.ToString();
            if (key!=null && key.StartsWith("Global_"))
            {
                string varKey = key.Replace("Global_","");
                switch (varKey)
                {
                    case "mdtrtid":
                        return Global.pat.mdtrtID;
                    case "admid":
                        return Global.pat.adm_Dr.ToString();
                    case "gender":
                        return Global.pat.gend;
                    case "name":
                        return Global.pat.name;
                    case "psn":
                        return Global.pat.psn_no;
                    case "birthday":
                        return Global.pat.brdy;
                    case "age":
                        return Global.pat.age;
                    case "insutype":
                        return Global.pat.insuType;
                    case "medtype":
                        return Global.pat.medType;
                    case "certtype":  //证件类型
                        return Global.pat.mdtrtcertType;
                    case "certno": //证件号
                        return Global.pat.mdtrtcertNO;
                    case "clearway":
                        return Global.Set.clearingWay;
                    case "insuplc":
                        return Utils.GetInsuCode();
                    case "insuorg":
                        return Utils.GetInsuOrgCode();
                    case "mdtrtarea":
                        return Global.pat.mdtrtarea_admvs;
                    case "ectoken":
                        return Global.pat.ecToken;
                    case "eccardno":
                        return Global.pat.ecCardNo;
                    case "cardsn":
                        return Global.pat.card.SN; //卡识别码
                    case "settlid":
                        return Global.pat.settlID;
                    case "biztype":
                        return Global.businessType;
                    default:
                        return string.Empty;
                }
            }
            return string.Empty;
        }
        /// <summary>
        /// 动态表达式
        /// </summary>
        /// <param name="person"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public object Run(string data, string expression)
        {
            //var options = ScriptOptions.Default
            //                .AddReferences(typeof(string).Assembly)
            //                .AddImports("System");
            //try
            //{
            //    return CSharpScript.EvaluateAsync(expression, options, person).Result;
            //}
            //catch (CompilationErrorException e)
            //{
            //    throw new InvalidOperationException("Compilation error: " + e.Diagnostics, e);
            //}
            return null;
        }
    }
}
