using System;
using Server.Targeting;
using Server.Network;
using Server.Items;
using Server.Regions;

namespace Server.Spells.Second
{
	public class MagicTrapSpell : MagerySpell
	{
		private static SpellInfo m_Info = new SpellInfo(
				"Magic Trap", "In Jux",
				212,
				9001,
				Reagent.Garlic,
				Reagent.SpidersSilk,
				Reagent.SulfurousAsh
			);

		public override SpellCircle Circle { get { return SpellCircle.Second; } }

		public MagicTrapSpell( Mobile caster, Item scroll ) : base( caster, scroll, m_Info )
		{
		}

		public override void OnCast()
		{
			Caster.Target = new InternalTarget( this );
		}

		public void Target( TrapableContainer item )
		{
			if ( !Caster.CanSee( item ) )
			{
				Caster.SendLocalizedMessage( 500237 ); // Target can not be seen.
			}
			else if ( item.TrapType != TrapType.None && item.TrapType != TrapType.MagicTrap )
			{
				base.DoFizzle();
			}
			else if ( CheckSequence() )
			{
				SpellHelper.Turn( Caster, item );

				item.TrapType = TrapType.MagicTrap;
				item.TrapPower = (int)( ( Caster.Skills[SkillName.Concentration].Value + Caster.Skills[SkillName.Magery].Value ) / 3 );
				item.TrapLevel = 0;

				Point3D loc = item.GetWorldLocation();

				Effects.SendLocationParticles( EffectItem.Create( new Point3D( loc.X + 1, loc.Y, loc.Z ), item.Map, EffectItem.DefaultDuration ), 0x376A, 9, 10, 9502 );
				Effects.SendLocationParticles( EffectItem.Create( new Point3D( loc.X, loc.Y - 1, loc.Z ), item.Map, EffectItem.DefaultDuration ), 0x376A, 9, 10, 9502 );
				Effects.SendLocationParticles( EffectItem.Create( new Point3D( loc.X - 1, loc.Y, loc.Z ), item.Map, EffectItem.DefaultDuration ), 0x376A, 9, 10, 9502 );
				Effects.SendLocationParticles( EffectItem.Create( new Point3D( loc.X, loc.Y + 1, loc.Z ), item.Map, EffectItem.DefaultDuration ), 0x376A, 9, 10, 9502 );
				Effects.SendLocationParticles( EffectItem.Create( new Point3D( loc.X, loc.Y,     loc.Z ), item.Map, EffectItem.DefaultDuration ), 0, 0, 0, 5014 );

				Effects.PlaySound( loc, item.Map, 0x1EF );
			}

			FinishSequence();
		}

		public void Doorway( BaseDoor item )
		{
			if ( !(Caster.Region is DungeonRegion) )
			{
				Caster.SendMessage( "This spell only works on dungeon doors." );
			}
			else if ( !Caster.CanSee( item ) )
			{
				Caster.SendLocalizedMessage( 500237 ); // Target can not be seen.
			}
			else if ( item.TrapType != TrapType.None && item.TrapType != TrapType.MagicTrap )
			{
				base.DoFizzle();
			}
			else if ( CheckSequence() )
			{
				SpellHelper.Turn( Caster, item );

				item.TrapType = TrapType.MagicTrap;
				item.TrapPower = (int)( ( Caster.Skills[SkillName.Concentration].Value + Caster.Skills[SkillName.Magery].Value ) / 3 );
				item.TrapLevel = 0;

				Point3D loc = item.GetWorldLocation();

				Effects.SendLocationParticles( EffectItem.Create( new Point3D( loc.X + 1, loc.Y, loc.Z ), item.Map, EffectItem.DefaultDuration ), 0x376A, 9, 10, 9502 );
				Effects.SendLocationParticles( EffectItem.Create( new Point3D( loc.X, loc.Y - 1, loc.Z ), item.Map, EffectItem.DefaultDuration ), 0x376A, 9, 10, 9502 );
				Effects.SendLocationParticles( EffectItem.Create( new Point3D( loc.X - 1, loc.Y, loc.Z ), item.Map, EffectItem.DefaultDuration ), 0x376A, 9, 10, 9502 );
				Effects.SendLocationParticles( EffectItem.Create( new Point3D( loc.X, loc.Y + 1, loc.Z ), item.Map, EffectItem.DefaultDuration ), 0x376A, 9, 10, 9502 );
				Effects.SendLocationParticles( EffectItem.Create( new Point3D( loc.X, loc.Y,     loc.Z ), item.Map, EffectItem.DefaultDuration ), 0, 0, 0, 5014 );

				Effects.PlaySound( loc, item.Map, 0x1EF );

				double time = Caster.Skills[SkillName.Magery].Value + Caster.Skills[SkillName.Concentration].Value + 20.0;
				DateTime currentTime = DateTime.Now;
				DateTime laterTime = currentTime.AddSeconds(time);

				item.Rigged = laterTime;
			}

			FinishSequence();
		}

		public void Targets( BaseDoor targ )
		{
			if ( !(Caster.Region is DungeonRegion) )
			{
				Caster.SendMessage( "This spell only works on dungeon doors." );
			}
			else if ( targ.Sealed > DateTime.Now )
			{
				Caster.SendMessage( "That door is already magically locked." );
			}
			else if ( CheckSequence() )
			{
				SpellHelper.Turn( Caster, targ );

				Point3D loc = targ.GetWorldLocation();

				Effects.SendLocationParticles( EffectItem.Create( loc, targ.Map, EffectItem.DefaultDuration ), 0x376A, 9, 32, 5020 );

				Effects.PlaySound( loc, targ.Map, 0x1FA );

				// The door is now locked!
				Caster.LocalOverheadMessage( MessageType.Regular, 0x3B2, 502401 );

				double time = Caster.Skills[SkillName.Magery].Value + Caster.Skills[SkillName.Concentration].Value + 20.0;
				DateTime currentTime = DateTime.Now;
				DateTime laterTime = currentTime.AddSeconds(time);

				targ.Sealed = laterTime;
			}

			FinishSequence();
		}

		private class InternalTarget : Target
		{
			private MagicTrapSpell m_Owner;

			public InternalTarget( MagicTrapSpell owner ) : base( 12, false, TargetFlags.None )
			{
				m_Owner = owner;
			}

			protected override void OnTarget( Mobile from, object o )
			{
				if ( o is LootChest )
					((LootChest)o).Setup();

				if ( o is TrapableContainer )
				{
					m_Owner.Target( (TrapableContainer)o );
				}
				else if ( o is BaseDoor )
				{
					m_Owner.Doorway( (BaseDoor)o );
				}
				else
				{
					from.SendMessage( "You can't trap that" );
				}
			}

			protected override void OnTargetFinish( Mobile from )
			{
				m_Owner.FinishSequence();
			}
		}
	}
}