/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\items\misc\addons\FiringCannon.cs
Lines of code: 154
***********************************************************/


using System;
using Server.Network;
using Server;
using Server.Targeting; 
using Server.Items;
using Server.Mobiles;
using Server.Targets;
using System.Collections;
using Server.Multis;

namespace Server.Items
{
	public class FiringCannon : AddonComponent
	{
		 
		public static string Pirate;
		public bool IsSpawned = false;  
		 
		[Constructable]
		public FiringCannon() : base( 3729 )
		{
			Weight = 10.0;
		}
		
		public bool IsOwner( Mobile mob )
		{
			BaseHouse house = BaseHouse.FindHouseAt( this );

			return ( house != null && house.IsOwner( mob ) );
		}

		public override void OnDoubleClick( Mobile from )
		{
			if ( from.InRange( this, 1 ) )
			{
				int am = from.Backpack.GetAmount(typeof(SulfurousAsh));
				int bm = from.Backpack.GetAmount(typeof(BolaBall));
				
				if (am >= 50 && bm >= 1)
				{
					from.Target = new KeyTarget( this );
					from.SendMessage( "Select Target to launch a cannonball at" );
				}
				else
					from.SendMessage("You need 50 sulphurous ash and 1 bola ball to fire a cannonball");
			}	
			else
			{
				from.SendLocalizedMessage( 500295 ); // You are too far away
			}
		}
		
		private class KeyTarget : Target 
		{ 
			private FiringCannon m_Key;

			public KeyTarget( FiringCannon knife ) : base( 15, false, TargetFlags.None )
			{
				m_Key = knife;
			}
			
			protected override void OnTarget( Mobile from, object target ) 
			{ 
				if ( target is Mobile ) 
				{	
					Mobile victim = (Mobile)target; 
					
					if (victim == null) return;
					
					if (!from.InRange( m_Key, 1 )) { 
						from.SendMessage("You are too far away from the cannon to fire");
						return;
					}
					if (!from.InLOS( victim )) { 
						from.SendLocalizedMessage( 500346 ); // Target cannot be seen
						return;
					}
					if (!from.InRange( victim, 15 )) { 
						from.SendLocalizedMessage( 500446 ); // That is too far away
						return;
					}
				
					from.Backpack.ConsumeTotal(typeof(SulfurousAsh), 50 );
					from.Backpack.ConsumeTotal(typeof(BolaBall), 1 );
					
					Map map = from.Map;

					if ( map == null || map == Map.Internal ) return;
				
					Effects.SendLocationParticles( EffectItem.Create( new Point3D(m_Key.X, m_Key.Y,m_Key.Z+2), m_Key.Map, EffectItem.DefaultDuration ), 0x36B0, 1, 23, 0, 7, 9915, 0 );
				
					Effects.SendPacket( victim, victim.Map, new HuedEffect( EffectType.Moving, Serial.Zero, Serial.Zero, 3699, m_Key.Location, victim.Location, 10, 0, false, false, 0, 0 ) );
				
					AOS.Damage( victim, from, 200, true, 100, 0, 0, 0, 0 );
					Effects.PlaySound( from.Location, from.Map, 519 );
				}
				else
				{
					from.SendMessage("Cannonballs can only be fired at players or creatures");
				}
			}
		}

		public FiringCannon( 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();
		}
	}
	
	public class FiringCannonAddon : BaseAddon
	{
		public override BaseAddonDeed Deed{ get{ return new FiringCannonDeed(); } }

		[Constructable]
		public FiringCannonAddon()
		{
			//if( east )
			//{
				AddComponent( new FiringCannon( ), 0, 0, 0 );
				AddComponent( new AddonComponent( 3731 ), 0, -2, 0 );
			//}
			/*else
			{
				AddComponent( new SacrificialAltar( 0x2A9B, 1074818 ), 0, 0, 0 );
				AddComponent( new SacrificialAltar( 0x2A9A, 1074818 ), 1, 0, 0 );
			}*/
		}

		public FiringCannonAddon( 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();
		}
	}

	public class FiringCannonDeed : BaseAddonDeed
	{
		//private bool m_East;

		public override BaseAddon Addon{ get{ return new FiringCannonAddon( ); } }

		[Constructable]
		public FiringCannonDeed()
		{
			//LootType = LootType.Blessed;
			Name = "A Deed For A Cannon";
		}

		public override void OnDoubleClick( Mobile from )
		{
			if ( IsChildOf( from.Backpack ) )
			{
				//from.CloseGump( typeof( InternalGump ) );
				//from.SendGump( new InternalGump( this ) );
			}
			else
			{
				from.SendLocalizedMessage( 1042001 ); // That must be in your pack for you to use it.
			}
		}

		/*private void SendTarget( Mobile m )
		{
			base.OnDoubleClick( m );
		}

		private class InternalGump : Gump
		{
			private FiringCannonDeed m_Deed;

			public InternalGump( FiringCannonDeed deed ) : base( 150, 50 )
			{
				m_Deed = deed;

				AddBackground( 0, 0, 350, 350, 0xA28 );

				AddItem( 125, 50, 0x2A9D );
				AddItem( 107, 84, 0x2A9C );
				AddButton( 70, 35, 0x868, 0x869, 1, GumpButtonType.Reply, 0 ); // SacrificialAltarEast

				AddItem( 210, 50, 0x2A9B );
				AddItem( 231, 83, 0x2A9A );
				AddButton( 185, 35, 0x868, 0x869, 2, GumpButtonType.Reply, 0 ); // SacrificialAltarSouth
			}

			public override void OnResponse( NetState sender, RelayInfo info )
			{
				if ( m_Deed.Deleted || info.ButtonID == 0 || info.ButtonID > 2 )
					return;
				switch ( info.ButtonID )
				{
					case 1:
					{
						m_Deed.m_East = true;
						break;
					}
					case 2:
					{
						m_Deed.m_East = false;
						break;
					}
				}
				m_Deed.SendTarget( sender.Mobile );
			}
		}*/

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

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

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

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

			int version = reader.ReadEncodedInt();
		}
	}
	
	
}