context: /* Author: David Carroll
   * Copyright (c) 2008, 2009 Bellevue Baptist Church
   * Licensed under the GNU General Public License (GPL v2)
   * you may not use this code except in compliance with the License.
   * You may obtain a copy of the License at http://bvcms.codeplex.com/license
   */
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Web;
  using CmsData;
  using CmsData.Codes;
  using CmsWeb.Code;
  using UtilityExtensions;
  
  namespace CmsWeb.Areas.Search.Models
  {
      public class SearchAddModel : SearchResultsModel
      {
          private readonly string[] noaddtypes = {"relatedfamily", "mergeto", "contactor", "taskdelegate", "taskowner", "taskdelegate2", "addtoemail" };
          private readonly string[] onlyonetypes = {"taskdelegate", "taskowner", "taskdelegate2", "mergeto", "relatedfamily"};
          private Organization org;
  
          public SearchAddModel()
          {
              PendingList = new List<PendingPersonModel>();
              DisplaySkipSearch = true;
          }
  
          public SearchAddModel(string context, string contextid, bool displaySkipSearch = true)
              : this()
          {
              DisplaySkipSearch = displaySkipSearch;
              AddContext = context;
              PrimaryKeyForContextType = contextid;
              CampusId = null;
              switch (AddContext.ToLower())
              {
                  case "addpeople":
                  case "menu":
                      EntryPointId = 0;
                      break;
                  case "addtotag":
                      EntryPointId = null;
                      break;
                  case "mergeto":
                      EntryPointId = null;
                      break;
                  case "family":
                  case "relatedfamily":
                      EntryPointId = 0;
                      break;
                  case "org":
                  case "pending":
                  case "prospect":
                  case "inactive":
                      org = DbUtil.Db.LoadOrganizationById(contextid.ToInt());
                      CampusId = org.CampusId;
                      EntryPointId = org.EntryPointId ?? 0;
                      break;
                  case "visitor":
                  case "registered":
                      org = (from meeting in DbUtil.Db.Meetings
                             where meeting.MeetingId == contextid.ToInt()
                             select meeting.Organization).Single();
                      EntryPointId = org.EntryPointId ?? 0;
                      CampusId = org.CampusId;
                      break;
                  case "contactee":
                      EntryPointId = 0;
                      break;
                  case "contactor":
                      EntryPointId = 0;
                      break;
                  case "contributor":
                      EntryPointId = 0;
                      break;
                  case "addtoemail":
                      EntryPointId = 0;
                      break;
  
                      
              }
          }
  
          public List<PendingPersonModel> PendingList { get; set; }
  
          public string DialogTitle
          {
              get
              {
                  switch (AddContext.ToLower())
                  {
                      case "addpeople":
                      case "menu":
                          return "Add to Database";
                      case "mergeto":
                          return "Merge Duplicate Into";
                      case "addtotag":
                          return "Add to Tag";
                      case "family":
                          return "Add to Family";
                      case "relatedfamily":
                          return "Add as Related Family";
                      case "org":
                          return "Add as Member of Organization";
                      case "prospect":
                          return "Add as Prospect of Organization";
                      case "pending":
                          return "Add as Pending Member of Organization";
                      case "inactive":
                          if (org == null)
                              org = DbUtil.Db.LoadOrganizationById(PrimaryKeyForContextType.ToInt());
                          return $"Add as {(org.IsMissionTrip == true ? "Sender of Mission Trip" : "Inactive Member of Organization")}";
                      case "visitor":
                          return "Add as Visitor to Meeting";
                      case "registered":
                          return "Add as Registered for Future Meeting";
                      case "contactee":
                          return "Add as Contactee for Ministry Contact";
                      case "contactor":
                          return "Add as Contactor for Ministry Contact";
                      case "contributor":
                          return "Add for a Gift";
                      case "taskdelegate":
                      case "taskdelegate2":
                          return "Delegate Task";
                      case "taskowner":
                          return "Transfer Task Ownership";
                      case "taskabout":
                          return "Change who Task is Regarding";
                      case "addtoemail":
                          return "Add To Email";
                  }
                  return "";
              }
          }
  
          public string PrimaryKeyForContextType { get; set; }
  
          public bool CanAdd => !noaddtypes.Contains(AddContext.ToLower());
  
          public bool OnlyOne => onlyonetypes.Contains(AddContext.ToLower());
  
          public int NewFamilyId { get; set; }
          public int? EntryPointId { get; set; }
          public int? CampusId { get; set; }
          public int Index { get; set; }
          public bool DisplaySkipSearch { get; set; }
  
          public int NextNewFamilyId()
          {
              NewFamilyId--;
              return NewFamilyId;
          }
  
          public PendingPersonModel NewPerson(int familyid)
          {
              var p = new PendingPersonModel
              {
                  FamilyId = familyid,
                  index = PendingList.Count,
                  Gender = new CodeInfo(99, "Gender"),
                  MaritalStatus = new CodeInfo(99, "MaritalStatus"),
                  Campus = new CodeInfo(CampusId, "Campus"),
                  EntryPoint = new CodeInfo(EntryPointId, "EntryPoint"),
                  context = AddContext
              };
              if (familyid == 0)
              {
                  p.FamilyId = NextNewFamilyId();
                  p.IsNewFamily = true;
              }
  #if DEBUG
              p.FirstName = "David";
              p.LastName = "Carr." + DateTime.Now.Millisecond;
  #endif
              PendingList.Add(p);
              return p;
          }
  
          public static string AddRelatedFamily(int peopleid, int relatedPersonId)
          {
              var p = DbUtil.Db.LoadPersonById(peopleid);
              var rf = p.AddRelated(DbUtil.Db, relatedPersonId);
              return $"#rf-{rf.FamilyId}-{rf.RelatedFamilyId}";
          }
  
          internal void AddExisting(int id)
          {
              var p = DbUtil.Db.LoadPersonById(id);
              var pp = new PendingPersonModel();
              pp.CopyPropertiesFrom(p);
              pp.LoadAddress();
              PendingList.Add(pp);
          }
  
          internal dynamic CommitAdd()
          {
              var id = PrimaryKeyForContextType;
              var iid = PrimaryKeyForContextType.ToInt();
              switch (AddContext.ToLower())
              {
                  case "menu":
                  case "addpeople":
                      return AddPeople(OriginCode.MainMenu);
                  case "addtotag":
                      return AddPeopleToTag(id, 0);
                  case "family":
                      return AddFamilyMembers(iid, OriginCode.NewFamilyMember);
                  case "relatedfamily":
                      return AddRelatedFamilys(iid, OriginCode.NewFamilyMember);
                  case "org":
                      return AddOrgMembers(iid, OriginCode.Enrollment);
                  case "pending":
                      return AddOrgMembers(iid, OriginCode.Enrollment, pending: true);
                  case "inactive":
                      return AddOrgMembers(iid, OriginCode.Enrollment, MemberTypeCode.InActive);
                  case "prospect":
                  case "prospects":
                      return AddOrgMembers(iid, OriginCode.Enrollment, MemberTypeCode.Prospect);
                  case "visitor":
                      return AddVisitors(iid, OriginCode.Visit);
                  case "registered":
                      return AddRegistered(iid, OriginCode.Visit);
                  case "contactee":
                      return AddContactees(iid, OriginCode.Visit);
                  case "contactor":
                      return AddContactors(iid, 0);
                  case "contributor":
                      return AddContributor(iid, OriginCode.Contribution);
                  case "taskdelegate":
                      if (PendingList.Count > 0)
                          return new ReturnResult {close = true, how = "addselected", url = "/Task/Delegate/", pid = PendingList[0].PeopleId, from = AddContext};
                      break;
                  case "taskdelegate2":
  
gt:                     if (PendingList.Count > 0)