using System;
using Server;
using Server.Network;
using Server.Mobiles;

namespace Server.Items
{
	public enum AxeTrapType
	{
		WestWall,
		NorthWall,
		WestDown,
		NorthDown
	}

	public class AxeTrap : BaseTrap
	{
		[CommandProperty( AccessLevel.GameMaster )]
		public AxeTrapType Type
		{
			get
			{
				if ( ItemID >= 4403 && ItemID <= 4409 )
					return AxeTrapType.WestWall;
				else if ( ItemID >= 4499 && ItemID <= 4505 )
					return AxeTrapType.NorthWall;
				else if ( ItemID >= 4416 && ItemID <= 4420 )
					return AxeTrapType.WestDown;
				else if ( ItemID >= 4427 && ItemID <= 4431 )
					return AxeTrapType.NorthDown;

				return AxeTrapType.WestWall;
			}
			set
			{
				bool extended = this.Extended;

				ItemID = ( extended ? GetExtendedID( value ) : GetBaseID( value ) );
			}
		}

		public bool Extended
		{
			get{ return ( ItemID == GetExtendedID( this.Type ) ); }
			set
			{
				if ( value )
					ItemID = GetExtendedID( this.Type );
				else
					ItemID = GetBaseID( this.Type );
			}
		}

		public static int GetBaseID( AxeTrapType type )
		{
			switch ( type )
			{
				case AxeTrapType.WestWall: return 4403;
				case AxeTrapType.NorthWall: return 4499;
				case AxeTrapType.WestDown: return 4416;
				case AxeTrapType.NorthDown: return 4427;
			}

			return 0;
		}

		public static int GetExtendedID( AxeTrapType type )
		{
			return GetBaseID( type ) + GetExtendedOffset( type );
		}

		public static int GetExtendedOffset( AxeTrapType type )
		{
			switch ( type )
			{
				case AxeTrapType.WestWall: return 6;
				case AxeTrapType.NorthWall: return 6;
				case AxeTrapType.WestDown: return 4;
				case AxeTrapType.NorthDown: return 4;
			}

			return 0;
		}

		[Constructable]
		public AxeTrap() : this( AxeTrapType.WestWall )
		{
		}

		[Constructable]
		public AxeTrap( AxeTrapType type ) : base( GetBaseID( type ) )
		{
		}

		public override bool PassivelyTriggered{ get{ return false; } }
		public override TimeSpan PassiveTriggerDelay{ get{ return TimeSpan.Zero; } }
		public override int PassiveTriggerRange{ get{ return 0; } }
		public override TimeSpan ResetDelay{ get{ return TimeSpan.FromSeconds( 6.0 ); } }

		public override void OnTrigger( Mobile from )
		{
			base.OnTrigger( from );

			Effects.SendLocationEffect( Location, Map, GetBaseID( this.Type ) + 1, 18, 3, GetEffectHue(), 0 );
			Effects.PlaySound( Location, Map, 0x232 );

			foreach ( Mobile mob in GetMobilesInRange( 0 ) )
			{
				if ( mob.Alive && !mob.IsDeadBondedPet )
					Spells.SpellHelper.Damage( TimeSpan.FromTicks( 1 ), mob, mob, Utility.RandomMinMax( 1, 6 ) * 6 );
			}

			Timer.DelayCall( TimeSpan.FromSeconds( 1.0 ), new TimerCallback( OnAxeExtended ) );

			from.LocalOverheadMessage( MessageType.Regular, 0x22, 501622 ); // You stepped into a scything blade!
		}

		public virtual void OnAxeExtended()
		{
			Extended = true;
			Timer.DelayCall( TimeSpan.FromSeconds( 4.0 ), new TimerCallback( OnAxeRetracted ) );
		}

		public virtual void OnAxeRetracted()
		{
			Extended = false;
			Effects.SendLocationEffect( Location, Map, GetExtendedID( this.Type ) - 1, 6, 3, GetEffectHue(), 0 );
		}

		public AxeTrap( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 0 ); // version
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			Extended = false;
		}
	}
}