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

namespace wispro.sp.web.Services
{
    public class Reason
    {
        public string Value { get; set; }
        public string Name { get; set; }

        public List<string> Expresses {get;set;}
    }

    public interface IAgentFeedBackMemoItemsService
    {
        List<Reason> GetItems(PerformanceItem item);
    }

    public class AgentFeedbackMemoItemsServices:IAgentFeedBackMemoItemsService
    {
        List<Reason> _Reasons = new List<Reason>()
        {
            new Reason(){Name="PCT首次英文案",Value="PCT首次英文案", Expresses= new List<string>(){ "p.DoItem==\"新申请\"" } },
            new Reason(){Name="我方转格式、复核",Value="我方转格式、复核" ,Expresses= new List<string>()},
            new Reason(){Name="台湾案转大陆案",Value="台湾案转大陆案",Expresses= new List<string>(){ "p.DoItem==\"新申请\" && p.CaseNo.StartsWith(\"PACN\") " } },
            new Reason(){Name="大陆案转台湾案",Value="大陆案转台湾案",Expresses= new List<string>(){ "p.DoItem==\"新申请\" && p.CaseNo.StartsWith(\"PATW\") " } },
            new Reason(){Name="同套大陆+台湾",Value="同套大陆+台湾",Expresses= new List<string>(){
                "p.DoItem==\"新申请\" && p.ApplicationType==\"发明\"  && p.CaseNo.StartsWith(\"PATW\")",
                "p.DoItem==\"新申请\" && p.ApplicationType==\"发明\"  && p.CaseNo.StartsWith(\"PACN\")",
                "p.DoItem==\"新申请\" && p.ApplicationType==\"实用新型\" && p.CaseNo.StartsWith(\"PACN\")",
                "p.DoItem==\"新申请\" && p.ApplicationType==\"实用新型\" && p.CaseNo.StartsWith(\"PATW\")"} },
            new Reason(){Name="不请款",Value="不请款",Expresses= new List<string>(){ 
                "p.DoItem==\"处理审查意见\" && p.ApplicationType==\"实用新型\"", 
                "p.DoItem==\"意见陈述\" && p.ApplicationType==\"实用新型\"" } },
            new Reason(){Name="撤回后重新递交",Value="撤回后重新递交",Expresses= new List<string>()},
            new Reason(){Name="发文后客户取消申请",Value="发文后客户取消申请",Expresses= new List<string>(){
                "p.ApplicationType==\"实用新型\" && p.CaseNo.StartsWith(\"PACN\")",
                "p.ApplicationType==\"发明\" && p.CaseNo.StartsWith(\"PACN\")"} },
            new Reason(){Name="发文后客户原因取消申请，系统结案",Value="发文后客户原因取消申请，系统结案",Expresses= new List<string>(){
                "p.DoItem==\"新申请\" && p.CaseNo.StartsWith(\"PADE\")",
                "p.DoItem==\"新申请\" && p.CaseNo.StartsWith(\"PAUS\")",
                "p.DoItem==\"新申请\" && p.CaseNo.StartsWith(\"PAIN\")",
                "p.DoItem==\"新申请\" && p.CaseNo.StartsWith(\"PAGB\")",
                "p.DoItem==\"新申请\" && p.CaseNo.StartsWith(\"PAEPO\")",
            } },
            new Reason(){Name="改权",Value="改权",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" } },
            new Reason(){Name="改权+改说明书",Value="改权+改说明书",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" }},
            new Reason(){Name="客户不进行答辩",Value="客户不进行答辩",Expresses= new List<string>(){ "p.DoItem==\"处理审查意见\"" } },
            new Reason(){Name="客户提供答辩点，撰写英文报导函",Value="客户提供答辩点，撰写英文报导函",Expresses= new List<string>(){ 
                "p.DoItem==\"处理审查意见\" && !p.CaseNo.Contains(\"CN\")" } },
            new Reason(){Name="客户未提供答辩点，撰写英文报导函",Value="客户未提供答辩点，撰写英文报导函辩",Expresses= new List<string>(){ "p.DoItem==\"处理审查意见\" && !p.CaseNo.Contains(\"CN\")" }},
            new Reason(){Name="内-外",Value="内-外",Expresses= new List<string>(){ "p.DoItem==\"翻译校核\"" } },
            new Reason(){Name="外-内",Value="外-内",Expresses= new List<string>(){ "p.DoItem==\"翻译校核\"" }},
            new Reason(){Name="外-内首次申请",Value="外-内首次申请",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" } },
            new Reason(){Name="请款",Value="请款",Expresses= new List<string>(){
                "p.DoItem==\"处理审查意见\" && p.ApplicationType==\"实用新型\"  && p.CaseStage==\"二通\"",
                "p.DoItem==\"处理审查意见\" && p.ApplicationType==\"实用新型\"  && p.CaseStage==\"一通\"",
                "p.DoItem==\"请求复审\" && p.ApplicationType==\"实用新型\"",
                "p.DoItem==\"意见陈述\" && p.ApplicationType==\"实用新型\"",
            } },
            new Reason(){Name="涉外OA不答辩，发报导函结案",Value="涉外OA不答辩，发报导函结案",Expresses= new List<string>(){ "p.DoItem==\"处理审查意见\"" } },
            new Reason(){Name="涉外实质改权",Value="涉外实质改权",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" } },
            new Reason(){Name="首次中文案",Value="首次中文案",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" } },
            new Reason(){Name="我方转格式、复核",Value="我方转格式、复核",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" } },
            new Reason(){Name="外所/他人首次转入OA",Value="外所/他人首次转入OA",Expresses= new List<string>(){ "p.DoItem==\"处理审查意见\"" } },
            new Reason(){Name="我方代交",Value="我方代交",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" } },
            new Reason(){Name="转格式",Value="转格式",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" } },
            new Reason(){Name="撰写中客户取消申请",Value="撰写中客户取消申请",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" }},
            new Reason(){Name="中-英",Value="中-英",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" , "p.DoItem==\"翻译\"" } },
            new Reason(){Name="英-中",Value="英-中",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" , "p.DoItem==\"翻译\"" }},
            new Reason(){Name="英-德",Value="英-德",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" , "p.DoItem==\"翻译\"" }},
            new Reason(){Name="已算绩效",Value="已算绩效",Expresses= new List<string>(){ "p.DoItem==\"新申请\"" , "p.DoItem==\"翻译\"", "p.DoItem==\"处理审查意见\""  }}
        };

        public List<Reason> GetItems(PerformanceItem item)
        {
            List<Reason> retList = new List<Reason>();

            foreach(var r in _Reasons)
            {
                if(r.Expresses == null || r.Expresses.Count == 0)
                {
                    retList.Add(r);
                }
                else
                {
                    foreach(string strExpress in r.Expresses)
                    {
                        var interpreter = new Interpreter();
                        //item.ApplicationType
                        Func<PerformanceItem, bool> func = interpreter.ParseAsDelegate<Func<PerformanceItem, bool>>(strExpress, "p");
                        bool result = func.Invoke(item);

                        if (result)
                        {
                            retList.Add(r);
                            break;
                        }
                    }
                }
            }

            return retList;
        }
    }
}
