using DynamicExpresso;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using wispro.sp.entity;

namespace wispro.sp.api.Utility
{
    public class Utility
    {
        public static void CalBasePoint(PerformanceItem item, List<BasePointRule> rules)
        {
            
            if (item.CaseNo.ToUpper().StartsWith("S") || item.AgentFeedbackMemo == "特殊点数申诉")
            {
                return;
            }
            #region 计算基本点数值及绩效类型

            rules.Sort((a, b) =>
            {
                var o = b.Priority - a.Priority;
                return o;
            });

            foreach (BasePointRule rule in rules)
            {
                var options = InterpreterOptions.Default | InterpreterOptions.LambdaExpressions;
                var interpreter = new Interpreter(options)
                    .Reference(typeof(System.Linq.Enumerable));

                try
                {
                    
                    Func<PerformanceItem,Customer, bool> func = interpreter.ParseAsDelegate<Func<PerformanceItem,Customer,bool>>(rule.Rule, "p","Customer");

                    bool result = func.Invoke(item,item.Customer);

                    if (result)
                    {
                        var temString = rule.PointExpress;

                        if (!rule.PointExpress.Contains("."))
                        {
                            temString = $"{rule.PointExpress.Trim()}.0";
                        }

                        var target = new Interpreter().SetVariable("p", item);
                        
                        double? temPoint = (double?)target.Eval(temString);
                        string temType = rule.Type;
                        if(temString.Contains("p.WordCount") && item.DoItem == "新申请" && temPoint.HasValue)
                        {
                            switch (item.DoItemCoefficient)
                            {
                                case "中-德":
                                    if (1.9 > temPoint.Value)
                                    {
                                        temType = "新申请";
                                        temPoint = 1.9;
                                    }
                                   
                                    break;
                                case "中-英":
                                    if (1.8 > temPoint.Value)
                                    {
                                        temType = "新申请";
                                        temPoint = 1.8;
                                    }
                                    break;
                                case "英-中":
                                    if (1.5 > temPoint.Value)
                                    {
                                        temType = "新申请";
                                        temPoint = 1.5;
                                    }
                                    break;

                            }
                        }

                        if(item.BasePoint != temPoint || item.Type !=temType)
                        {
                            item.BasePoint = temPoint;
                            item.Type = temType;
                        }
                        
                        break;
                    }
                }
                catch(Exception ex) {
                    System.Diagnostics.Debug.WriteLine($"{rule.Rule}\t{ex.Message}" );
                }
            }
            #endregion
        }

        public static List<string> GetDoItemCeofficient(PerformanceItem item, List<BasePointRule> rules)
        {
            List<string> retList = new List<string>();
            foreach (BasePointRule rule in rules)
            {
                Regex r = new System.Text.RegularExpressions.Regex("p.DoItemCoefficient\\s{0,}==\\s{0,}\"(.+?)\"");

                Match m = r.Match(rule.Rule);

                if (m.Success)
                {
                    string strFeedback = m.Groups[1].Value;
                    string temRule = rule.Rule.Replace(m.Value, "");

                    if (string.IsNullOrEmpty(temRule.Trim()))
                    {
                        continue;
                    }
                    else
                    {
                        r = new Regex("&&\\s{1,}&&");
                        m = r.Match(temRule);
                        if (m.Success)
                        {
                            temRule = temRule.Replace(m.Value, "&&");
                        }

                        if (temRule.Trim().EndsWith("&&"))
                        {
                            temRule = temRule.Trim().Substring(0, temRule.Trim().Length - 2).Trim();
                        }

                        if (temRule.Trim().StartsWith("&&"))
                        {
                            temRule = temRule.Substring(2).Trim();
                        }

                        var interpreter = new Interpreter();
                        //item.ApplicationType
                        Func<PerformanceItem, bool> func = interpreter.ParseAsDelegate<Func<PerformanceItem, bool>>(temRule, "p");

                        bool result = func.Invoke(item);

                        if (result)
                        {
                            if (!retList.Contains(strFeedback))
                            {
                                retList.Add(strFeedback);
                            }
                        }
                    }
                }

            }

            return retList;
        }

        public  static  List<string> GetFeedbackMemos(PerformanceItem item, List<BasePointRule> rules)
        {
            List<string> retList = new List<string>();
            foreach(BasePointRule rule in rules)
            {
                Regex r= new System.Text.RegularExpressions.Regex("p.AgentFeedbackMemo==\"(.+?)\"");

                Match m = r.Match(rule.Rule);

                if (m.Success)
                {
                    string strFeedback = m.Groups[1].Value;
                    string temRule = rule.Rule.Replace(m.Value, "");

                    if (string.IsNullOrEmpty(temRule.Trim()))
                    {
                        if (!retList.Contains(strFeedback))
                        {
                            retList.Add(strFeedback);
                        }
                    }
                    else
                    {
                        r = new Regex("&&\\s{1,}&&");
                        m = r.Match(temRule);
                        if (m.Success)
                        {
                            temRule = temRule.Replace(m.Value, "&&");
                        }

                        if (temRule.Trim().EndsWith("&&"))
                        {
                            temRule = temRule.Trim().Substring(0, temRule.Trim().Length - 2).Trim();
                        }

                        if (temRule.Trim().StartsWith("&&"))
                        {
                            temRule = temRule.Substring(2).Trim();
                        }

                        var interpreter = new Interpreter();
                        //item.ApplicationType
                        Func<PerformanceItem, bool> func = interpreter.ParseAsDelegate<Func<PerformanceItem, bool>>(temRule, "p");

                        bool result = func.Invoke(item);

                        if (result)
                        {
                            if (!retList.Contains(strFeedback))
                            {
                                retList.Add(strFeedback);
                            }
                        }
                    }
                }

            }

            return retList;
        }

        
    }
}
