context: using System;
  using Server.Gumps;
  using Server.Multis;
  using Server.Targeting;
  using Server.Mobiles;
  using Server.Network;
  using Server.Engines.VeteranRewards;
  
  namespace Server.Items
  {
      interface IEngravable
      {
          string EngravedText { get; set; }
      }
  
      public class BaseEngravingTool : Item, IUsesRemaining, IRewardItem
      {
          private int m_UsesRemaining;
          private bool m_IsRewardItem;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public int UsesRemaining
          {
              get { return m_UsesRemaining; }
              set
              {
                  m_UsesRemaining = value;
                  InvalidateProperties();
              }
          }
  
          public virtual bool ShowUsesRemaining
          {
              get { return true; }
              set { }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool IsRewardItem
          {
              get
              {
                  return m_IsRewardItem;
              }
              set
              {
                  m_IsRewardItem = value;
                  InvalidateProperties();
              }
          }
  
          [Constructable]
          public BaseEngravingTool(int itemID)
              : this(itemID, 1)
          {
          }
  
          [Constructable]
          public BaseEngravingTool(int itemID, int uses)
              : base(itemID)
          {
              Weight = 1.0;
              Hue = 0x48D;
  
              LootType = LootType.Blessed;
  
              m_UsesRemaining = uses;
          }
  
          public BaseEngravingTool(Serial serial)
              : base(serial)
          {
          }
  
          public virtual bool DeletedItem { get { return true; } }
          public virtual int LowSkillMessage { get { return 0; } }
          public virtual int VeteranRewardCliloc { get { return 0; } }
  
          public virtual Type[] Engraves { get { return null; } }
          public virtual int GumpTitle { get { return 1072359; } } // <CENTER>Engraving Tool</CENTER>
  
          public virtual int SuccessMessage { get { return 1072361; } } // You engraved the object.
          public virtual int TargetMessage { get { return 1072357; } } // Select an object to engrave.
          public virtual int RemoveMessage { get { return 1072362; } } // You remove the engraving from the object.
          public virtual int ReChargesMessage { get { return 1076166; } } // You do not have a blue diamond needed to recharge the engraving tool.
          public virtual int OutOfChargesMessage { get { return 1076163; } } // There are no charges left on this engraving tool.
          public virtual int NotAccessibleMessage { get { return 1072310; } } // The selected item is not accessible to engrave.
          public virtual int CannotEngraveMessage { get { return 1072309; } } // The selected item cannot be engraved by this engraving tool.
          public virtual int ObjectWasNotMessage { get { return 1072363; } } // The object was not engraved.        
  
          public virtual bool CheckType(IEntity entity)
          {
              if (Engraves == null || entity == null)
                  return false;
  
              Type type = entity.GetType();
  
              for (int i = 0; i < Engraves.Length; i++)
              {
                  if (type == Engraves[i] || type.IsSubclassOf(Engraves[i]))
                      return true;
              }
  
              return false;
          }
  
          public static BaseEngravingTool Find(Mobile from)
          {
              if (from.Backpack != null)
              {
                  BaseEngravingTool tool = from.Backpack.FindItemByType(typeof(BaseEngravingTool)) as BaseEngravingTool;
  
                  if (tool != null && !tool.DeletedItem && tool.UsesRemaining <= 0)
                      return tool;
                  else
                      return null;
              }
  
              return null;
          }
  
          public override void OnDoubleClick(Mobile from)
          {
              base.OnDoubleClick(from);
  
              if (!from.NetState.SupportsExpansion(Expansion.ML))
              {
                  from.SendLocalizedMessage(1072791); // You must upgrade to Mondain's Legacy in order to use that item.				
                  return;
              }
  
              if (m_UsesRemaining > 0)
              {
                  from.SendLocalizedMessage(TargetMessage);
                  from.Target = new InternalTarget(this);
              }
              else
              {
                  if (!DeletedItem)
                  {
                      if (CheckSkill(from))
                      {
                          Item diamond = from.Backpack.FindItemByType(typeof(BlueDiamond));
  
                          if (diamond != null)
                          {
                              from.SendGump(new ConfirmGump(this, null));
                          }
                          else
                          {
                              from.SendLocalizedMessage(ReChargesMessage);
                          }
                      }
                  }
  
                  from.SendLocalizedMessage(OutOfChargesMessage);
              }
          }
  
          private bool IsValid(IEntity entity, Mobile m)
          {
              if (entity is Item)
              {
                  Item item = entity as Item;
  
                  BaseHouse house = BaseHouse.FindHouseAt(item);
  
                  if (m.InRange(item.GetWorldLocation(), 3))
                  {
                      if (item.Movable && !item.IsLockedDown && !item.IsSecure && (item.RootParent == null || item.RootParent == m))
                      {
                          return true;
                      }
                      else if (house != null && house.IsFriend(m))
                      {
                          return true;
                      }
                  }
              }
              else if (entity is BaseCreature)
              {
                  BaseCreature bc = entity as BaseCreature;
  
                  if (bc.Controlled && bc.ControlMaster == m)
                      return true;
              }
  
              return false;
          }
  
          public bool CheckSkill(Mobile from)
          {
              if (from.Skills[SkillName.Tinkering].Value < 75.0)
              {
                  from.SendLocalizedMessage(LowSkillMessage);
                  return false;
              }
  
              return true;
          }
  
          public virtual void Recharge(Mobile from, Mobile guildmaster)
          {
              if (from.Backpack != null)
              {
                  Item diamond = from.Backpack.FindItemByType(typeof(BlueDiamond));
  
                  if (guildmaster != null)
                  {
                      if (m_UsesRemaining <= 0)
                      {
                          if (diamond != null && Banker.Withdraw(from, 100000))
                          {
                              diamond.Consume();
                              UsesRemaining = 10;
                              guildmaster.Say(1076165); // Your weapon engraver should be good as new!
                          }
                          else
                              guildmaster.Say(1076167); // You need a 100,000 gold and a blue diamond to recharge the weapon engraver.
                      }
                      else
                          guildmaster.Say(1076164); // I can only help with this if you are carrying an engraving tool that needs repair.
                  }
                  else
                  {
                      if (CheckSkill(from))
                      {
                          if (diamond != null)
                          {
                              diamond.Consume();
  
                              if (Utility.RandomDouble() < from.Skills[SkillName.Tinkering].Value / 100)
                              {
                                  UsesRemaining = 10;
                                  from.SendLocalizedMessage(1076165); // Your engraver should be good as new!
                              }
                              else
                                  from.SendLocalizedMessage(1076175); // You cracked the diamond attempting to fix the engraver.
                          }
                          else
                              from.SendLocalizedMessage(1076166); // You do not have a blue diamond needed to recharge the engraving tool.
                      }
                  }
              }
          }
  
          public override void GetProperties(ObjectPropertyList list)
          {
              base.GetProperties(list);
  
              if (m_IsRewardItem)
                  list.Add(VeteranRewardCliloc);	
          }
  
          public override void AddUsesRemainingProperties(ObjectPropertyList list)
          {
              if (ShowUsesRemaining)
              {
                  list.Add(1060584, m_UsesRemaining.ToString()); // uses remaining: ~1_val~
              }
          }   
  
          public override void Serialize(GenericWriter writer)
          {
              base.Serialize(writer);
              writer.Write((int)2); // version
  
              writer.Write((int)m_UsesRemaining);
              writer.Write((bool)m_IsRewardItem);
          }
  
          public override void Deserialize(GenericReader reader)
          {
              base.Deserialize(reader);
              int version = reader.ReadInt();
  
              switch (version)
              {
                  case 2:
                      {
                          m_UsesRemaining = reader.ReadInt();
                          m_IsRewardItem = reader.ReadBool();
                          break;
                      }
                  case 1:
                      {
                          m_UsesRemaining = reader.ReadInt();
                          break;
                      }
                  case 0:
                      {
                          if (this is WeaponEngravingTool)
                          {
                              InheritsItem = true;
                              m_UsesRemaining = reader.ReadInt();
                              m_IsRewardItem = reader.ReadBool();
                          }
                          else
                          {
                              LootType = LootType.Blessed;
                          }
                          break;
                      }
              }
          }
  
          #region Old Item Serialization Vars
          /* DO NOT USE! Only used in serialization of Weapon Engraving Tool that originally derived from Item */
          public bool InheritsItem { get; protected set; }
          #endregion
  
          private class InternalTarget : Target
          {
              private readonly BaseEngravingTool m_Tool;
  
              public InternalTarget(BaseEngravingTool tool)
                  : base(3, true, TargetFlags.None)
              {
                  m_Tool = tool;
              }
  
              protected override void OnTarget(Mobile from, object targeted)
              {
                  if (m_Tool == null || m_Tool.Deleted)
                      return;
  
                  if (targeted is IEntity)
                  {
                      IEntity entity = (IEntity)targeted;
  
                      if (m_Tool.IsValid(entity, from))
                      {
                          if (entity is IEngravable && m_Tool.CheckType(entity))
                          {
                              from.CloseGump(typeof(InternalGump));
  
gt:                             from.SendGump(new InternalGump(m_Tool, entity));