using Microsoft.EntityFrameworkCore;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.entity;

namespace wispro.sp.api.Job
{
    public class UpdateJXDataFromIPEasyJob: IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            spDbContext spDb = new spDbContext();

            var lstItem = spDb.PerformanceItems.Where<PerformanceItem>(p =>
                (p.DoItem == "新申请" && (p.CaseCoefficient == null || p.CaseCoefficient == "") && p.CalMonth.Status ==0) ||
                (p.DoItem == "处理审查意见" && (p.CaseStage == "一通" || p.CaseStage == "二通") && (p.DoItemCoefficient == null || p.DoItemCoefficient == "") && p.CalMonth.Status == 0))
                .Include(p=>p.Reviewer)
                .ToList<PerformanceItem>()
                ;

            if(lstItem != null)
            {
                int i = 0;
                foreach(var Item in lstItem)
                {
                    System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{++i}\t{Item.CaseNo}");
                    int iTryCount = 0;    
                TryAgain:
                    try
                    {
                        iTryCount++;
                        dynamic retObj = utility.IPEasyUtility.GetPerformanceRecord(Item.CaseNo, Item.DoItem,string.IsNullOrEmpty(Item.CaseStage)?null: Item.CaseStage);


                        IDictionary<String, Object> keyValuePairs = (IDictionary<String, Object>)retObj;

                        if (keyValuePairs.ContainsKey("DoItemCoefficient") && Item.DoItemCoefficient != retObj.DoItemCoefficient)
                        {
                            Item.DoItemCoefficient = retObj.DoItemCoefficient;
                        }

                        if (keyValuePairs.ContainsKey("DoItemMemo") && Item.DoItemMemo != retObj.DoItemMemo && !string.IsNullOrEmpty(retObj.DoItemMemo))
                        {
                            Item.DoItemMemo = retObj.DoItemMemo;
                        }

                        if (keyValuePairs.ContainsKey("DoItemState") && Item.DoItemState != retObj.DoItemState && !string.IsNullOrEmpty(retObj.DoItemState))
                        {
                            Item.DoItemState = retObj.DoItemState;
                        }

                        if (keyValuePairs.ContainsKey("CustomerLimitDate"))
                        {
                            if (!string.IsNullOrEmpty(retObj.CustomerLimitDate))
                            {
                                DateTime date = DateTime.Parse(retObj.CustomerLimitDate);

                                if (date != Item.CustomerLimitDate)
                                    Item.CustomerLimitDate = date;
                            }
                        }

                        if (keyValuePairs.ContainsKey("EntrustingDate"))
                        {
                            if (!string.IsNullOrEmpty(retObj.EntrustingDate))
                            {
                                DateTime date = DateTime.Parse(retObj.EntrustingDate);

                                if (date != Item.EntrustingDate)
                                    Item.EntrustingDate = date;
                            }
                        }

                        if (keyValuePairs.ContainsKey("FinalizationDate"))
                        {
                            if (!string.IsNullOrEmpty(retObj.FinalizationDate))
                            {
                                DateTime date = DateTime.Parse(retObj.FinalizationDate);

                                if (date != Item.FinalizationDate)
                                    Item.FinalizationDate = date;
                            }
                        }

                        if (keyValuePairs.ContainsKey("FinishedDate"))
                        {
                            if (!string.IsNullOrEmpty(retObj.FinishedDate))
                            {
                                DateTime date = DateTime.Parse(retObj.FinishedDate);

                                if (date != Item.FinishedDate)
                                    Item.FinishedDate = date;
                            }
                        }

                        if (keyValuePairs.ContainsKey("FirstDraftDate"))
                        {
                            if (!string.IsNullOrEmpty(retObj.FirstDraftDate))
                            {
                                DateTime date = DateTime.Parse(retObj.FirstDraftDate);

                                if (date != Item.FirstDraftDate)
                                    Item.FirstDraftDate = date;
                            }
                        }

                        if (keyValuePairs.ContainsKey("InternalDate"))
                        {
                            if (!string.IsNullOrEmpty(retObj.InternalDate))
                            {
                                DateTime date = DateTime.Parse(retObj.InternalDate);

                                if (date != Item.InternalDate)
                                    Item.InternalDate = date;
                            }
                        }

                        if (keyValuePairs.ContainsKey("ReturnDate"))
                        {
                            if (!string.IsNullOrEmpty(retObj.ReturnDate))
                            {
                                DateTime date = DateTime.Parse(retObj.ReturnDate);

                                if (date != Item.ReturnDate)
                                    Item.ReturnDate = date;
                            }
                        }

                        
                        if (keyValuePairs.ContainsKey("Reviewer") && (Item.Reviewer ==null || Item.Reviewer.Name != retObj.Reviewer) && !string.IsNullOrEmpty(retObj.Reviewer))
                        {
                            string name = retObj.Reviewer;

                            if (!string.IsNullOrEmpty(name))
                            {
                                var temReviewer = spDb.Staffs.Where<Staff>(s => s.Name == name).FirstOrDefault();
                                if (temReviewer == null)
                                {
                                    //Item.Reviewer = new Staff() { Name = retObj.Reviewer };
                                }
                                else
                                {
                                    //Item.Reviewer = temReviewer;
                                    //Item.ReviewerId = temReviewer.Id;
                                }
                            }
                        }

                        if (keyValuePairs.ContainsKey("ApplicationType") && Item.ApplicationType != retObj.ApplicationType && !string.IsNullOrEmpty(retObj.ApplicationType))
                        {
                            Item.ApplicationType = retObj.ApplicationType;
                        }

                        if (keyValuePairs.ContainsKey("BusinessType") && Item.BusinessType != retObj.BusinessType && !string.IsNullOrEmpty(retObj.BusinessType))
                        {
                            Item.BusinessType = retObj.BusinessType;
                        }

                        if (keyValuePairs.ContainsKey("CaseCoefficient") && Item.CaseCoefficient != retObj.CaseCoefficient && !string.IsNullOrEmpty(retObj.CaseCoefficient))
                        {
                            if (!string.IsNullOrEmpty(retObj.CaseCoefficient)){
                                Item.CaseCoefficient = retObj.CaseCoefficient;
                            }
                        }

                        //if (keyValuePairs.ContainsKey("CaseMemo") && Item.CaseMemo != retObj.CaseMemo && !string.IsNullOrEmpty(retObj.CaseMemo))
                        //{
                        //    Item.CaseMemo = retObj.CaseMemo;
                        //}

                        if (keyValuePairs.ContainsKey("CaseStage") && Item.CaseStage != retObj.CaseStage && !string.IsNullOrEmpty(retObj.CaseStage))
                        {
                            Item.CaseStage = retObj.CaseStage;
                        }

                        if (keyValuePairs.ContainsKey("CaseState") && Item.CaseState != retObj.CaseState && !string.IsNullOrEmpty(retObj.CaseState))
                        {
                            Item.CaseState = retObj.CaseState;
                        }

                        if (keyValuePairs.ContainsKey("CaseType") && Item.CaseType != retObj.CaseType && !string.IsNullOrEmpty(retObj.CaseType))
                        {
                            Item.CaseType = retObj.CaseType;
                        }

                        if (spDb.Entry(Item).State != EntityState.Unchanged)
                        {
                            Utility.Utility.CalBasePoint(Item, spDb.BasePointRules.ToList());
                            spDb.SaveChanges();
                        }
                    }
                    catch(Exception ex)
                    {
                        if(iTryCount <= 3)
                        {
                            goto TryAgain;
                        }

                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                    }
                }
            }

            return Task.CompletedTask;
        }
    }
}
