context: using Server.Engines.Craft;
  using Server.Network;
  using System;
  
  namespace Server.Items
  {
      public interface ITool : IEntity, IUsesRemaining
      {
          CraftSystem CraftSystem { get; }
  
          bool BreakOnDepletion { get; }
  
          bool CheckAccessible(Mobile from, ref int num);
      }
  
      public abstract class BaseTool : Item, ITool, IResource, IQuality
      {
          private Mobile m_Crafter;
          private ItemQuality m_Quality;
          private int m_UsesRemaining;
          private bool m_RepairMode;
          private CraftResource _Resource;
          private bool _PlayerConstructed;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public CraftResource Resource
          {
              get { return _Resource; }
              set
              {
                  _Resource = value;
                  Hue = CraftResources.GetHue(_Resource);
                  InvalidateProperties();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public Mobile Crafter
          {
              get { return m_Crafter; }
              set { m_Crafter = value; InvalidateProperties(); }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public ItemQuality Quality
          {
              get { return m_Quality; }
              set
              {
                  UnscaleUses();
                  m_Quality = value;
                  InvalidateProperties();
                  ScaleUses();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool PlayerConstructed
          {
              get { return _PlayerConstructed; }
              set
              {
                  _PlayerConstructed = value; InvalidateProperties();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public int UsesRemaining
          {
              get { return m_UsesRemaining; }
              set { m_UsesRemaining = value; InvalidateProperties(); }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool RepairMode
          {
              get { return m_RepairMode; }
              set { m_RepairMode = value; }
          }
  
          public void ScaleUses()
          {
              m_UsesRemaining = (m_UsesRemaining * GetUsesScalar()) / 100;
              InvalidateProperties();
          }
  
          public void UnscaleUses()
          {
              m_UsesRemaining = (m_UsesRemaining * 100) / GetUsesScalar();
          }
  
          public int GetUsesScalar()
          {
              if (m_Quality == ItemQuality.Exceptional)
                  return 200;
  
              return 100;
          }
  
          public bool ShowUsesRemaining
          {
              get { return true; }
              set { }
          }
  
          public virtual bool BreakOnDepletion => true;
  
          public abstract CraftSystem CraftSystem { get; }
  
          public BaseTool(int itemID)
              : this(Utility.RandomMinMax(25, 75), itemID)
          {
          }
  
          public BaseTool(int uses, int itemID)
              : base(itemID)
          {
              m_UsesRemaining = uses;
              m_Quality = ItemQuality.Normal;
          }
  
          public BaseTool(Serial serial)
              : base(serial)
          {
          }
  
          public override void AddCraftedProperties(ObjectPropertyList list)
          {
              if (m_Crafter != null)
                  list.Add(1050043, m_Crafter.TitleName); // crafted by ~1_NAME~
  
              if (m_Quality == ItemQuality.Exceptional)
                  list.Add(1060636); // exceptional
          }
  
          public override void AddUsesRemainingProperties(ObjectPropertyList list)
          {
              list.Add(1060584, UsesRemaining.ToString()); // uses remaining: ~1_val~
          }
  
          public virtual void DisplayDurabilityTo(Mobile m)
          {
              LabelToAffix(m, 1017323, AffixType.Append, ": " + m_UsesRemaining.ToString()); // Durability
          }
  
          public virtual bool CheckAccessible(Mobile m, ref int num)
          {
              if (RootParent != m)
              {
                  num = 1044263;
                  return false;
              }
  
              return true;
          }
  
          public static bool CheckAccessible(Item tool, Mobile m)
          {
              return CheckAccessible(tool, m, false);
          }
  
          public static bool CheckAccessible(Item tool, Mobile m, bool message)
          {
              if (tool == null || tool.Deleted)
              {
                  return false;
              }
  
              int num = 0;
  
              bool res;
  
              if (tool is ITool)
              {
                  res = ((ITool)tool).CheckAccessible(m, ref num);
              }
              else
              {
                  res = tool.IsChildOf(m) || tool.Parent == m;
              }
  
              if (num > 0 && message)
              {
                  m.SendLocalizedMessage(num);
              }
  
              return res;
          }
  
          public static bool CheckTool(Item tool, Mobile m)
          {
              if (tool == null || tool.Deleted)
              {
                  return false;
              }
  
              Item check = m.FindItemOnLayer(Layer.OneHanded);
  
              if (check is ITool && check != tool && !(check is AncientSmithyHammer))
                  return false;
  
              check = m.FindItemOnLayer(Layer.TwoHanded);
  
              if (check is ITool && check != tool && !(check is AncientSmithyHammer))
                  return false;
  
              return true;
          }
  
          public override void OnDoubleClick(Mobile from)
          {
  
gt:             if (IsChildOf(from.Backpack) || Parent == from)