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


using System;
using System.Collections;
using Server;
using Server.Network;
using Server.Targeting;
using Server.Spells;
using Server.Misc;
using Server.Items;
using Server.Mobiles;

namespace Server.Items
{
	public abstract class BaseSummoningPotion : BasePotion
	{
		//public abstract int MinDamage { get; }
		//public abstract int MaxDamage { get; }

		public override bool RequireFreeHand{ get{ return false; } }
		
		//public override bool CanBeTriggered{get{return true;} }
		//public abstract int ExplosionRange { get; }
		
		public static int x;
		public static int y;
		public static int z;


		public BaseSummoningPotion( PotionEffect effect ) : base( 3846, effect )
		{
			Hue = 33;
		}

		public BaseSummoningPotion( 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 override void Drink( Mobile from )
		{
			if ( Core.AOS && (from.Paralyzed || from.Frozen || (from.Spell != null && from.Spell.IsCasting)))
			{
				from.SendMessage( "You cannot use this at this moment" );
				return;
			}
			//else if(UseDelaySystem.CheckContext(from, DelayContextType.ConflagPotion))
			//{
			//	from.SendMessage("You must wait 20 seconds between summoning potions");
			//	return;	
			//}

			
			ThrowTarget targ = from.Target as ThrowTarget;

			if ( targ != null && targ.Potion == this )
				return;			
			
			
			if(!IsChildOf( from.Backpack ))
			{
				Summon( from, true, Location, Map );
			}
			else
			{				
				from.SendMessage("Target a location to throw the potion");
				from.Target = new ThrowTarget( this,from );
			}

			from.RevealingAction();
		}
		
		
		
		/*public override bool OnTrigger(Mobile from,Item itemfrom,TriggerType type)
		{
		if(0.1>Utility.RandomDouble())return false;
		
		bool dotrigger=false;
		if(type==TriggerType.Explosion)dotrigger=true;
		else if(type==TriggerType.Impact && 0.2>Utility.RandomDouble())dotrigger=true;
		else if(type==TriggerType.Heat && 0.4>Utility.RandomDouble())dotrigger=true;
		
		if(dotrigger)Summon( from, false, this.Location,this.Map );
		
		return dotrigger;
		}*/		
		
		
		

		private void Reposition_OnTick( object state )
		{
			if ( Deleted )
				return;

			object[] states = (object[])state;
			Mobile from = (Mobile)states[0];
			IPoint3D p = (IPoint3D)states[1];
			Map map = (Map)states[2];

			Point3D loc = new Point3D( p );
			

			Summon( from, true, loc, map );
		}

		public class ThrowTarget : Target
		{
			private BaseSummoningPotion m_Potion;

			public BaseSummoningPotion Potion
			{
				get{ return m_Potion; }
			}

			public ThrowTarget( BaseSummoningPotion potion,Mobile from ) : base( 12, true, TargetFlags.None )
			{
			m_Potion = potion;
			
					if(potion.Amount>1)
					{
					Type t = potion.GetType();
					
					Item newstack = Activator.CreateInstance(t) as Item;
					newstack.Amount=potion.Amount-1;
					m_Potion.Amount=1;
					m_Potion.Stackable=false; //so new stack wont just get readded to it, this particular pot is going to blow anyway
					
					if(!potion.IsChildOf( from.Backpack ))newstack.MoveToWorld(from.Location,from.Map);
					else if(from.Backpack!=null)
					{
					Container pack = from.Backpack;
					pack.AddItem(newstack);					
					}
					newstack.X=m_Potion.X;
					newstack.Y=m_Potion.Y;					
					}			
			}

			protected override void OnTarget( Mobile from, object targeted )
			{
				if ( m_Potion.Deleted || m_Potion.Map == Map.Internal )
					return;
					
					//x = targeted.X;
					//y = targeted.Y - 2;
					//z = targeted.Z;

				IPoint3D p = targeted as IPoint3D;

				if ( p == null )
					return;

				Map map = from.Map;

				if ( map == null )
					return;
								
				UseDelaySystem.AddContext(from, DelayContextType.ConflagPotion, TimeSpan.FromSeconds(20));

				SpellHelper.GetSurfaceTop( ref p );

				from.RevealingAction();

				IEntity to;

				if ( p is Mobile )
					to = (Mobile)p;
				else
					to = new Entity( Serial.Zero, new Point3D( p ), map );
					

				UseDelaySystem.AddContext(from, DelayContextType.ConflagPotion, TimeSpan.FromSeconds(20));

				Effects.SendMovingEffect( from, to, m_Potion.ItemID & 0x3FFF, 7, 0, false, false, m_Potion.Hue, 0 );

				//m_Potion.Internalize();
				Timer.DelayCall( TimeSpan.FromSeconds( 0.7 ), new TimerStateCallback( m_Potion.Reposition_OnTick ), new object[]{ from, p, map } );
			}
			
			protected override void OnTargetCancel( Mobile from, TargetCancelType cancelType )
			{
			UseDelaySystem.RemoveContext(from,DelayContextType.ConflagPotion);
			}			
		}

		public void Summon( Mobile from, bool direct, Point3D loc, Map map )
		{
			if ( Deleted )
				return;
			
			
			TimeSpan duration = TimeSpan.FromSeconds( (2 * from.Skills.Magery.Fixed) / 5 );
			//int duration = 15;
			//int damage=MinDamage + Utility.Random(MaxDamage-MinDamage);
			
			//duration+=Amount/10;
			
			//int range=ExplosionRange;
			
			/*for(int i=0;i<=range*2;i++)
			{
				for(int ii=0;ii<=range*2;ii++)
				{
					if(0.2>Utility.RandomDouble())new InternalItem( Utility.RandomBool() ? 0x398C : 0x3996, new Point3D(loc.X-range+i,loc.Y-range+ii,loc.Z), map,TimeSpan.FromSeconds(duration), 0 );
				}
			}*/
						
			//wildfirecenter core = new wildfirecenter(duration,range,damage,from);
			//core.MoveToWorld(loc,map);
			
			//SpellHelper.Summon( new SummonedDaemon(), from, 0x216, duration, false, false );
			
			BeginPentagram(from, loc, map);
			
			BaseCreature demon = Activator.CreateInstance( typeof( AlchemySummonedImp ) ) as BaseCreature;
			
			if (from.Skills.Alchemy.Value < 20.0 ) {
				demon = Activator.CreateInstance( typeof( AlchemySummonedImp ) ) as BaseCreature;
			}
			else if (from.Skills.Alchemy.Value >= 20.0 && from.Skills.Alchemy.Value < 50.0) {
				demon = Activator.CreateInstance( typeof( AlchemySummonedArcaneDaemon ) ) as BaseCreature;
			}
			else if (from.Skills.Alchemy.Value >= 50.0 && from.Skills.Alchemy.Value < 75.0) {
				demon = Activator.CreateInstance( typeof( AlchemySummonedDaemon ) ) as BaseCreature;
			}
			else if (from.Skills.Alchemy.Value >= 75.0 && from.Skills.Alchemy.Value < 100.0) {
				demon = Activator.CreateInstance( typeof( AlchemySummonedDevourer ) ) as BaseCreature;
			}
			else if (from.Skills.Alchemy.Value >= 100.0) {
				demon = Activator.CreateInstance( typeof( AlchemySummonedBalron ) ) as BaseCreature;
			}

			//demon.ControlSlots=3; 
			demon.Controlled = true;
			demon.ControlMaster = from;
			demon.MoveToWorld( loc, map );	
			demon.FixedParticles( 0x3709, 1, 30, 9904, 1108, 6, EffectLayer.RightFoot );
			

			Effects.PlaySound( loc, map, 0x5CF );
			Delete();			
		}
		
		public virtual void BeginPentagram( Mobile defender, Point3D loc, Map map  )
		{
			if ( Deleted || defender==null)
				return;
				
			defender.SendMessage("A demon leaps out of the flaming pentagram to do your bidding");
			//new PentagramAddon().MoveToWorld( loc, map );
			
			Item pentagram = new BloodPentagram();
			pentagram.Hue = 1;
			
			//Timer.DelayCall( TimeSpan.FromSeconds( 5.0 ), new TimerCallback( pentagram.Delete() ) );
			
			
			//Point3D spawnpoint = new Point3D( x, y, z);
			
			pentagram.MoveToWorld( loc, map );
			//new FormedPentagram().MoveToWorld( loc, map );
			PlaySound( 0x247 );

			//Timer.DelayCall( TimeSpan.FromSeconds( 2.0 ), new TimerCallback( pentagram.Delete() ) );
		}	
		
		private void Appear_Callback( object state )
		{
			/*Mobile defender = state as Mobile;

			if ( defender != null )
			{
				defender.Frozen = false;
				defender.Combatant = null;
				
				if (!defender.InRange( this, 12 ))
				{
					this.MoveToWorld( new Point3D( this.X, this.Y, 0 ), Map );
				}
				else
				{
					this.Location = defender.Location;
					int BurstAttack = Utility.RandomMinMax( 20, 50 );
					AOS.Damage(defender, this, BurstAttack, 100, 0, 0, 0, 0 );
				}
				//new FormedPentagram().MoveToWorld( Location, Map );
				new BloodPentagram().MoveToWorld(Location, Map);
				this.Frozen = false;
				this.Hidden = false;
				this.Blessed = false;
				defender.SendMessage("The antlion bursts out of the ground");
				PlaySound ( 0x164 );
			}*/
		}
		
		public class FormedPentagram : Item
		{
		public override bool DisplayWeight { get { return ( false ); } }			
		
			public FormedPentagram() : base( 4075 )
			{
				Item pentagram = new BloodPentagram();
				pentagram.Hue = 1;
				//pentagram.MoveToWorld( loc, map );
				
				//Movable = false;
				//Hue = 1175;
				//Name = "a pentagram";

				Timer.DelayCall( TimeSpan.FromSeconds( 5.0 ), new TimerCallback( Delete ) );
			}

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

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

				writer.Write( (int) 0 );
			}

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

				int version = reader.ReadInt();

				Delete();
			}
			
		}
	}	
	
}

	
