/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Jan-01-2012 10:09:01pm
Filepath: scripts\Commands\DupeHouse.cs
Lines of code: 97

Description: 

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


using System;
using System.Reflection;
using Server.Items;
using Server.Targeting;
using Server.Items;
using Server.Multis;

namespace Server.Commands
{
	public class DupeHouse
	{
		public static void Initialize()
		{
			CommandSystem.Register( "DupeHouse", AccessLevel.Seer, new CommandEventHandler( DupeHouse_OnCommand ),false );
		}

		[Usage( "DupeHouse [nesw offset]" )]
		[Description( "DupeHouses a targeted item." )]
		private static void DupeHouse_OnCommand( CommandEventArgs e )
		{
			if ( e.Length >= 2 )
			{			
				string dir = e.GetString( 0 );
				int tiles = e.GetInt32(1);
				
				if(tiles<=0)
				{
					e.Mobile.SendMessage( "Invalid tile offset, must be at least 1" );
					return; //avoid duping a house right on itself
				}
				
				e.Mobile.Target = new DupeHouseTarget(dir,tiles);
			}
			else
			{				
				e.Mobile.Target = new DupeHouseTarget();
			}
			
			e.Mobile.SendMessage( "What house do you wish to Dupe?" );
		}
		

		private class DupeHouseTarget : Target
		{		
			int m_Xoffset=0;
			int m_Yoffset=0;
			
			public DupeHouseTarget(string dir,int tiles) : base( 15, false, TargetFlags.None )
			{
				switch(dir)
				{
					case "n": m_Yoffset=tiles*-1; break;
					case "e": m_Xoffset=tiles; break;
					case "s": m_Yoffset=tiles; break;
					case "w": m_Xoffset=tiles*-1; break;
				}
			}
			
			public DupeHouseTarget() : base( 15, false, TargetFlags.None )
			{
			}			

			protected override void OnTarget( Mobile from, object targ )
			{
				bool done = false;
				if ( !( targ is HouseSign) || !(((HouseSign)targ).Owner is HouseFoundation))
				{
					from.SendMessage( "You can only dupe custom houses." );
					return;
				}
				
				HouseFoundation copyfrom = ((HouseSign)targ).Owner as HouseFoundation;
				
				if(m_Xoffset==0 && m_Yoffset==0)
				{				
					from.SendMessage( "Select location for new house");
					from.Target = new DupeHousePlaceTarget(copyfrom);					
				}
				else
				{
					from.SendMessage( "Duping with tile offset {0},{1}",m_Xoffset,m_Yoffset);
					HouseFoundation newhouse = new HouseFoundation(copyfrom);					
					
					newhouse.MoveToWorld( new Point3D(copyfrom.X+m_Xoffset,copyfrom.Y+m_Yoffset,copyfrom.Z), from.Map );
				}
				
			}
		}

		
		
		private class DupeHousePlaceTarget : Target
		{
			HouseFoundation m_tocopy;
			
			public DupeHousePlaceTarget(HouseFoundation tocopy) : base( 12, true, TargetFlags.None )
			{
				m_tocopy=tocopy;
			}

			protected override void OnTarget( Mobile from, object o )
			{
				if ( o is IPoint3D)
				{					
					IPoint3D loc = (IPoint3D)o;
					
					HouseFoundation newhouse = new HouseFoundation(m_tocopy);					
					
					newhouse.MoveToWorld( new Point3D(loc.X,loc.Y,loc.Z), from.Map );
					
				}
				else
				{
					from.SendMessage("Invalid target");	
				}
			}
			
		}
		
		
	}
}
