context: using Server.ContextMenus;
  using Server.Engines.Craft;
  using Server.Engines.VeteranRewards;
  using Server.Gumps;
  using Server.Multis;
  using Server.Network;
  using Server.Targeting;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace Server.Items
  {
      public class RepairBenchComponent : LocalizedAddonComponent
      {
          public override bool ForceShowProperties => true;
  
          public RepairBenchComponent(int id)
              : base(id, 1158860) // Repair Bench
          {
          }
  
          public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
          {
              base.GetContextMenuEntries(from, list);
  
              SetSecureLevelEntry.AddTo(from, Addon, list);
          }
  
          public RepairBenchComponent(Serial serial)
              : base(serial)
          {
          }
  
          public override void Serialize(GenericWriter writer)
          {
              base.Serialize(writer);
              writer.Write(0); // Version
          }
  
          public override void Deserialize(GenericReader reader)
          {
              base.Deserialize(reader);
              int version = reader.ReadInt();
          }
      }
  
      public class RepairBenchAddon : BaseAddon, ISecurable
      {
          public List<RepairBenchDefinition> Tools;
  
          public static RepairBenchDefinition[] Definitions = new RepairBenchDefinition[]
          {
              new RepairBenchDefinition(DefTinkering.CraftSystem, RepairSkillType.Tinkering, 1044097, 0, 0),
              new RepairBenchDefinition(DefBlacksmithy.CraftSystem, RepairSkillType.Smithing, 1044067, 0, 0),
              new RepairBenchDefinition(DefCarpentry.CraftSystem, RepairSkillType.Carpentry, 1044071, 0, 0),
              new RepairBenchDefinition(DefTailoring.CraftSystem, RepairSkillType.Tailoring, 1044094, 0, 0),
              new RepairBenchDefinition(DefMasonry.CraftSystem, RepairSkillType.Masonry, 1072392, 0, 0),
              new RepairBenchDefinition(DefGlassblowing.CraftSystem, RepairSkillType.Glassblowing, 1072393, 0, 0),
              new RepairBenchDefinition(DefBowFletching.CraftSystem, RepairSkillType.Fletching, 1015156, 0, 0)
          };
  
          public static RepairBenchDefinition GetInfo(RepairSkillType type)
          {
              return Definitions.ToList().Find(x => x.Skill == type);
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool IsRewardItem { get; set; }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public SecureLevel Level { get; set; }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public Mobile User { get; set; }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool Using
          {
              get
              {
                  if (User != null)
                  {
                      if (User.InRange(Location, 2))
                      {
                          return true;
                      }
  
                      User = null;
                  }
  
                  return false;
              }
          }
  
          public override bool HandlesOnMovement => User != null;
  
          public override void OnMovement(Mobile m, Point3D oldLocation)
          {
              if (m == User && !m.InRange(GetWorldLocation(), 2))
              {
                  User = null;
  
                  m.CloseGump(typeof(ConfirmRemoveGump));
                  m.CloseGump(typeof(RepairBenchGump));
                  Target.Cancel(m);
              }
          }
  
          [Constructable]
          public RepairBenchAddon(DirectionType type, List<RepairBenchDefinition> tools)
          {
              switch (type)
              {
                  case DirectionType.South:
                      AddComponent(new RepairBenchComponent(0xA278), 0, 0, 0);
                      break;
                  case DirectionType.East:
                      AddComponent(new RepairBenchComponent(0xA27F), 0, 0, 0);
                      break;
              }
  
              if (tools == null)
              {
                  Tools = new List<RepairBenchDefinition>();
  
                  Definitions.ToList().ForEach(x =>
                  {
                      Tools.Add(x);
                  });
              }
              else
              {
                  Tools = tools;
              }
          }
  
          public RepairBenchAddon(Serial serial)
              : base(serial)
          {
          }
  
          public void AccessibleFailMessage(Mobile from)
          {
              Components.FirstOrDefault().SendLocalizedMessageTo(from, 1061637); // You are not allowed to access this.
          }
  
          public bool CheckAccessible(Mobile from, Item item)
          {
              if (from.AccessLevel >= AccessLevel.GameMaster)
                  return true; // Staff can access anything
  
              BaseHouse house = BaseHouse.FindHouseAt(item);
  
              if (house == null)
                  return false;
  
              switch (Level)
              {
                  case SecureLevel.Owner: return house.IsOwner(from);
                  case SecureLevel.CoOwners: return house.IsCoOwner(from);
                  case SecureLevel.Friends: return house.IsFriend(from);
                  case SecureLevel.Anyone: return true;
                  case SecureLevel.Guild: return house.IsGuildMember(from);
              }
  
              return false;
          }
  
          public override BaseAddonDeed Deed
          {
              get
              {
                  RepairBenchDeed deed = new RepairBenchDeed(Tools)
                  {
                      IsRewardItem = IsRewardItem
                  };
  
                  return deed;
              }
          }
  
          public override void OnComponentUsed(AddonComponent c, Mobile from)
          {
              if ((from.InRange(c.Location, 2)))
              {
                  BaseHouse house = BaseHouse.FindHouseAt(from);
  
                  if (house != null)
                  {
                      if (CheckAccessible(from, this))
                      {
                          if (from.HasGump(typeof(RepairBenchGump)))
                              return;
  
                          if (!Using || from == User)
                          {
                              User = from;
                              from.CloseGump(typeof(RepairBenchGump));
                              from.SendGump(new RepairBenchGump(from, this));
                          }
                          else
                          {
                              from.SendLocalizedMessage(500291); // Someone else is using that.
                          }
                      }
                      else
                      {
                          AccessibleFailMessage(from);
                      }
                  }
                  else
                  {
                      from.SendLocalizedMessage(502092); // You must be in your house to do this.
                  }
              }
              else
              {
                  from.SendLocalizedMessage(500325); // I am too far away to do that.
              }
          }
  
          public override void Serialize(GenericWriter writer)
          {
              base.Serialize(writer);
              writer.Write(1);
  
              writer.Write((int)Level);
  
              writer.Write(IsRewardItem);
  
              writer.Write(Tools == null ? 0 : Tools.Count);
  
              if (Tools != null)
              {
                  Tools.ForEach(x =>
                  {
                      writer.Write((int)x.Skill);
                      writer.Write((int)x.SkillValue);
                      writer.Write(x.Charges);
                  });
              }
          }
  
          public override void Deserialize(GenericReader reader)
          {
              base.Deserialize(reader);
              int version = reader.ReadInt();
  
              if (version != 0)
                  Level = (SecureLevel)reader.ReadInt();
  
              IsRewardItem = reader.ReadBool();
  
              Tools = new List<RepairBenchDefinition>();
  
              int toolcount = reader.ReadInt();
  
              for (int x = 0; x < toolcount; x++)
              {
                  RepairSkillType skill = (RepairSkillType)reader.ReadInt();
                  int skillvalue = reader.ReadInt();
                  int charge = reader.ReadInt();
  
                  Tools.Add(new RepairBenchDefinition(GetInfo(skill).System, skill, GetInfo(skill).Cliloc, skillvalue, charge));
              }
          }
      }
  
      public class RepairBenchDeed : BaseAddonDeed, IRewardItem, IRewardOption
      {
          public override int LabelNumber => 1158860;  // Repair Bench
  
          public override BaseAddon Addon
          {
              get
              {
                  RepairBenchAddon addon = new RepairBenchAddon(_Direction, Tools)
                  {
                      IsRewardItem = m_IsRewardItem
                  };
  
                  return addon;
              }
          }
  
          private DirectionType _Direction;
  
          private bool m_IsRewardItem;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool IsRewardItem
          {
              get { return m_IsRewardItem; }
              set
              {
                  m_IsRewardItem = value;
                  InvalidateProperties();
              }
          }
  
          public List<RepairBenchDefinition> Tools;
  
          [Constructable]
          public RepairBenchDeed()
              : this(null)
          {
          }
  
          [Constructable]
          public RepairBenchDeed(List<RepairBenchDefinition> tools)
              : base()
          {
              Tools = tools;
              LootType = LootType.Blessed;
          }
  
          public RepairBenchDeed(Serial serial)
              : base(serial)
          {
          }
  
          public override void GetProperties(ObjectPropertyList list)
          {
              base.GetProperties(list);
  
              if (m_IsRewardItem)
                  list.Add(1076221); // 5th Year Veteran Reward
  
              if (Tools != null)
              {
                  int[] value = Tools.Select(x => x.Charges).ToArray();
                  list.Add(1158899, string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", value[0], value[1], value[2], value[3], value[4], value[5], value[6])); // Tinkering: ~1_CHARGES~<br>Blacksmithing: ~2_CHARGES~<br>Carpentry: ~3_CHARGES~<br>Tailoring: ~4_CHARGES~<br>Fletching: ~5_CHARGES~<br>Masonry: ~6_CHARGES~<br>Glassblowing: ~7_CHARGES~
              }
          }
  
          public void GetOptions(RewardOptionList list)
          {
              list.Add((int)DirectionType.South, 1075386); // South
              list.Add((int)DirectionType.East, 1075387); // East
          }
  
          public void OnOptionSelected(Mobile from, int choice)
          {
              _Direction = (DirectionType)choice;
  
              if (!Deleted)
                  base.OnDoubleClick(from);
          }
  
          public override void OnDoubleClick(Mobile from)
          {
              if (IsChildOf(from.Backpack))
              {
                  from.CloseGump(typeof(AddonOptionGump));
                  from.SendGump(new AddonOptionGump(this, 1154194)); // Choose a Facing:
              }
              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);
  
              writer.Write(m_IsRewardItem);
  
              writer.Write(Tools == null ? 0 : Tools.Count);
  
              if (Tools != null)
              {
                  Tools.ForEach(x =>
                  {
                      writer.Write((int)x.Skill);
                      writer.Write((int)x.SkillValue);
                      writer.Write(x.Charges);
                  });
              }
          }
  
          public override void Deserialize(GenericReader reader)
          {
              base.Deserialize(reader);
              int version = reader.ReadInt();
  
              m_IsRewardItem = reader.ReadBool();
  
              int toolcount = reader.ReadInt();
  
              if (toolcount != 0)
              {
  
gt:                 Tools = new List<RepairBenchDefinition>();