using System;
using System.Xml;
using Server;
using Server.Mobiles;
using Server.Gumps;
using Server.Network;

namespace Server.Regions
{
	public class DungeonRegion : BaseRegion
	{
		public DungeonRegion( XmlElement xml, Map map, Region parent ) : base( xml, map, parent )
		{
		}

		public override bool AllowHousing( Mobile from, Point3D p )
		{
			return false;
		}

		public override bool NoMounts( Mobile from, Point3D p )
		{
			return true;
		}

		public override void AlterLightLevel( Mobile m, ref int global, ref int personal )
		{
			global = LightCycle.DungeonLevel;
		}

		public override bool CanUseStuckMenu( Mobile m )
		{
			return false;
		}

		public override void OnEnter( Mobile m )
		{
			base.OnEnter( m );
			if ( m is PlayerMobile )
				m.SendMessage( "You have entered the <BASEFONT COLOR='#FF0000'>{0}</BASEFONT>.", this.Name );
		}

		public override void OnExit(Mobile m)
		{
			base.OnExit( m );
			if ( m is PlayerMobile )
			{
				if ( !PlayersLeftInRegion( m, this ) )
				{
					foreach ( Mobile creature in World.Mobiles.Values )
					if ( creature.Region == this && creature is BaseCreature )
					{
						BaseCreature dweller = (BaseCreature)creature;
						Point3D loc = dweller.Home;
						if ( loc.X > 0 && dweller.RangeHome < 10 && dweller.SummonMaster == null && dweller.ControlMaster == null )
						{
							dweller.Location = dweller.Home;
						}
					}
				}
			}
		}

		public static bool PlayersLeftInRegion( Mobile from, Region region )
		{
			bool occupied = false;

			foreach ( NetState state in NetState.Instances )
			{
				Mobile m = state.Mobile;

				if ( m != null && m != from && m.Region == region )
				{
					occupied = true;
				}
			}

			return occupied;
		}
	}
}
