/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: http://www.uovalor.com
Last modified by Death on Jan-25-2011 08:34:25pm
Filepath: scripts\_custom\engines\Dye System\PigmentBottle.cs
Lines of code: 133
***********************************************************/


using Server;
using System;
using Server.Misc;
using Server.Mobiles;
using System.Collections;
using Server.Items;


	public class PigmentBottle : Item
	{

	private int m_UsesRemaining;
	private string colorname;

		[Constructable]
		public PigmentBottle(int hue, string label) : base(6215)
		{
		m_UsesRemaining = 1;
		Hue = hue;
		colorname = label;
		
		Name="A Bottle Of Pigment";		
		}
		
		[Constructable]
		public PigmentBottle(int hue, string label, int uses) : base(6215)
		{
		m_UsesRemaining = uses;
		Hue = hue;
		colorname = label;
		
		Name="A Bottle Of Pigment";		
		}
		
		public override void GetProperties( ObjectPropertyList list )
		{
			base.GetProperties( list );
			if (colorname != null) {
				list.Add( 1060658,"Pigment Color \t" + colorname);
			}
			list.Add( 1060584, m_UsesRemaining.ToString() ); // uses remaining: ~1_val~
		}


		public override void OnDoubleClick( Mobile from )
		{
			if(from==null)return;
			if( IsAccessibleTo( from )  && IsChildOf( from.Backpack ) )
			{
				from.SendMessage( "Select an item to dye"); // Select the artifact or enhanced magic item to dye.
				from.BeginTarget( 3, false, Server.Targeting.TargetFlags.None, new TargetStateCallback( InternalCallback ), this );
			}
			else
				from.SendLocalizedMessage( 502436 ); // That is not accessible.
		}

		private void InternalCallback( Mobile from, object targeted, object state )
		{
			PigmentBottle pigment = (PigmentBottle)state;

			if(!IsChildOf( from.Backpack ))
			{
			from.SendMessage("Pigments must be in your backpack to be used!");
			return;
			}
			
			if( pigment.Deleted || pigment.UsesRemaining <= 0 || !from.InRange( pigment.GetWorldLocation(), 3 ) || !pigment.IsAccessibleTo( from ))
				return;

			Item i = targeted as Item;

			bool isvalid= (i is BaseArmor ||i is BaseJewel || i is Quiver || i is BaseTalisman || i is BaseWeapon || i is EtherealMount || i is BaseDecorationArtifact || i is Spellbook || i is BaseClothing || i is Bottle || i is PigmentBottle);
			
			if( i == null || !isvalid)
				from.SendMessage( "This cannot be dyed" ); 
			else if( !from.InRange( i.GetWorldLocation(), 3 ) || !IsAccessibleTo( from ) || !i.IsChildOf( from.Backpack ))
				from.SendLocalizedMessage( 502436 ); // That is not accessible.
			else if( from.Items.Contains( i ) )
				from.SendLocalizedMessage( 1070930 ); // Can't dye artifacts or enhanced magic items that are being worn.
			else if( i.IsLockedDown )
				from.SendLocalizedMessage( 1070932 ); // You may not dye artifacts and enhanced magic items which are locked down.
			else if( i is PigmentsOfTokuno || i is pigmentsoffel)
				from.SendLocalizedMessage( 1042417 ); // You cannot dye that.
			else if ( i is PigmentBottle && i.Hue != this.Hue) {
				from.SendMessage( "You cannot transfer dye charges if the colors are different" ); 
			}
			else if ( i is PigmentBottle && i.Hue == this.Hue) {
				from.PlaySound(576);
				from.SendMessage( "You transfer some dye to another bottle of pigment" ); 
				
				PigmentBottle b = (PigmentBottle)i;
				b.UsesRemaining++;
				
				if( --pigment.UsesRemaining <= 0 ) {
					pigment.Delete();
					from.AddToBackpack(new Bottle());
				}
			}
			else if (i is Bottle) {
				from.PlaySound(576);
				from.SendMessage( "You create a new bottle of pigment" ); 
				PigmentBottle newbottle = new PigmentBottle(this.Hue, this.Name);
				
				from.AddToBackpack(newbottle);
				newbottle.X=i.X;
				newbottle.Y=i.Y;
				
				if( --pigment.UsesRemaining <= 0 ) {
					pigment.Delete();
					from.AddToBackpack(new Bottle());
				}
				
				i.Amount--;
				if(i.Amount==0)i.Delete();				
			}
			else
			{
				if (i.Hue == this.Hue) {
					from.SendMessage( "This item is already that color." ); 
				}
				else {
					i.Hue = this.Hue;
					from.PlaySound(0x23E);

					if( --pigment.UsesRemaining <= 0 ) {
						pigment.Delete();
						from.AddToBackpack(new Bottle());
					}
				}
			}
		}

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

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

			writer.Write( (int)0 );

			writer.WriteEncodedInt( m_UsesRemaining );
			writer.Write((string)colorname);
		}

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

			int version = reader.ReadInt();

			m_UsesRemaining = reader.ReadEncodedInt();
			colorname = reader.ReadString();
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public int UsesRemaining
		{
			get { return m_UsesRemaining; }
			set { m_UsesRemaining = value; InvalidateProperties(); }
		}


	}
	
