context: using Server.Gumps;
  using Server.Multis;
  using Server.Network;
  using Server.Targeting;
  using System;
  using System.Collections.Generic;
  
  namespace Server.Items
  {
      [FlipableAttribute(0x0FBF, 0x0FC0)]
      public class PenOfWisdom : Item, IUsesRemaining
      {
          public override int LabelNumber => 1115358;  // Pen of Wisdom		
          private int m_UsesRemaining;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public int UsesRemaining
          {
              get { return m_UsesRemaining; }
              set { m_UsesRemaining = value; InvalidateProperties(); }
          }
  
          [Constructable]
          public PenOfWisdom() : this(100)
          {
          }
  
          [Constructable]
          public PenOfWisdom(int uses)
              : base(0x0FC0)
          {
              LootType = LootType.Blessed;
              Weight = 1.0;
              m_UsesRemaining = uses;
              Hue = 1260;
              ShowUsesRemaining = true;
          }
  
          public virtual bool ShowUsesRemaining
          {
              get { return true; }
              set { }
          }
  
          public PenOfWisdom(Serial serial)
              : base(serial)
          {
          }
  
          public override void AddUsesRemainingProperties(ObjectPropertyList list)
          {
              list.Add(1060584, m_UsesRemaining.ToString()); // uses remaining: ~1_val~
          }
  
          public override void OnDoubleClick(Mobile from)
          {
              base.OnDoubleClick(from);
  
              if (IsChildOf(from.Backpack))
              {
                  from.SendLocalizedMessage(1115359); // Please select the source runebook. Recall runes and Mark scrolls at the base level of your backpack are consumed.
                  from.Target = new SourceTarget(this);
              }
              else
              {
                  from.SendLocalizedMessage(1062334); // This item must be in your backpack to be used.
              }
          }
  
          public override void Serialize(GenericWriter writer)
          {
              base.Serialize(writer);
              writer.Write(0); // version
  
              writer.Write(m_UsesRemaining);
          }
  
          public override void Deserialize(GenericReader reader)
          {
              base.Deserialize(reader);
              int version = reader.ReadInt();
  
              m_UsesRemaining = reader.ReadInt();
          }
  
          public bool CheckAccess(Mobile m, Runebook book)
          {
              if (!book.IsLockedDown || m.AccessLevel >= AccessLevel.GameMaster)
                  return true;
  
              BaseHouse house = BaseHouse.FindHouseAt(book);
  
              return house != null && house.IsCoOwner(m);
          }
  
          private class SourceTarget : Target
          {
              private readonly PenOfWisdom Pen;
  
              public SourceTarget(PenOfWisdom pen) : base(12, false, TargetFlags.None)
              {
                  Pen = pen;
              }
  
              protected override void OnTarget(Mobile from, object targ)
              {
                  if (targ is Runebook || targ is RunicAtlas)
                  {
                      Runebook book = targ as Runebook;
  
                      if (!book.IsChildOf(from.Backpack) && book.Movable)
                      {
                          from.SendLocalizedMessage(1115329); // Runebooks you wish to copy must be in your backpack.
                      }
                      else if (!Pen.CheckAccess(from, book) && !book.Movable)
                      {
                          from.SendLocalizedMessage(1115332); // Only the house owner and co-owners can copy the lockdowned runebook with the Pen.
                      }
                      else if (book.Entries.Count == 0)
                      {
                          from.SendLocalizedMessage(1115362); // Can't copy an empty runebook.
                      }
                      else if (book.Openers.Count != 0)
                      {
                          from.SendLocalizedMessage(1115361); // Someone else is using this runebook.
                      }
                      else
                      {
                          from.SendLocalizedMessage(1115363); // Please select the destination runebook.
                          from.Target = new CopyTarget(Pen, book);
                      }
                  }
                  else
                  {
                      from.SendLocalizedMessage(1115333); // This item only works on runebooks or runic atlas.
                  }
              }
          }
  
          private class CopyTarget : Target
          {
              private readonly PenOfWisdom Pen;
              private readonly Runebook SourceBook;
  
              public CopyTarget(PenOfWisdom pen, Runebook book) : base(12, false, TargetFlags.None)
              {
                  Pen = pen;
                  SourceBook = book;
              }
  
              protected override void OnTarget(Mobile from, object targ)
              {
                  if (targ is Runebook)
                  {
                      Runebook book = targ as Runebook;
  
                      if (!book.IsChildOf(from.Backpack))
                      {
                          from.SendLocalizedMessage(1062334); // This item must be in your backpack to be used.
                      }
                      else if (book == SourceBook)
                      {
                          from.SendLocalizedMessage(1115360); // You can't select the same runebook!
                      }
                      else if (book.Openers.Count != 0)
                      {
                          from.SendLocalizedMessage(1115361); // Someone else is using this runebook.
                      }
                      else
                      {
                          if (!from.HasGump(typeof(PenOfWisdomGump)))
                              from.SendGump(new PenOfWisdomGump(from, Pen, SourceBook, book, null));
                      }
                  }
                  else
                  {
                      from.SendLocalizedMessage(1115333); // This item only works on runebooks or runic atlas.
                  }
              }
          }
      }
  
      public class PenOfWisdomGump : Gump
      {
          public readonly int EntryColor = 0xFFFFFF;
  
          public readonly List<RunebookEntry> Checked;
          public readonly Runebook SourceBook;
          public readonly Runebook CopyBook;
          public readonly int RuneAmount;
          public readonly int MarkScrollAmount;
          public readonly int Blank;
          public readonly PenOfWisdom Pen;
  
          public PenOfWisdomGump(Mobile from, PenOfWisdom pen, Runebook sourcebook, Runebook copybook, List<RunebookEntry> list)
              : base(50, 50)
          {
              Container bp = from.Backpack;
  
              Pen = pen;
              SourceBook = sourcebook;
              CopyBook = copybook;
              MarkScrollAmount = bp.GetAmount(typeof(MarkScroll), true);
              RuneAmount = bp.GetAmount(typeof(RecallRune), true);
              Blank = copybook.MaxEntries - copybook.Entries.Count;
  
  
gt:             if (list == null)