/
etc/
lib/
src/Abilities/
src/Abilities/Skills/
src/Abilities/Spells/
src/Abilities/Spells/Enums/
src/Affects/
src/ArtheaConsole/
src/ArtheaConsole/Properties/
src/ArtheaGUI/Properties/
src/Clans/Enums/
src/Commands/Communication/
src/Commands/ItemCommands/
src/Connections/
src/Connections/Colors/
src/Connections/Enums/
src/Connections/Players/
src/Connections/Players/Enums/
src/Continents/
src/Continents/Areas/
src/Continents/Areas/Characters/
src/Continents/Areas/Characters/Enums/
src/Continents/Areas/Items/
src/Continents/Areas/Items/Enums/
src/Continents/Areas/Rooms/
src/Continents/Areas/Rooms/Enums/
src/Continents/Areas/Rooms/Exits/
src/Creation/
src/Creation/Attributes/
src/Creation/Interfaces/
src/Database/
src/Database/Interfaces/
src/Environment/
src/Properties/
src/Scripts/Enums/
src/Scripts/Interfaces/
#region Arthea License

/***********************************************************************
*  Arthea MUD by R. Jennings (2007)      http://arthea.googlecode.com/ *
*  By using this code you comply with the Artistic and GPLv2 Licenses. *
***********************************************************************/

#endregion

using System.Text;
using System.Xml.Serialization;
using Arthea.Clans;
using Arthea.Commands.Admin;
using Arthea.Connections.Players;
using Arthea.Continents.Areas.Characters;
using Arthea.Continents.Areas.Items;
using Arthea.Continents.Areas.Rooms.Enums;
using Arthea.Creation.Attributes;
using Arthea.Database.Interfaces;
using Arthea.Environment;
using Arthea.Interfaces;
using Arthea.Scripts;

namespace Arthea.Continents.Areas.Rooms
{
    /// <summary>
    /// Implements a room
    /// </summary>
    public class Room : Scriptable, Indexed
    {
        #region [rgn] Fields (9)

        [EditShow] private readonly CharList characters = new CharList();
        [EditShow] private readonly ItemList items = new ItemList();
        [EditShow] private Area area;
        private string description;
        private ExitList exits = new ExitList();
        [EditShow] private uint id;
        private string name;
        private ScriptList scripts = new ScriptList();
        private RoomType type;

        #endregion [rgn]

        #region [rgn] Constructors (2)

