/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Jan-01-2012 10:09:06pm
Filepath: scripts\Items\Skill Items\Magical\Potions\BasePotion.cs
Lines of code: 226

Description: 

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


using System;
using Server;
using Server.Engines.Craft;
using Server.Mobiles;

namespace Server.Items
{
	public enum PotionEffect
	{
		Nightsight,
		CureLesser,
		Cure,
		CureGreater,
		Agility,
		AgilityGreater,
		Strength,
		StrengthGreater,
		PoisonLesser,
		Poison,
		PoisonGreater,
		PoisonDeadly,
		Refresh,
		RefreshTotal,
		HealLesser,
		Heal,
		HealGreater,
		ExplosionLesser,
		Explosion,
		ExplosionGreater,
		Conflag,
		ConflagGreater,
		Invisibility,
		Confusion,
		GreaterConfusion
	}

	public abstract class BasePotion : Item, ICraftable
	{
		private PotionEffect m_PotionEffect;

		public PotionEffect PotionEffect
		{
			get
			{
				return m_PotionEffect;
			}
			set
			{
				m_PotionEffect = value;
				InvalidateProperties();
			}
		}
		
		private int GetLabelNumber()
		{
		//the old pots follow cliloc orders, newers don't
		if((int)m_PotionEffect<20)return 1041314+(int)m_PotionEffect;
		
		//new ones:
		
			switch(m_PotionEffect)
			{
			case PotionEffect.Conflag: return 1072096; 
			case PotionEffect.ConflagGreater: return 1072099; 
			case PotionEffect.Invisibility: return 1072941;
			case PotionEffect.Confusion: return 1072107;
			case PotionEffect.GreaterConfusion: return 1072109;			
			}

		return 0;//default to "unknown potion"
		}
		
		

		
		public override int LabelNumber{ get{
			
			int lb = GetLabelNumber();
			if(lb!=0)return lb;
			return base.LabelNumber;		
		} }

		public BasePotion( int itemID, PotionEffect effect ) : base( itemID )
		{
			m_PotionEffect = effect;

			Stackable = true;
			Weight = 1.0;
		}

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

		public virtual bool RequireFreeHand{ get{ return true; } }

		public static bool HasFreeHand( Mobile m )
		{
			Item handOne = m.FindItemOnLayer( Layer.OneHanded );
			Item handTwo = m.FindItemOnLayer( Layer.TwoHanded );			
	
			if ( handTwo is BaseWeapon )
				handOne = handTwo;
				
		if(handOne is BaseWeapon && ((BaseWeapon)handOne).WeaponAttributes.Balanced!=0)return true;				

			return ( handOne == null || handTwo == null );
		}
		

		public override void OnDoubleClick( Mobile from )
		{		
		if(from is BaseCreature)Drink(from);
		
			if ( !Movable )
				return;

			if ( from.InRange( this.GetWorldLocation(), 1 ) )
			{
				if ( !RequireFreeHand || HasFreeHand( from ) )
				{
					from.RevealingAction();
					Drink( from );					
				}
				else
					from.SendLocalizedMessage( 502172 ); // You must have a free hand to drink a potion.
			}
			else
			{
				from.SendLocalizedMessage( 502138 ); // That is too far away for you to use
			}
		}

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

			writer.Write( (int) 0 ); // version

			writer.Write( (int) m_PotionEffect );
		}

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

			int version = reader.ReadInt();

