context: using System;
  using System.Collections.Generic;
  using Server.ContextMenus;
  using Server.Engines.Craft;
  using Server.MainConfiguration;
  using Server.T2AConfiguration;
  using Server.Engines.XmlSpawner2;
  
  namespace Server.Items
  {
      public enum GemType
      {
          None,
          StarSapphire,
          Emerald,
          Sapphire,
          Ruby,
          Citrine,
          Amethyst,
          Tourmaline,
          Amber,
          Diamond
      }
  
      public abstract class BaseJewel : Item, ICraftable, ISetItem, IEngravable, IWearableDurability
      {
          private string m_EngravedText;
          [CommandProperty(AccessLevel.GameMaster)]
          public string EngravedText
          {
              get { return m_EngravedText; }
              set
              {
                  m_EngravedText = value;
                  InvalidateProperties();
              }
          }
  
          private bool m_Identified;
          [CommandProperty(AccessLevel.GameMaster)]
          public bool Identified
          {
              get { return m_Identified; }
              set { m_Identified = value; InvalidateProperties(); }
          }
          private int m_MaxHitPoints;
          private int m_HitPoints;
  
          private AosAttributes m_AosAttributes;
          private AosElementAttributes m_AosResistances;
          private AosSkillBonuses m_AosSkillBonuses;
          private SAAbsorptionAttributes m_SAAbsorptionAttributes;
          private NegativeAttributes m_NegativeAttributes;
          private CraftResource m_Resource;
          private GemType m_GemType;
  
          private int m_TimesImbued;
          private bool m_IsImbued;
          private bool m_BlockRepair;
          private ItemPower m_ItemPower;
          private ReforgedPrefix m_ReforgedPrefix;
          private ReforgedSuffix m_ReforgedSuffix;
  
          private Mobile m_BlessedBy;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public Mobile BlessedBy
          {
              get
              {
                  return this.m_BlessedBy;
              }
              set
              {
                  this.m_BlessedBy = value;
                  this.InvalidateProperties();
              }
          }
  
          public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
          {
              base.GetContextMenuEntries(from, list);
  
              if (this.BlessedFor == from && this.BlessedBy == from && this.RootParent == from)
              {
                  list.Add(new UnBlessEntry(from, this));
              }
          }
  
          private class UnBlessEntry : ContextMenuEntry
          {
              private readonly Mobile m_From;
              private readonly BaseJewel m_Item;
  
              public UnBlessEntry(Mobile from, BaseJewel item)
                  : base(6208, -1)
              {
                  this.m_From = from;
                  this.m_Item = item;
              }
  
              public override void OnClick()
              {
                  this.m_Item.BlessedFor = null;
                  this.m_Item.BlessedBy = null;
  
                  Container pack = this.m_From.Backpack;
  
                  if (pack != null)
                  {
                      pack.DropItem(new PersonalBlessDeed(this.m_From));
                      this.m_From.SendLocalizedMessage(1062200); // A personal bless deed has been placed in your backpack.
                  }
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public int MaxHitPoints
          {
              get
              {
                  return this.m_MaxHitPoints;
              }
              set
              {
                  this.m_MaxHitPoints = value;
                  this.InvalidateProperties();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public int HitPoints
          {
              get 
              {
                  return this.m_HitPoints;
              }
              set 
              {
                  if (value != this.m_HitPoints && this.MaxHitPoints > 0)
                  {
                      this.m_HitPoints = value;
  
                      if (this.m_HitPoints < 0)
                          this.Delete();
                      else if (this.m_HitPoints > this.MaxHitPoints)
                          this.m_HitPoints = this.MaxHitPoints;
  
                      this.InvalidateProperties();
                  }
              }
          }
  
          [CommandProperty(AccessLevel.Player)]
          public AosAttributes Attributes
          {
              get
              {
                  return this.m_AosAttributes;
              }
              set
              {
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public AosElementAttributes Resistances
          {
              get
              {
                  return this.m_AosResistances;
              }
              set
              {
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public AosSkillBonuses SkillBonuses
          {
              get
              {
                  return this.m_AosSkillBonuses;
              }
              set
              {
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public SAAbsorptionAttributes AbsorptionAttributes
          {
              get
              {
                  return this.m_SAAbsorptionAttributes;
              }
              set
              {
              }
          }
  
          [CommandProperty(AccessLevel.Player)]
          public NegativeAttributes NegativeAttributes
          {
              get
              {
                  return m_NegativeAttributes;
              }
              set
              {
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public CraftResource Resource
          {
              get
              {
                  return this.m_Resource;
              }
              set
              {
                  this.m_Resource = value;
                  this.Hue = CraftResources.GetHue(this.m_Resource);
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public GemType GemType
          {
              get
              {
                  return this.m_GemType;
              }
              set
              {
                  this.m_GemType = value;
                  this.InvalidateProperties();
              }
          }
          [CommandProperty(AccessLevel.GameMaster)]
          public int TimesImbued
          {
              get { return m_TimesImbued; }
              set { m_TimesImbued = value; InvalidateProperties(); }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool IsImbued
          {
              get
              {
                  if (TimesImbued >= 1 && !m_IsImbued)
                  {
                      m_IsImbued = true;
                  }
                  return m_IsImbued;
              }
              set
              {
                  if (TimesImbued >= 1)
                  {
                      m_IsImbued = true;
                  }
                  else
                  {
                      m_IsImbued = value; InvalidateProperties();
                  }
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public ReforgedPrefix ReforgedPrefix
          {
              get { return m_ReforgedPrefix; }
              set { m_ReforgedPrefix = value; InvalidateProperties(); }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public ReforgedSuffix ReforgedSuffix
          {
              get { return m_ReforgedSuffix; }
              set { m_ReforgedSuffix = value; InvalidateProperties(); }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool BlockRepair
          {
              get { return m_BlockRepair; }
              set { m_BlockRepair = value; InvalidateProperties(); }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public ItemPower ItemPower
          {
              get { return m_ItemPower; }
              set { m_ItemPower = value; InvalidateProperties(); }
          }
  
          public override int PhysicalResistance
          {
              get
              {
                  return this.m_AosResistances.Physical;
              }
          }
          public override int FireResistance
          {
              get
              {
                  return this.m_AosResistances.Fire;
              }
          }
          public override int ColdResistance
          {
              get
              {
                  return this.m_AosResistances.Cold;
              }
          }
          public override int PoisonResistance
          {
              get
              {
                  return this.m_AosResistances.Poison;
              }
          }
          public override int EnergyResistance
          {
              get
              {
                  return this.m_AosResistances.Energy;
              }
          }
          public virtual int BaseGemTypeNumber
          {
              get
              {
                  return 0;
              }
          }
  
          public virtual int InitMinHits
          {
              get
              {
                  return 0;
              }
          }
          public virtual int InitMaxHits
          {
              get
              {
                  return 0;
              }
          }
  
          public virtual bool Brittle { get { return false; } }
          public virtual bool NoRepairs { get { return false; } }
          public virtual bool CanImbue { get { return ArtifactRarity == 0; } }
  
          public virtual Race RequiredRace
          {
              get
              {
                  return null;
              }
          }
  
          public virtual bool CanBeWornByGargoyles { get { return true; } }
  
          public override int LabelNumber
          {
              get
              {
                  if (this.m_GemType == GemType.None)
                      return base.LabelNumber;
  
                  return this.BaseGemTypeNumber + (int)this.m_GemType - 1;
              }
          }
  
          public override double DefaultWeight
          {
              get
              {
                  if (NegativeAttributes == null || NegativeAttributes.Unwieldly == 0)
                      return base.DefaultWeight;
  
                  return base.DefaultWeight * 3;
              }
          }
  
          public override void OnAfterDuped(Item newItem)
          {
              BaseJewel jewel = newItem as BaseJewel;
  
              if (jewel == null)
                  return;
  
              jewel.m_AosAttributes = new AosAttributes(newItem, this.m_AosAttributes);
              jewel.m_AosResistances = new AosElementAttributes(newItem, this.m_AosResistances);
              jewel.m_AosSkillBonuses = new AosSkillBonuses(newItem, this.m_AosSkillBonuses);
              jewel.m_SetAttributes = new AosAttributes(newItem, this.m_SetAttributes);
              jewel.m_SetSkillBonuses = new AosSkillBonuses(newItem, this.m_SetSkillBonuses);
              jewel.m_SAAbsorptionAttributes = new SAAbsorptionAttributes(newItem, this.m_SAAbsorptionAttributes);
              jewel.m_NegativeAttributes = new NegativeAttributes(newItem, this.m_NegativeAttributes);
          }
  
          public virtual int ArtifactRarity
          {
              get
              {
                  return 0;
              }
          }
  
          private Mobile m_Crafter;
          private ArmorQuality m_Quality;
  
          [CommandProperty(AccessLevel.GameMaster)]
          public Mobile Crafter
          {
              get { return m_Crafter; }
              set { m_Crafter = value; InvalidateProperties(); }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public ArmorQuality Quality
          {
              get { return m_Quality; }
              set { m_Quality = value; InvalidateProperties(); }
          }
  
          public BaseJewel(int itemID, Layer layer)
              : base(itemID)
          {
              this.m_AosAttributes = new AosAttributes(this);
              this.m_AosResistances = new AosElementAttributes(this);
              this.m_AosSkillBonuses = new AosSkillBonuses(this);
              this.m_Resource = CraftResource.Iron;
              this.m_GemType = GemType.None;
  
              this.Layer = layer;
  
              this.m_HitPoints = this.m_MaxHitPoints = Utility.RandomMinMax(this.InitMinHits, this.InitMaxHits);
  
              this.m_SetAttributes = new AosAttributes(this);
              this.m_SetSkillBonuses = new AosSkillBonuses(this);
              this.m_SAAbsorptionAttributes = new SAAbsorptionAttributes(this);
              m_NegativeAttributes = new NegativeAttributes(this);
          }
  
          public override bool CanEquip(Mobile from)
          {
              if (MainConfig.MainItemIDEnabled || T2AConfig.T2AItemIDEnabled)
              {
                  if (from.AccessLevel < AccessLevel.GameMaster)
                  {
  
                      if (m_Identified == false)
                      {
                          from.SendMessage("You are hesitant to put on something that is unknown to you");
                          return false;
                      }
  
                  }
              }
              if (this.BlessedBy != null && this.BlessedBy != from)
              {
                  from.SendLocalizedMessage(1075277); // That item is blessed by another player.
                  return false;
              }
  
              if (from.AccessLevel < AccessLevel.GameMaster)
              {
                  if (from.Race == Race.Gargoyle && !this.CanBeWornByGargoyles)
                  {
                      from.SendLocalizedMessage(1111708); // Gargoyles can't wear this.
                      return false;
                  }
                  else if (this.RequiredRace != null && from.Race != this.RequiredRace)
                  {
                      if (this.RequiredRace == Race.Elf)
                          from.SendLocalizedMessage(1072203); // Only Elves may use this.
                      else if (this.RequiredRace == Race.Gargoyle)
                          from.SendLocalizedMessage(1111707); // Only gargoyles can wear this.
                      else
                          from.SendMessage("Only {0} may use this.", this.RequiredRace.PluralName);
  
                      return false;
                  }
              }
  
              return base.CanEquip(from);
          }
  
          public virtual int OnHit(BaseWeapon weap, int damage)
          {
              if (m_TimesImbued == 0 && m_MaxHitPoints == 0)
                  return damage;
  
              int wear = Utility.Random(2);
  
              if (NegativeAttributes.Antique > 0)
                   wear *= 2;
  
              if (wear > 0 && m_MaxHitPoints > 0)
              {
                  if (m_HitPoints >= wear)
                  {
                      HitPoints -= wear;
                      wear = 0;
                  }
                  else
                  {
                      wear -= HitPoints;
                      HitPoints = 0;
                  }
  
                  if (wear > 0)
                  {
                      if (m_MaxHitPoints > wear)
                      {
                          MaxHitPoints -= wear;
  
                          if (Parent is Mobile)
                              ((Mobile)Parent).LocalOverheadMessage(Server.Network.MessageType.Regular, 0x3B2, 1061121); // Your equipment is severely damaged.
                              ((Mobile)Parent).PlaySound(0x38E);
                      }
                      else
                      {
                          Delete();
                      }
                  }
              }
  
              return damage;
          }
  
          public virtual void UnscaleDurability()
          {
          }
  
          public virtual void ScaleDurability()
          {
          }
  
          //public virtual bool CanFortify { get { return false; } }
          //public virtual bool CanFortify { get { return m_TimesImbued == 0 && NegativeAttributes.Antique < 3; } }
          public virtual bool CanFortify { get { return IsImbued == false && NegativeAttributes.Antique < 3; } }
          public virtual bool CanRepair { get { return m_NegativeAttributes.NoRepair == 0; } }
  
          public override void OnAdded(IEntity parent)
          {
              if (Core.AOS && parent is Mobile)
              {
                  Mobile from = (Mobile)parent;
  
                  this.m_AosSkillBonuses.AddTo(from);
  
                  int strBonus = this.m_AosAttributes.BonusStr;
                  int dexBonus = this.m_AosAttributes.BonusDex;
                  int intBonus = this.m_AosAttributes.BonusInt;
  
                  if (strBonus != 0 || dexBonus != 0 || intBonus != 0)
                  {
                      string modName = this.Serial.ToString();
  
                      if (strBonus != 0)
                          from.AddStatMod(new StatMod(StatType.Str, modName + "Str", strBonus, TimeSpan.Zero));
  
                      if (dexBonus != 0)
                          from.AddStatMod(new StatMod(StatType.Dex, modName + "Dex", dexBonus, TimeSpan.Zero));
  
                      if (intBonus != 0)
                          from.AddStatMod(new StatMod(StatType.Int, modName + "Int", intBonus, TimeSpan.Zero));
                  }
  
                  from.CheckStatTimers();
  
                  if (this.IsSetItem)
                  {
                      this.m_SetEquipped = SetHelper.FullSetEquipped(from, this.SetID, this.Pieces);
  
                      if (this.m_SetEquipped)
                      {
                          this.m_LastEquipped = true;
                          SetHelper.AddSetBonus(from, this.SetID);
                      }
                  }
              }
  
              if (parent is Mobile)
              {
                  if (XmlAttach.CheckCanEquip(this, (Mobile)parent))
                      XmlAttach.CheckOnEquip(this, (Mobile)parent);
                  else
                      ((Mobile)parent).AddToBackpack(this);
              }
          }
  
          public override void OnRemoved(IEntity parent)
          {
              if (Core.AOS && parent is Mobile)
              {
                  Mobile from = (Mobile)parent;
  
                  this.m_AosSkillBonuses.Remove();
  
                  string modName = this.Serial.ToString();
  
                  from.RemoveStatMod(modName + "Str");
                  from.RemoveStatMod(modName + "Dex");
                  from.RemoveStatMod(modName + "Int");
  
                  from.CheckStatTimers();
  
                  if (this.IsSetItem && this.m_SetEquipped)
                      SetHelper.RemoveSetBonus(from, this.SetID, this);
              }
  
              XmlAttach.CheckOnRemoved(this, parent);
          }
  
          public BaseJewel(Serial serial)
              : base(serial)
          {
          }
  
          public override void AddNameProperty(ObjectPropertyList list)
          {
              if (m_ReforgedPrefix != ReforgedPrefix.None || m_ReforgedSuffix != ReforgedSuffix.None)
              {
                  if (m_ReforgedPrefix != ReforgedPrefix.None)
                  {
                      int prefix = RunicReforging.GetPrefixName(m_ReforgedPrefix);
  
                      if (m_ReforgedSuffix == ReforgedSuffix.None)
                          list.Add(1151757, String.Format("#{0}\t{1}", prefix, GetNameString())); // ~1_PREFIX~ ~2_ITEM~
                      else
                          list.Add(1151756, String.Format("#{0}\t{1}\t#{2}", prefix, GetNameString(), RunicReforging.GetSuffixName(m_ReforgedSuffix))); // ~1_PREFIX~ ~2_ITEM~ of ~3_SUFFIX~
                  }
                  else if (m_ReforgedSuffix != ReforgedSuffix.None)
                      list.Add(1151758, String.Format("{0}\t#{1}", GetNameString(), RunicReforging.GetSuffixName(m_ReforgedSuffix))); // ~1_ITEM~ of ~2_SUFFIX~
              }
  
              if (this.Name == null)
              {
                  list.Add(this.LabelNumber);
              }
              else
              {
                  list.Add(this.Name);
              }
  
              if (!String.IsNullOrEmpty(m_EngravedText))
              {
                  list.Add(1062613, m_EngravedText);
              }
              else
              {
                  base.AddNameProperty(list);
              }
          }
  
          private string GetNameString()
          {
              string name = this.Name;
  
              if (name == null)
                  name = String.Format("#{0}", LabelNumber);
  
              return name;
          }
  
          public override void GetProperties(ObjectPropertyList list)
          {
              base.GetProperties(list);
  
              if (MainConfig.MainItemIDEnabled || T2AConfig.T2AItemIDEnabled)
              {
                  if (m_Identified)
                  {
                      //if (this.m_TimesImbued > 0)
                      if (IsImbued)
                          list.Add(1080418); // (Imbued)
  
                      if (m_AosAttributes.Brittle > 0 || Brittle)
                          list.Add(1116209); // Brittle
  
                      m_NegativeAttributes.GetProperties(list, this);
  
                      if (m_AosAttributes.NoRepairs > 0)
                          list.Add(1151782);//cannot be repaired
  
                      if (m_Quality == ArmorQuality.Exceptional)
                          list.Add(1063341); // exceptional
  
                      if (m_Crafter != null)
                          list.Add(1050043, m_Crafter.TitleName); // crafted by ~1_NAME~ 
  
                      if (this.IsSetItem)
                      {
                          list.Add(1080240, this.Pieces.ToString()); // Part of a Jewelry Set (~1_val~ pieces)
  
                          if (this.m_SetEquipped)
                          {
                              list.Add(1080241); // Full Jewelry Set Present					
                              SetHelper.GetSetProperties(list, this);
                          }
                      }
  
                      this.m_AosSkillBonuses.GetProperties(list);
  
                      int prop;
  
                      if (this.RequiredRace == Race.Elf)
                          list.Add(1075086); // Elves Only
                      else if (this.RequiredRace == Race.Gargoyle)
                          list.Add(1111709); // Gargoyles Only
  
                      if ((prop = this.ArtifactRarity) > 0)
  
gt:                         list.Add(1061078, prop.ToString()); // artifact rarity ~1_val~