/
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.Connections.Players;
using Arthea.Continents.Areas.Rooms.Enums;
using Arthea.Creation;
using Arthea.Environment;
using Arthea.Interfaces;

namespace Arthea.Continents.Areas.Rooms.Exits
{
    /// <summary>
    /// Implements a room exit
    /// </summary>
    public class Exit
    {
        #region [rgn] Fields (5)

        private Direction direction;
        private ExitFlags flags = new ExitFlags();
        private uint key;
        private string keyword;
        private ToRoom toRoom;

        #endregion [rgn]

        #region [rgn] Constructors (3)

        /// <summary>
        /// Initializes a new instance of the <see cref="Exit"/> class.
        /// </summary>
        /// <param name="toRoom">To room.</param>
        /// <param name="dir">The dir.</param>
        public Exit(Room toRoom, Direction dir)
        {
            this.toRoom = new ToRoom(toRoom);
            direction = dir;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Exit"/> class.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="dir">The dir.</param>
        public Exit(uint id, Direction dir)
        {
            ToRoom = new ToRoom(id);
            direction = dir;
        }

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

        #endregion [rgn]

        #region [rgn] Properties (6)

        /// <summary>
        /// Gets or sets the direction.
        /// </summary>
        /// <value>The direction.</value>
        [XmlAttribute]
        public Direction Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        /// <value>The flags.</value>
        public ExitFlags Flags
        {
            get { return flags; }
            set { flags = value; }
        }

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

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

        /// <summary>
        /// Gets or sets to room.
        /// </summary>
        /// <value>To room.</value>
        [XmlIgnore]
        public ToRoom ToRoom
        {
            get { return toRoom; }
            set { toRoom = value; }
        }

        /// <summary>
        /// Gets or sets the toRoom id.
        /// </summary>
        /// <value>The ToRoom id.</value>
        [XmlElement("ToRoom")]
        public uint XmlToRoom
        {
            get { return toRoom.Id; }
            set { toRoom = new ToRoom(value); }
        }

        #endregion [rgn]

        #region [rgn] Methods (2)

        // [rgn] Public Methods (2)

        /// <summary>
        /// Reverses the specified dir.
        /// </summary>
        /// <param name="dir">The dir.</param>
        /// <returns></returns>
        public static Direction Reverse(Direction dir)
        {
            switch (dir)
            {
                case Direction.North:
                    return Direction.South;
                case Direction.South:
                    return Direction.North;
                case Direction.East:
                    return Direction.West;
                case Direction.West:
                    return Direction.East;
                case Direction.Up:
                    return Direction.Down;
                case Direction.Down:
                    return Direction.Up;
                default:
                    Log.Error("Invalid direction.");
                    return 0;
            }
        }

        /// <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()
        {
            StringBuilder buf = new StringBuilder();

            buf.Append(direction.ToString());
            buf.Append(" - To Room [");
            buf.Append(toRoom.Value.ToString());
            buf.Append("] Flags [");
            buf.Append(flags.ToString());
            buf.Append("] Keyword: [");
            buf.Append(keyword);
            buf.Append("] Key: [");
            buf.Append(key.ToString());
            buf.Append("]");

            return buf.ToString();
        }

        #endregion [rgn]
    }

    /// <summary>
    /// Implements a room an exit points to.
    /// Used for loading an exit before a room exists.
    /// </summary>
    public class ToRoom : CustomEditType
    {
        #region [rgn] Fields (2)

        private uint id;
        private Room room;

        #endregion [rgn]

        #region [rgn] Constructors (2)

        /// <summary>
        /// Initializes a new instance of the <see cref="ToRoom"/> class.
        /// </summary>
        /// <param name="id">The id.</param>
        public ToRoom(uint id)
        {
            this.id = id;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ToRoom"/> class.
        /// </summary>
        /// <param name="room">The room.</param>
        public ToRoom(Room room)
        {
            id = room.Id;
            this.room = room;
        }

        #endregion [rgn]

        #region [rgn] Properties (2)

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

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

        #endregion [rgn]

        #region [rgn] Methods (1)

        // [rgn] Public Methods (1)

        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="player">The player.</param>
        /// <param name="editer">Editer information.</param>
        /// <param name="argument">The argument.</param>
        public void CustomEdit(Player player, OlcField editer, String argument)
        {
            uint toRoomId;

            if (!uint.TryParse(argument, out toRoomId))
            {
                player.WriteLine("That is not a valid room id.");
                return;
            }

            Room toRoom = Lists.Rooms[toRoomId];

            if (toRoom == null)
            {
                player.WriteLine("That room does not exist.");
                return;
            }

            room = toRoom;
            id = toRoomId;
            player.WriteLine("Exit now leads to room {0}.", toRoomId);
        }

        #endregion [rgn]
    }
}