using System;
using System.Text;
using System.Collections;
using Server.Network;
using Server.Targeting;
using Server.Spells;

namespace Server.Items
{
	public enum WandEffect
	{
		Clumsiness,
		Healing,
		Feeblemindedness,
		Weakness,
		MagicArrow,
		Harming,
		Fireball,
		GreaterHealing,
		Lightning,
		ManaDraining,
		Recall,
		Mark,
		RemoveTrap,
		Unlock,
		Resurrection
	}

	public abstract class BaseWand : BaseBashing
	{
		public override int UOStrengthReq { get { return 0; } }
		public override int UOMinDamage { get { return 2; } }
		public override int UOMaxDamage { get { return 6; } }
		public override int UOSpeed { get { return 35; } }

		public override int InitMinHits { get { return 31; } }
		public override int InitMaxHits { get { return 110; } }

		private WandEffect m_WandEffect;

		public virtual TimeSpan GetUseDelay{ get{ return TimeSpan.FromSeconds( 4.0 ); } }

		[CommandProperty( AccessLevel.GameMaster )]
		public WandEffect Effect
		{
			get{ return m_WandEffect; }
			set{ m_WandEffect = value; InvalidateProperties(); }
		}

		public BaseWand( WandEffect effect, int minCharges, int maxCharges ) : base( Utility.RandomList( 0xDF2, 0xDF3, 0xDF4, 0xDF5 ) )
		{
			Weight = 1.0;
			Effect = effect;
			m_Uses = Utility.RandomMinMax( minCharges, maxCharges );
			m_UsesMax = m_Uses;
		}

		public void ConsumeCharge( Mobile from )
		{
			ConsumeUses();

			if ( m_Uses == 0 )
			{
				from.SendLocalizedMessage( 1019073 ); // This item is out of charges.
				Name = "useless wand";
			}

			ApplyDelayTo( from );
		}

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

		public virtual void ApplyDelayTo( Mobile from )
		{
			from.BeginAction( typeof( BaseWand ) );
			Timer.DelayCall( GetUseDelay, new TimerStateCallback( ReleaseWandLock_Callback ), from );
		}

		public virtual void ReleaseWandLock_Callback( object state )
		{
			((Mobile)state).EndAction( typeof( BaseWand ) );
		}

		public override void OnDoubleClick( Mobile from )
		{
			if ( !from.CanBeginAction( typeof( BaseWand ) ) )
				return;

			if ( Parent == from )
			{
				if ( m_Uses > 0 )
					OnWandUse( from );
				else
					from.SendLocalizedMessage( 1019073 ); // This item is out of charges.
			}
			else
			{
				from.SendLocalizedMessage( 502641 ); // You must equip this item to use it.
			}
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 ); // version
			writer.Write( (int) m_WandEffect );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
			m_WandEffect = (WandEffect)reader.ReadInt();
		}

