/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Aug-08-2011 09:29:13pm
Filepath: scripts\Items\artifacts\Artifacts of Assimilation\lessers\wallgenerator.cs
Lines of code: 155

Description: 

***********************************************************/


using System;
using Server.Items;
using Server.Misc;

namespace Server.Items
{
	public class wallgenerator : Item
	{
	
	 private int wallsize=3;

	
		[CommandProperty( AccessLevel.GameMaster )] 
		public int Wallsize
		{
			get { return wallsize; }
			set 
			{ 
				wallsize = value;
				
			}
		}
	
			[Constructable]
		public wallgenerator() : base(10922)
		{
			Hue = 2101;
		}

		public override string DefaultName
		{
			get { return "Duel Wall Generator (Lock down and double click)"; }
		}
		
		
		public override void OnDoubleClick( Mobile from )
		{
		
			if (this.Movable==true)
			{
			from.SendMessage( "This item must be locked down to be used" );
			return;
			}
			
			else if ( !from.InRange( this, 5 ) )
			{
				from.SendMessage("That is too far away"); 
				return;
			}
	
	
			int dx = from.Location.X - this.X;
			int dy = from.Location.Y - this.Y;
			int rx = (dx - dy) * 44;
			int ry = (dx + dy) * 44;

			bool eastToWest;

			if ( rx >= 0 && ry >= 0 )
			{
				eastToWest = false;
			}
			else if ( rx >= 0 )
			{
				eastToWest = true;
			}
			else if ( ry >= 0 )
			{
				eastToWest = true;
			}
			else
			{
				eastToWest = false;
			}
	
			GenerateDualWall(this.Location,this.Map,eastToWest,wallsize,10,this.Hue);
		}
		
		
		
		public static void GenerateDualWall(Point3D location,Map map,bool eastToWest,int size,int duration,int hue)
		{
	
			for ( int i = -size; i <= size; ++i )
			{					
				Point3D loc = new Point3D( eastToWest ? location.X + i : location.X, eastToWest ? location.Y : location.Y + i, location.Z );


				Item item = new InternalItem( loc, map, hue,duration );

				Effects.SendLocationParticles( item, 0x376A, 9, 10, 5025 );
			}
		}
		
		
		
		public class InternalItem : Item
		{
			private Timer m_Timer;
			private DateTime m_End;

			public override bool BlocksFit{ get{ return true; } }

			public InternalItem( Point3D loc, Map map, int hue,int duration ) : base( 0x80 )
			{
				Visible = true;
				Movable = false;
				Hue=hue;

				MoveToWorld( loc, map );

				m_Timer = new InternalTimer( this, TimeSpan.FromSeconds(duration) );
				m_Timer.Start();

				m_End = DateTime.Now + TimeSpan.FromSeconds( duration );
			}

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

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

				writer.Write( (int) 1 ); // version

				writer.WriteDeltaTime( m_End );
			}

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

				int version = reader.ReadInt();

				switch ( version )
				{
					case 1:
					{
						m_End = reader.ReadDeltaTime();

						m_Timer = new InternalTimer( this, m_End - DateTime.Now );
						m_Timer.Start();

						break;
					}
					case 0:
					{
						TimeSpan duration = TimeSpan.FromSeconds( 10.0 );

						m_Timer = new InternalTimer( this, duration );
						m_Timer.Start();

						m_End = DateTime.Now + duration;

						break;
					}
				}
			}

			public override void OnAfterDelete()
			{
				base.OnAfterDelete();

				if ( m_Timer != null )
					m_Timer.Stop();
			}

			private class InternalTimer : Timer
			{
				private InternalItem m_Item;

				public InternalTimer( InternalItem item, TimeSpan duration ) : base( duration )
				{
					Priority = TimerPriority.FiftyMS;
					m_Item = item;
				}

				protected override void OnTick()
				{
					m_Item.Delete();
				}
			}
		}
		
		

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

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

			writer.Write( (int) 1 ); // version

			writer.Write( wallsize );
		}

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

			wallsize=reader.ReadInt();
			
			if(version<=1)ItemID = 10922;
		}
	}
}