using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.api.Controllers;
using wispro.sp.entity;
using wispro.sp.share;

namespace wispro.sp.api.AppealHandler
{
    public class ChangeDoPersonReviewer : IDoAppealObject
    {
        public void DoAppeal(AppealObject appeal, int appealRecordId, DbContext spContext)
        {
            bool isAggree = false;
            foreach (var iv in appeal.inputFieldValues)
            {
                if (iv.InputField == null)
                {
                    iv.InputField = ((spDbContext)spContext).InputFields.FirstOrDefault(s => s.Id == iv.InputFieldId);
                }

                if (iv.InputField.FieldName == "审核意见" && iv.Value == "确认变更")
                {
                    isAggree = true;
                    break;
                }
                else
                {
                    if(iv.InputField.FieldName == "审核意见" && string.IsNullOrEmpty(iv.Value))
                    {
                        throw (new ApplicationException("请填写审核意见!"));
                    }
                }

            }
            if (isAggree)
            {
                var result = ((spDbContext)spContext).InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == appealRecordId && f.InputField.AppealState == 0).ToList();

                string strOldPerson = "";
                string strNewPerson = "";
                
                foreach (var iv in result)
                {
                    if (iv.InputField == null)
                    {
                        iv.InputField = ((spDbContext)spContext).InputFields.FirstOrDefault(s => s.Id == iv.InputFieldId);
                    }

                    if (iv.InputField.FieldName == "变更前处理人")
                    {
                        strOldPerson = iv.Value;
                        continue;
                    }

                    if (iv.InputField.FieldName == "变更后处理人")
                    {
                        strNewPerson = iv.Value;
                        continue;
                    }

                }

                var appRecord = ((spDbContext)spContext).AppealRecords.FirstOrDefault(f => f.Id == appealRecordId);

                if(appRecord != null)
                {
                    var itemStaffs = ((spDbContext)spContext).ItemStaffs.Include(p=>p.DoPerson).Where(s => s.ItemId == appRecord.ItemId.Value);

                    var oldPersons = strOldPerson.Split(new char[] { ' ', ',', '，' },StringSplitOptions.RemoveEmptyEntries);
                    var newPersons = strNewPerson.Split(new char[] { ' ', ',', '，' }, StringSplitOptions.RemoveEmptyEntries);
                    
                    bool oldPersonExist = true;
                    List<ItemStaff> waitingRemoveObj = new List<ItemStaff>();

                    foreach(var temName in oldPersons)
                    {
                        var temItemStaff = itemStaffs.Where(s => s.DoPerson.Name == temName).FirstOrDefault();
                        if(temItemStaff == null)
                        {
                            oldPersonExist = false;
                            break;
                        }
                        else
                        {
                            waitingRemoveObj.Add(temItemStaff);
                        }
                    }
                    //foreach (ItemStaff staff in itemStaffs)
                    //{
                    //    if (oldPersons.Contains(staff.DoPerson.Name))
                    //    {
                    //        waitingRemoveObj.Add(staff);
                            
                    //        oldPersonExist = true;
                            
                    //    }
                    //    else
                    //    {
                    //        oldPersonExist = false;
                    //        break;
                    //    }
                    //}

                    

                    if (oldPersonExist)
                    {
                        ((spDbContext)spContext).ItemStaffs.RemoveRange(waitingRemoveObj);
                        ((spDbContext)spContext).SaveChanges();
                        foreach (var temName in newPersons)
                        {
                            var newStaff = ((spDbContext)spContext).Staffs.FirstOrDefault(s => s.Name == temName.Trim());
                            
                            if (newStaff != null )
                            {
                                var temItemStaff = ((spDbContext)spContext).ItemStaffs.Where(s => s.ItemId == appRecord.ItemId.Value && s.DoPersonId == newStaff.Id).FirstOrDefault();

                                if (temItemStaff == null)
                                {
                                    ((spDbContext)spContext).ItemStaffs.Add(new ItemStaff()
                                    {
                                        ItemId = appRecord.ItemId.Value,
                                        DoPersonId = newStaff.Id
                                    });
                                }
                            }
                            else
                            {
                                throw new ApplicationException("变更后处理人不存在！");
                            }
                        }
                    }
                    else
                    {
                        throw new ApplicationException("变更前处理人不存在！");
                    }

                }
            }
        }
    }
}
