context: using Server.ContextMenus;
  using Server.Items;
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace Server.Mobiles
  {
      public class BaseHire : BaseCreature
      {
          public override bool IsBondable => false;
          public override bool CanAutoStable => false;
          public override bool CanDetectHidden => false;
          public override bool KeepsItemsOnDeath => true;
  
          private bool _IsHired;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public DateTime NextPay { get; set; }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public int Pay => PerDayCost();
  
          [CommandProperty(AccessLevel.GameMaster)]
          public int HoldGold { get; set; }
  
          public int GoldOnDeath { get; set; }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool IsHired
          {
              get { return _IsHired; }
              set
              {
                  _IsHired = value;
  
                  Delta(MobileDelta.Noto);
                  InvalidateProperties();
              }
          }
  
          public BaseHire(AIType AI)
              : base(AI, FightMode.Aggressor, 10, 1, 0.1, 4.0)
          {
              ControlSlots = 2;
              HoldGold = 8;
          }
  
          public BaseHire()
              : base(AIType.AI_Melee, FightMode.Aggressor, 10, 1, 0.1, 4.0)
          {
              ControlSlots = 2;
          }
  
          public BaseHire(Serial serial)
              : base(serial)
          {
          }
  
          public override void Serialize(GenericWriter writer)
          {
              base.Serialize(writer);
  
              writer.Write(1);// version
  
              writer.Write(NextPay);
              writer.Write(IsHired);
              writer.Write(HoldGold);
          }
  
          public override void Deserialize(GenericReader reader)
          {
              base.Deserialize(reader);
  
              int version = reader.ReadInt();
  
              switch (version)
              {
                  case 1:
                      NextPay = reader.ReadDateTime();
                      goto case 0;
                  case 0:
                      IsHired = reader.ReadBool();
                      HoldGold = reader.ReadInt();
                      break;
              }
  
              if (IsHired)
              {
                  PayTimer.RegisterTimer(this);
              }
          }
  
          public override bool OnBeforeDeath()
          {
              if (Backpack != null)
              {
                  Item[] AllGold = Backpack.FindItemsByType(typeof(Gold), true);
  
                  if (AllGold != null)
                  {
                      foreach (Gold g in AllGold)
                      {
                          GoldOnDeath += g.Amount;
                      }
                  }
              }
  
              return base.OnBeforeDeath();
          }
  
          public override void Delete()
          {
              base.Delete();
  
              PayTimer.RemoveTimer(this);
          }
  
          public override void OnDeath(Container c)
          {
              if (GoldOnDeath > 0)
              {
                  c.DropItem(new Gold(GoldOnDeath));
              }
  
              base.OnDeath(c);
          }
  
          #region [ GetOwner ]
          public virtual Mobile GetOwner()
          {
              if (!Controlled)
              {
                  return null;
              }
  
              var owner = ControlMaster;
              IsHired = true;
  
              if (owner == null)
              {
                  return null;
              }
  
              if (owner.Deleted)
              {
                  Say(1005653, 0x3B2);// Hmmm.  I seem to have lost my master.
                  SetControlMaster(null);
                  return null;
              }
  
              return owner;
          }
  
          #endregion 
  
          #region [ AddHire ] 
          public virtual bool AddHire(Mobile m)
          {
              Mobile owner = GetOwner();
  
              if (owner != null)
              {
                  m.SendLocalizedMessage(1043283, owner.Name);// I am following ~1_NAME~. 
                  return false;
              }
  
              if (SetControlMaster(m))
              {
                  IsHired = true;
  
                  return true;
              }
  
              return false;
          }
  
          #endregion 
  
          #region [ PerDayCost ] 
          public int PerDayCost()
          {
              var pay = (int)Skills[SkillName.Anatomy].Value + (int)Skills[SkillName.Tactics].Value;
              pay += (int)Skills[SkillName.Macing].Value + (int)Skills[SkillName.Swords].Value;
              pay += (int)Skills[SkillName.Fencing].Value + (int)Skills[SkillName.Archery].Value;
              pay += (int)Skills[SkillName.MagicResist].Value + (int)Skills[SkillName.Healing].Value;
              pay += (int)Skills[SkillName.Magery].Value + (int)Skills[SkillName.Parry].Value;
              pay /= 35;
              pay += 1;
  
              return pay;
          }
  
          #endregion 
  
          #region [ OnDragDrop ]
          public override bool OnDragDrop(Mobile from, Item item)
          {
              if (Pay != 0)
              {
                  // Is the creature already hired
                  if (!Controlled)
                  {
                      // Is the item the payment in gold
                      if (item is Gold)
                      {
                          // Is the payment in gold sufficient
                          if (item.Amount >= Pay)
                          {
                              if (from.Followers + ControlSlots > from.FollowersMax)
                              {
                                  SayTo(from, 500896, 0x3B2); // I see you already have an escort.
                                  return false;
                              }
  
                              // Try to add the hireling as a follower
                              if (AddHire(from))
                              {
                                  SayTo(from, 1043258, string.Format("{0}", item.Amount / Pay), 0x3B2);//"I thank thee for paying me. I will work for thee for ~1_NUMBER~ days.", (int)item.Amount / Pay );
                                  HoldGold += item.Amount;
  
  
gt:                                 NextPay = DateTime.UtcNow + PayTimer.GetInterval();