/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\Mobiles\Monsters\SE\BakeKitsune.cs
Lines of code: 263
***********************************************************/


using System;
using System.Collections;
using Server.Items;
using Server.Targeting;
using Server.Spells;
using Server.Spells.Seventh;
using Server.Spells.Fifth;
using Server.Spells.Necromancy;
using Server.Spells.Ninjitsu;
using Server.Spells.Spellweaving;
using Server.Engines.CannedEvil;

namespace Server.Mobiles
{
	[CorpseName( "a bake kitsune corpse" )]
	public class BakeKitsune : BaseCreature
	{
	
		private static DateTime lastmorph;

		[Constructable]
		public BakeKitsune() : base( AIType.AI_Super, FightMode.Closest, 10, 1, 0.2, 0.4 )
		{
			Name = "a bake kitsune";
			Body = 246;

			SetStr( 171, 220 );
			SetDex( 126, 145 );
			SetInt( 376, 425 );

			SetHits( 301, 350 );

			SetDamage( 15, 22 );

			SetDamageType( ResistanceType.Physical, 70 );
			SetDamageType( ResistanceType.Energy, 30 );

			SetResistance( ResistanceType.Physical, 40, 60 );
			SetResistance( ResistanceType.Fire, 70, 90 );
			SetResistance( ResistanceType.Cold, 40, 60 );
			SetResistance( ResistanceType.Poison, 40, 60 );
			SetResistance( ResistanceType.Energy, 40, 60 );

			SetSkill( SkillName.EvalInt, 80.1, 90.0 );
			SetSkill( SkillName.Magery, 80.1, 90.0 );
			SetSkill( SkillName.MagicResist, 80.1, 100.0 );
			SetSkill( SkillName.Tactics, 70.1, 90.0 );
			SetSkill( SkillName.Wrestling, 50.1, 55.0 );

			Fame = 8000;
			Karma = -8000;

			Tamable = true;
			ControlSlots = 2;
			MinTameSkill = 80.7;

			if ( Utility.RandomDouble() < .25 )
				PackItem( Engines.Plants.Seed.RandomBonsaiSeed() );	
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.FilthyRich );
			AddLoot( LootPack.Rich );
			AddLoot( LootPack.MedScrolls, 2 );
		}

		public override int Meat{ get{ return 5; } }
		public override int Hides{ get{ return 10; } }
		public override HideType HideType{ get{ return HideType.Barbed; } }
		public override FoodType FavoriteFood{ get{ return FoodType.Fish; } }
		
		public override void OnThink()
		{
			if ( !IsBodyMod && 0.001 > Utility.RandomDouble() && !this.Controlled && this.ControlMaster==null && this.Alive && (lastmorph+TimeSpan.FromMinutes(10))<DateTime.Now ) 
					Polymorph( this );
					
			if (this.Controlled && this.ControlMaster!=null && IsBodyMod) 
			{
					this.BodyMod = 0;
					this.HueMod = -1;
					this.NameMod = null;
					lastmorph = DateTime.Now;
					this.EndAction( typeof( PolymorphSpell ) );
			}
			
			base.OnThink();
		}
		
		public void Polymorph( Mobile m )
		{
			if ( !m.CanBeginAction( typeof( PolymorphSpell ) ) || !m.CanBeginAction( typeof( IncognitoSpell ) ) || m.IsBodyMod )
				return;

			IMount mount = m.Mount;

			if ( mount != null )
				mount.Rider = null;

			if ( m.Mounted )
				return;

			if ( m.BeginAction( typeof( PolymorphSpell ) ) )
			{
			
				Item disarm = m.FindItemOnLayer( Layer.OneHanded );

				if ( disarm != null && disarm.Movable )
					m.AddToBackpack( disarm );

				disarm = m.FindItemOnLayer( Layer.TwoHanded );

				if ( disarm != null && disarm.Movable )
					m.AddToBackpack( disarm );
					
				switch ( Utility.Random( 14 ) )
				{
					default:
					case 0: 
						m.NameMod = "a yomotsu warrior";
						m.BodyMod = 245;
						break;
					case 1: 
						m.NameMod = "a deathwatch beetle";
						m.BodyMod = 242; 
						break;
					case 2:
						m.NameMod = "a fan dancer";
						m.BodyMod = 247;
						break;
					case 3: 
						m.NameMod = "a kappa";
						m.BodyMod = 240;
						break;
					case 4: 
						m.NameMod = "a kaze kemono";
						m.BodyMod = 196;
						break;
					case 5: 
						m.NameMod = "a lady of the snow";
						m.BodyMod = 252;
						break;
					case 6: 
						m.NameMod = "an oni";
						m.BodyMod = 241;
						break;
					case 7: 
						m.NameMod = "a Rai-Ju";
						m.BodyMod = 199;
						break;
					case 8:
						m.NameMod = "a Revenant Lion";
						m.BodyMod = 251;
						break;
					case 9: 
						m.NameMod = "a rune beetle";
						m.BodyMod = 244;
						break;
					case 10: 
						m.NameMod = "a tsuki wolf";
						m.BodyMod = 250;
						break;
					case 11: 
						m.NameMod = "a yamandon";
						m.BodyMod = 249;
						break;
					case 12:
						m.NameMod = "a yomotsu elder";
						m.BodyMod = 255;
						break;
					case 13: 
						m.NameMod = "a yomotsu priest";
						m.BodyMod = 253;
						break;
				}

				m.HueMod = 0;

				new ExpirePolymorphTimer( m ).Start();
			}
		}

