/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\items\potions\Invisibility Potion\BaseInvisibilityPotion.cs
Lines of code: 64
***********************************************************/


using System;
using Server;
using Server.Network;

namespace Server.Items
{
	public abstract class BaseInvisibilityPotion : BasePotion
	{
		private Timer m_Timer;

		public BaseInvisibilityPotion( PotionEffect effect ) : base( 0xF06, effect )
		{
			Hue = 1165;
		}

		public BaseInvisibilityPotion( 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 void MakeInvisible( Mobile from )
		{
			Effects.SendLocationParticles( EffectItem.Create( new Point3D( from.X, from.Y, from.Z + 16 ), from.Map, EffectItem.DefaultDuration ), 0x376A, 10, 15, 5045 );
			from.PlaySound( 0x3C4 );
			
			from.SendLocalizedMessage( 503327 );
			from.Hidden = true;
		}

		public override void Drink( Mobile from )
		{
			if(UseDelaySystem.CheckContext(from, DelayContextType.InvisPotion) || UseDelaySystem.CheckContext(from, DelayContextType.Hiding))
			{
				from.LocalOverheadMessage( MessageType.Regular, 0x22, 501789 ); // You must wait before trying again
				return;
			}
			
			if ( !from.Hidden )
			{
			Timer.DelayCall( TimeSpan.FromSeconds( 3 ), new TimerStateCallback( DoHide ), from );
			
			UseDelaySystem.AddContext(from,DelayContextType.InvisPotion,TimeSpan.FromSeconds(5));
			}
			//**Need to check if mobile is hidden AND the Invisibility timer is running (2 checks prove that a pot has been used)
			else if (from.Hidden) 
			{
				from.SendLocalizedMessage( 1073186 ); // An invisibility potion is already taking effect on your person.
			}
		}
		
		
		public void DoHide(object state)
		{
			Mobile from = (Mobile)state;
			BasePotion.PlayDrinkEffect( from );

			this.Consume();
			
			TimeSpan InvisibilityExpire = TimeSpan.FromSeconds( 20 + GetEnhancePot(from)/10 );
			
			Timer.DelayCall(  InvisibilityExpire , new TimerStateCallback( RevealInvisible ), from );
			MakeInvisible( from );
		}
		
		
		
		private static void RevealInvisible( object state )
		{
			((Mobile)state).Hidden = false;
		}


	}
}