		public override void GetProperties( ObjectPropertyList list )
		{
			base.GetProperties( list );

			if ( m_Uses > 0 && Server.Misc.Settings.DisplayInfo() )
			{
				switch ( m_WandEffect )
				{
					case WandEffect.Clumsiness:			list.Add( 1017326, m_Uses.ToString() ); break; // clumsiness charges: ~1_val~
					case WandEffect.Healing:			list.Add( 1017329, m_Uses.ToString() ); break; // healing charges: ~1_val~
					case WandEffect.Feeblemindedness:	list.Add( 1017327, m_Uses.ToString() ); break; // feeblemind charges: ~1_val~
					case WandEffect.Weakness:			list.Add( 1017328, m_Uses.ToString() ); break; // weakness charges: ~1_val~
					case WandEffect.MagicArrow:			list.Add( 1060492, m_Uses.ToString() ); break; // magic arrow charges: ~1_val~
					case WandEffect.Harming:			list.Add( 1017334, m_Uses.ToString() ); break; // harm charges: ~1_val~
					case WandEffect.Fireball:			list.Add( 1060487, m_Uses.ToString() ); break; // fireball charges: ~1_val~
					case WandEffect.GreaterHealing:		list.Add( 1017330, m_Uses.ToString() ); break; // greater healing charges: ~1_val~
					case WandEffect.Lightning:			list.Add( 1060491, m_Uses.ToString() ); break; // lightning charges: ~1_val~
					case WandEffect.ManaDraining:		list.Add( 1017339, m_Uses.ToString() ); break; // mana drain charges: ~1_val~
					case WandEffect.Recall:				list.Add( 1017343, m_Uses.ToString() ); break; // recall charges: ~1_val~
					case WandEffect.Mark:				list.Add( 1017344, m_Uses.ToString() ); break; // mark charges: ~1_val~
					case WandEffect.RemoveTrap:			list.Add( 1017345, m_Uses.ToString() ); break; // remove trap charges: ~1_val~
					case WandEffect.Unlock:				list.Add( 1017346, m_Uses.ToString() ); break; // unlock charges: ~1_val~
					case WandEffect.Resurrection:		list.Add( 1017349, m_Uses.ToString() ); break; // resurrection charges: ~1_val~
				}
			}
			else if ( m_Uses > 0 )
			{
				double usage = (double)m_Uses / (double)m_UsesMax;

				if ( usage > 0.95 )
					list.Add( 1063269 ); // Fully Charged
				else if ( usage > 0.75 )
					list.Add( 1063270 ); // Mostly Charged
				else if ( usage > 0.50 )
					list.Add( 1063271 ); // Partially Charged
				else if ( usage > 0.25 )
					list.Add( 1063272 ); // Somewhat Charged
				else if ( usage > 0.05 )
					list.Add( 1063273 ); // Barely Charged
				else
					list.Add( 1063274 ); // Almost Drained
			}
		}

		public override void OnSingleClick( Mobile from )
		{
			ArrayList attrs = new ArrayList();

			if ( DisplayLootType )
			{
				if ( LootType == LootType.Blessed )
					attrs.Add( new EquipInfoAttribute( 1038021 ) ); // blessed
			}

			int num = 0;

			switch ( m_WandEffect )
			{
				case WandEffect.Clumsiness:			num = 3002011; break;
				case WandEffect.Healing:			num = 3002014; break;
				case WandEffect.Feeblemindedness:	num = 3002013; break;
				case WandEffect.Weakness:			num = 3002018; break;
				case WandEffect.MagicArrow:			num = 3002015; break;
				case WandEffect.Harming:			num = 3002022; break;
				case WandEffect.Fireball:			num = 3002028; break;
				case WandEffect.GreaterHealing:		num = 3002039; break;
				case WandEffect.Lightning:			num = 3002040; break;
				case WandEffect.ManaDraining:		num = 3002041; break;
				case WandEffect.Recall:				num = 3002042; break;
				case WandEffect.Mark:				num = 3002055; break;
				case WandEffect.RemoveTrap:			num = 3002024; break;
				case WandEffect.Unlock:				num = 3002033; break;
				case WandEffect.Resurrection:		num = 3002069; break;
			}

			if ( num > 0 )
				attrs.Add( new EquipInfoAttribute( num, m_Uses ) );

			int number;

			if ( Name == null )
			{
				number = 1017085;
			}
			else
			{
				this.LabelTo( from, Name );
				number = 1041000;
			}

			if ( attrs.Count == 0 && Crafter == null && Name != null )
				return;

			EquipmentInfo eqInfo = new EquipmentInfo( number, Crafter, false, (EquipInfoAttribute[])attrs.ToArray( typeof( EquipInfoAttribute ) ) );

			from.Send( new DisplayEquipmentInfo( this, eqInfo ) );
		}

		public void Cast( Spell spell )
		{
			bool m = Movable;

			Movable = false;
			spell.Cast();
			Movable = m;
		}

		public virtual void OnWandUse( Mobile from )
		{
			from.Target = new WandTarget( this );
		}

		public virtual void DoWandTarget( Mobile from, object o )
		{
			if ( Deleted || m_Uses <= 0 || Parent != from || o is StaticTarget || o is LandTarget )
				return;

			if ( OnWandTarget( from, o ) )
				ConsumeCharge( from );
		}

		public virtual bool OnWandTarget( Mobile from, object o )
		{
			return true;
		}
	}
}