			switch ( version )
			{
				case 0:
				{
					m_PotionEffect = (PotionEffect)reader.ReadInt();
					break;
				}
			}
		}

		public abstract void Drink( Mobile from );

		public static void PlayDrinkEffect( Mobile m )
		{
			m.RevealingAction();

			m.PlaySound( 0x2D6 );

			m.AddToBackpack( new Bottle() );

			if ( m.Body.IsHuman /*&& !m.Mounted*/ )
				m.Animate( 34, 5, 1, true, false, 0 );
		}
		
		//range is about 0-100
		public static int GetEnhancePot(Mobile m)
		{
		if(m==null)return 0;
		int val=AosAttributes.GetValue( m, AosAttribute.EnhancePotions );
		
		int cap = 50 + (10*((int)(m.Skills[SkillName.Alchemy].Value/33)));
		
		if(val>cap)val=cap;
		
		
		val+=(int)(m.Skills[SkillName.Alchemy].Value/5);
		
		return val;
		}
		

		public static TimeSpan Scale( Mobile m, TimeSpan v )
		{
			if ( !Core.AOS )
				return v;

			double scalar = 1.0 + (0.01 * GetEnhancePot(m));

			return TimeSpan.FromSeconds( v.TotalSeconds * scalar );
		}

		public static double Scale( Mobile m, double v )
		{
			if ( !Core.AOS )
				return v;

			double scalar = 1.0 + (0.01 * GetEnhancePot(m));

			return v * scalar;
		}

		public static int Scale( Mobile m, int v )
		{
			if ( !Core.AOS )
				return v;

			return AOS.Scale( v, 100 + GetEnhancePot(m) );
		}
		#region ICraftable Members

		public int OnCraft( int quality, bool makersMark, Mobile from, CraftSystem craftSystem, Type typeRes, BaseTool tool, CraftItem craftItem, int resHue )
		{
			if ( craftSystem is DefAlchemy )
			{
				Container pack = from.Backpack;

				if ( pack != null )
				{
					Item[] kegs = pack.FindItemsByType( typeof( PotionKeg ), true );

					for ( int i = 0; i < kegs.Length; ++i )
					{
						PotionKeg keg = kegs[i] as PotionKeg;

						if ( keg == null )
							continue;

						if ( keg.Held <= 0 || keg.Held >= 100 )
							continue;

						if ( keg.Type != PotionEffect )
							continue;

						++keg.Held;

						Delete();
						from.AddToBackpack( new Bottle() );

						return -1; // signal placed in keg
					}
				}
			}

			return 1;
		}

		#endregion
		
		/**
			public enum PotionEffect
	{
		Nightsight,
		CureLesser,
		Cure,
		CureGreater,
		Agility,
		AgilityGreater,
		Strength,
		StrengthGreater,
		PoisonLesser,
		Poison,
		PoisonGreater,
		PoisonDeadly,
		
		Refresh,
		RefreshTotal,
		HealLesser,
		Heal,
		HealGreater,
		ExplosionLesser,
		Explosion,
		ExplosionGreater,
		Conflag,
		ConflagGreater,
		Invisibility,
		Confusion,
		GreaterConfusion
	}
		*/
		
		public static BasePotion RandomPotion()
		{
			BasePotion ret = null;
			
			
			switch(Utility.Random(25))
			{
				case 0: ret = new NightSightPotion();break;
				case 1: ret = new LesserCurePotion();break;
				case 2: ret = new CurePotion();break;
				case 3: ret = new GreaterCurePotion();break;
				case 4: ret = new AgilityPotion();break;
				case 5: ret = new GreaterAgilityPotion();break;
				case 6: ret = new StrengthPotion();break;
				case 7: ret = new GreaterStrengthPotion();break;
				case 8: ret = new LesserPoisonPotion();break;
				case 9: ret = new PoisonPotion();break;
				case 10: ret = new GreaterPoisonPotion();break;
				case 11: ret = new DeadlyPoisonPotion();break;
				case 12: ret = new RefreshPotion();break;
				case 13: ret = new TotalRefreshPotion();break;
				case 14: ret = new LesserHealPotion();break;
				case 15: ret = new HealPotion();break;
				case 16: ret = new GreaterHealPotion();break;
				case 17: ret = new LesserExplosionPotion();break;
				case 18: ret = new ExplosionPotion();break;
				case 19: ret = new GreaterExplosionPotion();break;
				case 20: ret = new ConflagarationPotion();break;
				case 21: ret = new GreaterConflagarationPotion();break;
				case 22: ret = new InvisibilityPotion();break;
				case 23: ret = new ConfusionBlastPotion();break;
				case 24: ret = new GreaterConfusionBlastPotion();break;
			}
		
			return ret;		
		}
	}
}