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)
        {
            #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));

                //System.Diagnostics.Debug.WriteLine(rule.Rule);

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

                    bool result = func.Invoke(item);

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

                        item.BasePoint = (double?)interpreter.Eval(temString);
                        item.Type = rule.Type;
                        break;
                    }
                }
                catch(Exception ex) {
                    System.Diagnostics.Debug.WriteLine(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;
        }

        
    }
}
