using System;
using Server;
using Server.Targeting;
using Server.Items;
using Server.Engines.Harvest;
using Server.Mobiles;

namespace Server.Targets
{
	public class BladedItemTarget : Target
	{
		private Item m_Item;

		public BladedItemTarget( Item item ) : base( 2, false, TargetFlags.None )
		{
			m_Item = item;
		}

		protected override void OnTarget( Mobile from, object targeted )
		{
			if ( m_Item.Deleted )
				return;

			if ( targeted is ICarvable )
			{
				((ICarvable)targeted).Carve( from, m_Item );
			}
			else
			{
				HarvestSystem system = Lumberjacking.System;
				HarvestDefinition def = Lumberjacking.System.Definition;

				int tileID;
				Map map;
				Point3D loc;

				if ( !system.GetHarvestDetails( from, m_Item, targeted, out tileID, out map, out loc ) )
				{
					from.SendLocalizedMessage( 500494 ); // You can't use a bladed item on that!
				}
				else if ( !def.Validate( tileID ) )
				{
					from.SendLocalizedMessage( 500494 ); // You can't use a bladed item on that!
				}
				else
				{
					HarvestBank bank = def.GetBank( map, loc.X, loc.Y );

					if ( bank == null )
						return;

					if ( bank.Current < 5 )
					{
						from.SendLocalizedMessage( 500493 ); // There's not enough wood here to harvest.
					}
					else
					{
						bank.Consume( 5, from );

						if ( tileID == 0x4D96 || tileID == 0x4D9A ) // apples
						{
							Item item = new Apple();

							if ( from.PlaceInBackpack( item ) )
							{
								from.SendMessage( "You put an apple into your backpack." );
							}
							else
							{
								from.SendMessage( "You can't place any apples into your backpack!" );
								item.Delete();
							}
						}
						else if ( tileID == 0x4DA6 || tileID == 0x4DAA ) // pears
						{
							Item item = new Pear();

							if ( from.PlaceInBackpack( item ) )
							{
								from.SendMessage( "You put a pear into your backpack." );
							}
							else
							{
								from.SendMessage( "You can't place any pears into your backpack!" );
								item.Delete();
							}
						}
						else if ( tileID == 0x4D9E || tileID == 0x4DA2 ) // peaches
						{
							Item item = new Peach();

							if ( from.PlaceInBackpack( item ) )
							{
								from.SendMessage( "You put a peach into your backpack." );
							}
							else
							{
								from.SendMessage( "You can't place any peaches into your backpack!" );
								item.Delete();
							}
						}
						else if ( tileID == 0x4CA8 || tileID == 0x4CAA || tileID == 0x4CAB ) // bananas
						{
							Item item = new Banana();

							if ( from.PlaceInBackpack( item ) )
							{
								from.SendMessage( "You put a banana into your backpack." );
							}
							else
							{
								from.SendMessage( "You can't place any bananas into your backpack!" );
								item.Delete();
							}
						}
						else if ( tileID == 0x4C95 ) // coconut
						{
							Item item = new Coconut();

							if ( from.PlaceInBackpack( item ) )
							{
								from.SendMessage( "You put a coconut into your backpack." );
							}
							else
							{
								from.SendMessage( "You can't place any coconuts into your backpack!" );
								item.Delete();
							}
						}
						else if ( tileID == 0x4C96 ) // dates
						{
							Item item = new Dates();

							if ( from.PlaceInBackpack( item ) )
							{
								from.SendMessage( "You put some dates into your backpack." );
							}
							else
							{
								from.SendMessage( "You can't place any dates into your backpack!" );
								item.Delete();
							}
						}
						else
						{
							Item item = new Kindling();

							if ( from.PlaceInBackpack( item ) )
							{
								from.SendLocalizedMessage( 500491 ); // You put some kindling into your backpack.
								from.SendLocalizedMessage( 500492 ); // An axe would probably get you more wood.
							}
							else
							{
								from.SendLocalizedMessage( 500490 ); // You can't place any kindling into your backpack!

								item.Delete();
							}
						}
					}
				}
			}
		}
	}
}