using System;
using Server.Network;
using Server;
using Server.Mobiles;
using Server.Regions;

namespace Server.Misc
{
	public class FoodDecayTimer : Timer
	{
		public static void Initialize()
		{
			new FoodDecayTimer().Start();
		}

		public FoodDecayTimer() : base( TimeSpan.FromMinutes( 5 ), TimeSpan.FromMinutes( 5 ) )
		{
			Priority = TimerPriority.OneMinute;
		}

		protected override void OnTick()
		{
			FoodDecay();
		}

		public static void FoodDecay()
		{
			foreach ( NetState state in NetState.Instances )
			{
				HungerDecay( state.Mobile );
			}
		}

		public static void HungerDecay( Mobile m )
		{
			if ( m != null && m is PlayerMobile && !(m.AccessLevel > AccessLevel.Player) && !(m.Region is InnRegion) )
			{
				if ( m.Hunger >= 1 )
					m.Hunger -= 1;
				if ( m.Thirst >= 1 )
					m.Thirst -= 1;
			}
		}
	}

	public class EatDecayTimer : Timer
	{
		public static void Initialize()
		{
			new EatDecayTimer().Start();
		}

		public EatDecayTimer() : base( TimeSpan.FromSeconds( 11.0 ), TimeSpan.FromSeconds( 11.0 ) )
		{
			Priority = TimerPriority.OneSecond;
		}

		protected override void OnTick()
		{
			EatDecay();			
		}

		public static void EatDecay()
		{
			foreach ( NetState state in NetState.Instances )
			{
				EatDecaying( state.Mobile );
			}
		}

		public static void EatDecaying( Mobile m )
		{
			if ( m is PlayerMobile && m.Alive && (m.Hunger < 5 || m.Thirst < 5) )
			{
				if ( m.Hunger < 5 )
				{
					int hits = 0;

					switch (m.Hunger)
					{
						case 4: hits = 2; break;
						case 3: hits = 3; break;
						case 2: hits = 4; break;
						case 1: hits = 5; break;
						case 0:
						{
							hits = 6;
							m.SendMessage( "You are starving to death!" );
							m.LocalOverheadMessage(MessageType.Emote, 1150, true, "I am so hungry!");
							break;
						}
					}

					if ( m.Hits < hits )
						hits = m.Hits-1;

					if ( hits > 0 )
						m.Hits -= hits;

					if ( m.Hunger < 3 && m.Hunger > 0 )
					{
						if ( Utility.RandomBool() ){ m.SendMessage( "You are getting very hungry!" ); }
						if ( Utility.RandomMinMax(1,5)==1 ){ m.LocalOverheadMessage(MessageType.Emote, 1150, true, "I am getting hungry!"); }
					}
				}
				if ( m.Thirst < 5 )
				{
					switch (m.Thirst)
					{
						case 4: m.Stam -= 2; break;
						case 3: m.Stam -= 3; break;
						case 2: m.Stam -= 4; break;
						case 1: m.Stam -= 5; break;
						case 0:
						{
							m.Stam -= 6;
							m.SendMessage( "You are exhausted from thirst!" );
							m.LocalOverheadMessage(MessageType.Emote, 1150, true, "I am so thirsty!");
							break;
						}
					}
					if ( m.Thirst < 3 && m.Thirst > 0 )
					{
						if ( Utility.RandomBool() ){ m.SendMessage( "You are getting exhausted from thirst!" ); }
						if ( Utility.RandomMinMax(1,5)==1 ){ m.LocalOverheadMessage(MessageType.Emote, 1150, true, "I am getting thirsty!"); }
					}
					if ( m.Stam < 0 )
						m.Stam = 0;
				}
				if ( m.Thirst < 5 || m.Hunger < 5 )
				{
					int test = m.Thirst;
						if ( m.Hunger < m.Thirst )
							test = m.Hunger;

					switch (test)
					{
						case 4: m.Mana -= 2; break;
						case 3: m.Mana -= 3; break;
						case 2: m.Mana -= 4; break;
						case 1: m.Mana -= 5; break;
						case 0: m.Mana -= 6; break;
					}
					if ( m.Mana < 0 )
						m.Mana = 0;
				}
			}
		}
	}
}