context: using System;
  using System.Collections.Generic;
  using Server.ContextMenus;
  using Server.Engines.Craft;
  using Server.Factions;
  using Server.Network;
  using Server.Mobiles;
  
  namespace Server.Items
  {
      public enum ClothingQuality
      {
          Low,
          Regular,
          Exceptional
      }
  
      public interface IArcaneEquip
      {
          bool IsArcane { get; }
          int CurArcaneCharges { get; set; }
          int MaxArcaneCharges { get; set; }
      }
  
      public abstract class BaseClothing : Item, IDyable, IScissorable, IFactionItem, ICraftable, IWearableDurability
      {
          #region Factions
          private FactionItem m_FactionState;
  
          public FactionItem FactionItemState
          {
              get
              {
                  return this.m_FactionState;
              }
              set
              {
                  this.m_FactionState = value;
  
                  if (this.m_FactionState == null)
                      this.Hue = 0;
  
                  this.LootType = (this.m_FactionState == null ? LootType.Regular : LootType.Blessed);
              }
          }
          #endregion
  
          private int m_MaxHitPoints;
          private int m_HitPoints;
          private Mobile m_Crafter;
          private ClothingQuality m_Quality;
          private bool m_PlayerConstructed;
          protected CraftResource m_Resource;
          private int m_StrReq = -1;
  
          [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.GameMaster)]
          public Mobile Crafter
          {
              get
              {
                  return this.m_Crafter;
              }
              set
              {
                  this.m_Crafter = value;
                  this.InvalidateProperties();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public int StrRequirement
          {
              get
              {
                  return (int)((double)(m_StrReq == -1 ? (Core.AOS ? AosStrReq : OldStrReq) : m_StrReq));
              }
              set
              {
                  this.m_StrReq = value;
                  this.InvalidateProperties();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public ClothingQuality Quality
          {
              get
              {
                  return this.m_Quality;
              }
              set
              {
                  this.m_Quality = value;
                  this.InvalidateProperties();
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool PlayerConstructed
          {
              get
              {
                  return this.m_PlayerConstructed;
              }
              set
              {
                  this.m_PlayerConstructed = value;
              }
          }
  
          #region Personal Bless Deed
          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 BaseClothing m_Item;
  
              public UnBlessEntry(Mobile from, BaseClothing item)
                  : base(6208, -1)
              {
                  this.m_From = from;
                  this.m_Item = item; // BaseArmor, BaseWeapon or BaseClothing
              }
  
              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.
                  }
              }
          }
          #endregion
  
          public virtual CraftResource DefaultResource
          {
              get
              {
                  return CraftResource.None;
              }
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public CraftResource Resource
          {
              get
              {
                  return this.m_Resource;
              }
              set
              {
                  this.m_Resource = value;
                  this.Hue = CraftResources.GetHue(this.m_Resource);
                  this.InvalidateProperties();
              }
          }
  
          public virtual int BasePhysicalResistance
          {
              get
              {
                  return 0;
              }
          }
          public virtual int BaseFireResistance
          {
              get
              {
                  return 0;
              }
          }
          public virtual int BaseColdResistance
          {
              get
              {
                  return 0;
              }
          }
          public virtual int BasePoisonResistance
          {
              get
              {
                  return 0;
              }
          }
          public virtual int BaseEnergyResistance
          {
              get
              {
                  return 0;
              }
          }
          
          public virtual int ArtifactRarity
          {
              get
              {
                  return 0;
              }
          }
  
          public virtual int BaseStrBonus
          {
              get
              {
                  return 0;
              }
          }
          public virtual int BaseDexBonus
          {
              get
              {
                  return 0;
              }
          }
          public virtual int BaseIntBonus
          {
              get
              {
                  return 0;
              }
          }
  
          public override bool AllowSecureTrade(Mobile from, Mobile to, Mobile newOwner, bool accepted)
          {
              if (!Ethics.Ethic.CheckTrade(from, to, newOwner, this))
                  return false;
  
              return base.AllowSecureTrade(from, to, newOwner, accepted);
          }
  
          public virtual Race RequiredRace
          {
              get
              {
                  return null;
              }
          }
  
          public override bool CanEquip(Mobile from)
          {
              if (!Ethics.Ethic.CheckEquip(from, this))
                  return false;
  
              if (from.IsPlayer())
              {
                  if (!this.AllowMaleWearer && !from.Female)
                  {
                      if (this.AllowFemaleWearer)
                          from.SendLocalizedMessage(1010388); // Only females can wear this.
                      else
                          from.SendMessage("You may not wear this.");
  
                      return false;
                  }
                  else if (!this.AllowFemaleWearer && from.Female)
                  {
                      if (this.AllowMaleWearer)
                          from.SendLocalizedMessage(1063343); // Only males can wear this.
                      else
                          from.SendMessage("You may not wear this.");
  
                      return false;
                  }
                  #region Personal Bless Deed
                  else if (this.BlessedBy != null && this.BlessedBy != from)
                  {
                      from.SendLocalizedMessage(1075277); // That item is blessed by another player.
  
                      return false;
                  }
                  #endregion
              }
  
              return base.CanEquip(from);
          }
  
          public virtual int AosStrReq
          {
              get
              {
                  return 10;
              }
          }
          public virtual int OldStrReq
          {
              get
              {
                  return 0;
              }
          }
  
          public virtual int InitMinHits
          {
              get
              {
                  return 0;
              }
          }
          public virtual int InitMaxHits
          {
              get
              {
                  return 0;
              }
          }
  
          public virtual bool AllowMaleWearer
          {
              get
              {
                  return true;
              }
          }
          public virtual bool AllowFemaleWearer
          {
              get
              {
                  return true;
              }
          }
          public virtual bool CanBeBlessed
          {
              get
              {
                  return true;
              }
          }
  
          public static void ValidateMobile(Mobile m)
          {
              for (int i = m.Items.Count - 1; i >= 0; --i)
              {
                  if (i >= m.Items.Count)
                      continue;
  
                  Item item = m.Items[i];
  
                  if (item is BaseClothing)
                  {
                      BaseClothing clothing = (BaseClothing)item;
  
                      if (!clothing.AllowMaleWearer && !m.Female && m.AccessLevel < AccessLevel.GameMaster)
                      {
                          if (clothing.AllowFemaleWearer)
                              m.SendLocalizedMessage(1010388); // Only females can wear this.
                          else
                              m.SendMessage("You may not wear this.");
  
                          m.AddToBackpack(clothing);
                      }
                      else if (!clothing.AllowFemaleWearer && m.Female && m.AccessLevel < AccessLevel.GameMaster)
                      {
                          if (clothing.AllowMaleWearer)
                              m.SendLocalizedMessage(1063343); // Only males can wear this.
                          else
                              m.SendMessage("You may not wear this.");
  
                          m.AddToBackpack(clothing);
                      }
                  }
              }
          }
  
          public override void OnAdded(object parent)
          {
              Mobile mob = parent as Mobile;
  
              if (mob != null)
              {
  
                  mob.CheckStatTimers();
              }
  
              base.OnAdded(parent);
          }
  
          public override void OnRemoved(object parent)
          {
              Mobile mob = parent as Mobile;
  
              if (mob != null)
              {
                  string modName = this.Serial.ToString();
  
                  mob.RemoveStatMod(modName + "Str");
                  mob.RemoveStatMod(modName + "Dex");
                  mob.RemoveStatMod(modName + "Int");
  
                  mob.CheckStatTimers();
              }
  
              base.OnRemoved(parent);
          }
  
          public virtual int OnHit(BaseWeapon weapon, int damageTaken)
          {
              if (m_MaxHitPoints == 0)
                  return damageTaken;
  
              int Absorbed = Utility.RandomMinMax(1, 4);
  
              damageTaken -= Absorbed;
  
              if (damageTaken < 0) 
                  damageTaken = 0;
  
              if (25 > Utility.Random(100)) // 25% chance to lower durability
              {
                  int wear;
  
                  if (weapon.Type == WeaponType.Bashing)
                      wear = Absorbed / 2;
                  else
                      wear = Utility.Random(2);
  
                  if (wear > 0 && this.m_MaxHitPoints > 0)
                  {
                      if (this.m_HitPoints >= wear)
                      {
                          this.HitPoints -= wear;
                          wear = 0;
                      }
                      else
                      {
                          wear -= this.HitPoints;
                          this.HitPoints = 0;
                      }
  
                      if (wear > 0)
                      {
                          if (this.m_MaxHitPoints > wear)
                          {
                              this.MaxHitPoints -= wear;
  
                              if (this.Parent is Mobile)
                                  ((Mobile)this.Parent).LocalOverheadMessage(MessageType.Regular, 0x3B2, 1061121); // Your equipment is severely damaged.
                          }
                          else
                          {
                              this.Delete();
                          }
                      }
                  }
              }
              return damageTaken;
          }
  
          public BaseClothing(int itemID, Layer layer)
              : this(itemID, layer, 0)
          {
          }
  
          public BaseClothing(int itemID, Layer layer, int hue)
              : base(itemID)
          {
              this.Layer = layer;
              this.Hue = hue;
  
              this.m_Resource = this.DefaultResource;
              this.m_Quality = ClothingQuality.Regular;
  
              this.m_HitPoints = this.m_MaxHitPoints = Utility.RandomMinMax(this.InitMinHits, this.InitMaxHits);
          }
  
          public BaseClothing(Serial serial)
              : base(serial)
          {
          }
  
          public override bool AllowEquipedCast(Mobile from)
          {
              if (base.AllowEquipedCast(from))
                  return true;
  
              return (false);
          }
  
          public override bool CheckPropertyConfliction(Mobile m)
          {
              if (base.CheckPropertyConfliction(m))
                  return true;
  
              if (this.Layer == Layer.Pants)
  
gt:                 return (m.FindItemOnLayer(Layer.InnerLegs) != null);