context: using System;
  using Server;
  using Server.Items;
  using Server.Mobiles;
  using Server.Network;
  using Server.Targeting;
  
  namespace Server.Factions
  {
  	public class Sigil : BaseSystemController, IComparable
  	{
  		public const int OwnershipHue = 0xB;
  
  		// ?? time corrupting faction has to return the sigil before corruption time resets ?
  		public static readonly TimeSpan CorruptionGrace = TimeSpan.FromMinutes( 30.0 );
  
  		// Sigil must be held at a stronghold for this amount of time in order to become corrupted
  		public static readonly TimeSpan CorruptionPeriod = TimeSpan.FromHours( 10.0 );
  
  		// After a sigil has been corrupted it must be returned to the town within this period of time
  		public static readonly TimeSpan ReturnPeriod = TimeSpan.FromHours( 1.0 );
  
  		// Once it's been returned the corrupting faction owns the town for this period of time
  		public static readonly TimeSpan PurificationPeriod = TimeSpan.FromDays( 3.0 );
  
  		private Mobile m_OriginalThief;
  		private Mobile m_PreviousThief;
  		private BaseMonolith m_LastMonolith;
  
  		private Town m_Town;
  		private Faction m_Corrupted;
  		private Faction m_Corrupting;
  
  		private DateTime m_LastStolen;
  		private DateTime m_GraceStart;
  		private DateTime m_CorruptionStart;
  		private DateTime m_PurificationStart;
  
  		[CommandProperty( AccessLevel.Counselor, AccessLevel.Administrator )]
  		public DateTime LastStolen { get { return m_LastStolen; } set { m_LastStolen = value; } }
  
  		[CommandProperty( AccessLevel.Counselor, AccessLevel.Administrator )]
  		public DateTime GraceStart { get { return m_GraceStart; } set { m_GraceStart = value; } }
  
  		[CommandProperty( AccessLevel.Counselor, AccessLevel.Administrator )]
  		public DateTime CorruptionStart { get { return m_CorruptionStart; } set { m_CorruptionStart = value; } }
  
  		[CommandProperty( AccessLevel.Counselor, AccessLevel.Administrator )]
  		public DateTime PurificationStart { get { return m_PurificationStart; } set { m_PurificationStart = value; } }
  
  		[CommandProperty( AccessLevel.Counselor, AccessLevel.Administrator )]
  		public Town Town
  		{
  			get { return m_Town; }
  			set
  			{
  				m_Town = value;
  				Update();
  			}
  		}
  
  		[CommandProperty( AccessLevel.Counselor, AccessLevel.Administrator )]
  		public Faction Corrupted
  		{
  			get { return m_Corrupted; }
  			set
  			{
  				m_Corrupted = value;
  				Update();
  			}
  		}
  
  		[CommandProperty( AccessLevel.Counselor, AccessLevel.Administrator )]
  		public Faction Corrupting
  		{
  			get { return m_Corrupting; }
  			set
  			{
  				m_Corrupting = value;
  				Update();
  			}
  		}
  
  		[CommandProperty( AccessLevel.Counselor, AccessLevel.Administrator )]
  		public BaseMonolith LastMonolith { get { return m_LastMonolith; } set { m_LastMonolith = value; } }
  
  		[CommandProperty( AccessLevel.Counselor )]
  		public bool IsBeingCorrupted { get { return ( m_LastMonolith is StrongholdMonolith && m_LastMonolith.Faction == m_Corrupting && m_Corrupting != null ); } }
  
  		[CommandProperty( AccessLevel.Counselor )]
  		public bool IsCorrupted { get { return ( m_Corrupted != null ); } }
  
  		[CommandProperty( AccessLevel.Counselor )]
  		public bool IsPurifying { get { return ( m_PurificationStart != DateTime.MinValue ); } }
  
  		[CommandProperty( AccessLevel.Counselor )]
  		public bool IsCorrupting { get { return ( m_Corrupting != null && m_Corrupting != m_Corrupted ); } }
  
  		[CommandProperty( AccessLevel.GameMaster )]
  		public Mobile OriginalThief { get { return ( m_OriginalThief ); } }
  
  		[CommandProperty( AccessLevel.GameMaster )]
  		public Mobile PreviousThief { get { return ( m_PreviousThief ); } }
  
  		public void Update()
  		{
  			ItemID = ( m_Town == null ? 0x1869 : m_Town.Definition.SigilID );
  
  			if ( m_Town == null )
  				AssignName( null );
  			else if ( IsCorrupted || IsPurifying )
  				AssignName( m_Town.Definition.CorruptedSigilName );
  			else
  				AssignName( m_Town.Definition.SigilName );
  
  			InvalidateProperties();
  		}
  
  		private void UpdateThief( Mobile thief )
  		{
  			m_PreviousThief = m_OriginalThief;
  			m_OriginalThief = thief;
  		}
  
  		public void RestoreThief( Faction killerfaction )
  		{
  			Faction previousfaction = Faction.Find( m_PreviousThief );
  
  			if ( previousfaction != killerfaction )
  				return;
  
  			Mobile swapvar = m_OriginalThief;
  
  			m_OriginalThief = m_PreviousThief;
  			m_PreviousThief = swapvar;
  		}
  
  		public override bool CanInsure { get { return false; } }
  
  		public override void GetProperties( ObjectPropertyList list )
  		{
  			base.GetProperties( list );
  
  			if ( IsCorrupted )
  				TextDefinition.AddTo( list, m_Corrupted.Definition.SigilControl );
  			else
  				list.Add( 1042256 ); // This sigil is not corrupted.
  
  			if ( IsCorrupting )
  				list.Add( 1042257 ); // This sigil is in the process of being corrupted.
  			else if ( IsPurifying )
  				list.Add( 1042258 ); // This sigil has recently been corrupted, and is undergoing purification.
  			else
  				list.Add( 1042259 ); // This sigil is not in the process of being corrupted.
  		}
  
  		public override bool CheckLift( Mobile from, Item item, ref LRReason reject )
  		{
  			from.SendLocalizedMessage( 1005225 ); // You must use the stealing skill to pick up the sigil
  			return false;
  		}
  
  		private Mobile FindOwner( object parent )
  		{
  			if ( parent is Item )
  				return ( (Item) parent ).RootParent as Mobile;
  
  			if ( parent is Mobile )
  				return (Mobile) parent;
  
  			return null;
  		}
  
  		public void OnStolen( Mobile mob )
  		{
  			PlayerState ps = PlayerState.Find( mob );
  			Faction faction = ( ps == null ? null : ps.Faction );
  
  			if ( faction != null )
  			{
  				if ( Corrupted != faction && Corrupting != faction )
  				{
  					UpdateThief( mob );
  					Server.Scripts.Commands.CommandHandlers.BroadcastFactionMessage( 38, "[Factions] " + this.Town + "'s sigil has been stolen!" );
  				}
  			}
  		}
  
  		public override void OnAdded( object parent )
  		{
  			base.OnAdded( parent );
  
  			Mobile mob = FindOwner( parent );
  
  			if ( mob != null )
  				mob.SolidHueOverride = OwnershipHue;
  		}
  
  		public override void OnRemoved( object parent )
  		{
  			base.OnRemoved( parent );
  
  
gt: 			Mobile mob = FindOwner( parent );