        /// <summary>
        /// Initializes a new instance of the <see cref="Room"/> class.
        /// </summary>
        /// <param name="area">The area.</param>
        /// <param name="Id">The id.</param>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        public Room(Area area, uint Id, string name, string description)
        {
            this.area = area;
            id = Id;
            this.name = name;
            this.description = description;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Room"/> class.
        /// </summary>
        public Room()
        {
        }

        #endregion [rgn]

        #region [rgn] Properties (10)

        /// <summary>
        /// Gets or sets the area.
        /// </summary>
        /// <value>The area.</value>
        [XmlIgnore]
        public Area Area
        {
            get { return area; }
            set { area = value; }
        }

        /// <summary>
        /// Gets the characters.
        /// </summary>
        /// <value>The characters.</value>
        [XmlIgnore]
        public CharList Characters
        {
            get { return characters; }
        }

        /// <summary>
        /// Gets the clan.
        /// </summary>
        /// <value>The clan.</value>
        public Clan Clan
        {
            get { return area != null ? area.Clan : null; }
        }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        /// <summary>
        /// Gets or sets the exits.
        /// </summary>
        /// <value>The exits.</value>
        public ExitList Exits
        {
            get { return exits; }
            set { exits = value; }
        }

        /// <summary>
        /// Gets the items.
        /// </summary>
        /// <value>The items.</value>
        [XmlIgnore]
        public ItemList Items
        {
            get { return items; }
        }

        /// <summary>
        /// Gets or sets the scripts.
        /// </summary>
        /// <value>The scripts.</value>
        public ScriptList Scripts
        {
            get { return scripts; }
            set { scripts = value; }
        }

        /// <summary>
        /// Gets or sets the room type.
        /// </summary>
        /// <value>The room type.</value>
        public RoomType Type
        {
            get { return type; }
            set { type = value; }
        }

        #region Indexed Members

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public uint Id
        {
            get { return id; }
            set { id = value; }
        }

        #endregion

        #region Scriptable Members

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        #endregion

        #endregion [rgn]

        #region [rgn] Methods (8)

        // [rgn] Public Methods (7)

        /// <summary>
        /// Attaches this instance to applicable lists.
        /// </summary>
        public void Attach()
        {
            if (area != null)
                area.Rooms.Add(this);
            Lists.Rooms.Add(this);
        }

        /// <summary>
        /// Creates a room for editing by a player.
        /// </summary>
        /// <param name="player">The player.</param>
        /// <param name="argument">The argument.</param>
        public static void Create(Player player, String argument)
        {
            uint id;
            String arg = argument.FirstArg();
            Area areaToCreate;

            if ((areaToCreate = GetRoomArea(arg, player, out id)) == null)
            {
                return;
            }

            if (id == 0)
            {
                player.WriteLine("No more rooms available in this area.");
            }

            Room newRoom = new Room();
            newRoom.Area = areaToCreate;
            newRoom.Name = argument;
            newRoom.Id = id;
            newRoom.Type = areaToCreate.DefaultRoomType;

            newRoom.Attach();

            player.Room = newRoom;

            player.Connection.Edit(newRoom);

            player.WriteLine("Room created.");
        }

        /// <summary>
        /// Releases this instance from applicable lists.
        /// </summary>
        public void Release()
        {
            if (area != null)
                area.Rooms.Remove(this);
            Lists.Rooms.Remove(this);
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString()
        {
            return name;
        }

        /// <summary>
        /// Returns this room as the string for a player.
        /// </summary>
        /// <param name="player">The player.</param>
        /// <returns></returns>
        public string ToString(Player player)
        {
            StringBuilder buf = new StringBuilder();
            buf.Append(name);
            if (player.Powers[Powers.HolyLight])
            {
                buf.Append(" (");
                buf.Append(id);
                buf.Append(")");
            }
            return buf.ToString();
        }

        /// <summary>
        /// Writes a line.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="args">The args.</param>
        public void WriteLine(string text, params object[] args)
        {
            characters.WriteLine(text, args);
        }

        /// <summary>
        /// Writes a line.
        /// </summary>
        /// <param name="from">From.</param>
        /// <param name="text">The text.</param>
        /// <param name="args">The args.</param>
        public void WriteLine(Character from, string text, params object[] args)
        {
            foreach (Character ch in characters)
            {
                if (ch != from && ch is Player)
                {
                    (ch as Player).WriteLine(text, args);
                }
            }
        }

        // [rgn] Private Methods (1)

        /// <summary>
        /// Gets the room area.
        /// </summary>
        /// <param name="arg">The arg.</param>
        /// <param name="player">The player.</param>
        /// <param name="roomId">The room id.</param>
        /// <returns></returns>
        private static Area GetRoomArea(String arg, Player player, out uint roomId)
        {
            Area areaRet;

            if (arg.Empty())
            {
                areaRet = player.Room.Area;
            }
            else if (arg.IsNumber())
            {
                if (!uint.TryParse(arg, out roomId))
                {
                    player.WriteLine("That is not a valid id.");
                    return null;
                }

                areaRet = Lists.Areas.FindById(roomId);

                if (areaRet.Rooms[roomId] != null)
                {
                    player.WriteLine("There is already a room with that id.");
                    return null;
                }
                return areaRet;
            }
            else
            {
                areaRet = Lists.Areas.FindName(arg);
            }

            if (areaRet == null)
            {
                player.WriteLine("Unable to find an area.");
                roomId = 0;
                return null;
            }

            roomId = areaRet.GetNextRoomId();

            return areaRet;
        }

        #endregion [rgn]

        #region Scriptable Members

        /// <summary>
        /// Gets a short description for scriptable object
        /// </summary>
        public string ShortDescr
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// Creates a script of proper type.
        /// </summary>
        /// <returns></returns>
        public Script CreateScript(ScriptCode code, String trigger)
        {
            return new RoomScript(code, trigger);
        }

        #endregion
    }
}