		private class ExpirePolymorphTimer : Timer
		{
			private Mobile m_Owner;

			public ExpirePolymorphTimer( Mobile owner ) : base( TimeSpan.FromMinutes( 7.0 ) )
			{
				m_Owner = owner;

				Priority = TimerPriority.OneSecond;
			}

			protected override void OnTick()
			{
				if ( !m_Owner.CanBeginAction( typeof( PolymorphSpell ) ) )
				{
					m_Owner.BodyMod = 0;
					m_Owner.HueMod = -1;
					m_Owner.NameMod = null;
					lastmorph = DateTime.Now;
					m_Owner.EndAction( typeof( PolymorphSpell ) );
				}
			}
		}
		
		//public static readonly TimeSpan MorphDelay = TimeSpan.FromSeconds( 10.0 );

		public override void OnGaveMeleeAttack( Mobile defender )
		{
			if (IsBodyMod) {
				this.EndAction( typeof( PolymorphSpell ) );
			}
		
			base.OnGaveMeleeAttack( defender );
			
			this.EndAction( typeof( PolymorphSpell ) );

			if ( 0.1 > Utility.RandomDouble() )
			{
				/* Blood Bath
				 * Start cliloc 1070826
				 * Sound: 0x52B
				 * 2-3 blood spots
				 * Damage: 2 hps per second for 7 seconds
				 * End cliloc: 1070824
				 */
			
				ExpireTimer timer = (ExpireTimer)m_Table[defender];

				if ( timer != null )
				{
					timer.DoExpire();
					defender.SendLocalizedMessage( 1070825 ); // The creature continues to rage!
				}
				else
					defender.SendLocalizedMessage( 1070826 ); // The creature goes into a rage, inflicting heavy damage!

				timer = new ExpireTimer( defender, this );
				timer.Start();
				m_Table[defender] = timer;
			}
		}

		private static Hashtable m_Table = new Hashtable();
	
		private class ExpireTimer : Timer
		{
			private Mobile m_Mobile;
			private Mobile m_From;
			private int m_Count;

			public ExpireTimer( Mobile m, Mobile from ) : base( TimeSpan.FromSeconds( 1.0 ), TimeSpan.FromSeconds( 1.0 ) )
			{
				m_Mobile = m;
				m_From = from;
				Priority = TimerPriority.TwoFiftyMS;
			}

			public void DoExpire()
			{
				Stop();
				m_Table.Remove( m_Mobile );
			}

			public void DrainLife()
			{
				if ( m_Mobile.Alive )
					m_Mobile.Damage( 2, m_From );
				else
					DoExpire();
			}

			protected override void OnTick()
			{
				DrainLife();

				if ( ++m_Count >= 7 )
				{
					DoExpire();
					m_Mobile.SendLocalizedMessage( 1070824 ); // The creature's rage subsides.
				}
			}
		}
		
		public override int GetAngerSound()
		{
			return 0x4DE;
		}

		public override int GetIdleSound()
		{
			return 0x4DD;
		}

		public override int GetAttackSound()
		{
			return 0x4DC;
		}

		public override int GetHurtSound()
		{
			return 0x4DF;
		}

		public override int GetDeathSound()
		{
			return 0x4DB;
		}

		public BakeKitsune( 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();
		}
	}
}
