/
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;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Arthea.Connections.Players;
using Arthea.Continents.Areas.Rooms.Enums;
using Arthea.Continents.Areas.Rooms.Exits;
using Arthea.Creation;
using Arthea.Environment;
using Arthea.Interfaces;

namespace Arthea.Continents.Areas.Rooms
{
    /// <summary>
    /// Implementation of a exit list.
    /// </summary>
    public class ExitList : Dictionary<Direction, Exit>, IXmlSerializable, CustomEditType
    {
        #region [rgn] Properties (1)

        /// <summary>
        /// Gets the <see cref="Exit"/> with the specified dir.
        /// </summary>
        /// <value></value>
        public new Exit this[Direction dir]
        {
            get
            {
                try
                {
                    return base[dir];
                }
                catch
                {
                    return null;
                }
            }
        }

        #endregion [rgn]

        #region [rgn] Methods (6)

        // [rgn] Public Methods (6)

        /// <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)
        {
            String arg = argument.FirstArg();

            Direction dir;

            try
            {
                dir = (Direction) Enum.Parse(typeof (Direction), arg.ToString(), true);
            }
            catch (Exception)
            {
                player.WriteLine("Invalid direction.");
                return;
            }

            Exit exit = this[dir];

            if (exit == null)
            {
                if (argument.StartsWith("create"))
                {
                    uint id;

                    argument.FirstArg();

                    if (!argument)
                    {
                        player.WriteLine("Add an exit to which room?");
                        return;
                    }

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

                    Room toRoom = Lists.Rooms[id];

                    if (toRoom == null)
                    {
                        player.WriteLine("Room {0} does not exist.", id);
                        return;
                    }

                    if (toRoom.Exits[Exit.Reverse(dir)] != null)
                    {
                        player.WriteLine("Room {0} already has a {1} exit.", id,
                                         Exit.Reverse(dir));
                        return;
                    }

                    Add(dir, new Exit(toRoom, dir));

                    player.WriteLine("Exit set.");
                    return;
                }

                player.WriteLine("There is no exit in that direction.");
                return;
            }

            if (argument == "delete")
            {
                if (exit.ToRoom.Value != null)
                {
                    Exit reverseDir = exit.ToRoom.Value.Exits[Exit.Reverse(dir)];
                    if (reverseDir != null && reverseDir.ToRoom.Value == exit.ToRoom.Value)
                        exit.ToRoom.Value.Exits.Remove(reverseDir);
                }
                Remove(exit);
                player.WriteLine("Exit deleted.");
                return;
            }

            if (argument == "unlink")
            {
                Remove(exit);
                player.WriteLine("Exit unlinked.");
            }

            Olc oldEdit = player.Connection.Editing;

            player.Connection.Edit(exit);

            if (!player.Connection.Editing.Edit(argument))
                player.WriteLine("Syntax: {0} create, <dir> <delete|unlink>",
                                 editer.Name);

            player.Connection.Editing = oldEdit;
        }

        /// <summary>
        /// Adds the specified exit.
        /// </summary>
        /// <param name="exit">The exit.</param>
        public void Add(Exit exit)
        {
            base.Add(exit.Direction, exit);
        }

        /// <summary>
        /// Adds the specified dir.
        /// </summary>
        /// <param name="dir">The dir.</param>
        /// <param name="exit">The exit.</param>
        public new void Add(Direction dir, Exit exit)
        {
            base.Add(exit.Direction, exit);
        }

        /// <summary>
        /// Finds the exit.
        /// </summary>
        /// <param name="argument">The argument.</param>
        /// <returns>an exit</returns>
        public Exit FindExit(String argument)
        {
            if (!argument) return null;

            foreach (Exit exit in Values)
            {
                if (!string.IsNullOrEmpty(exit.Keyword) &&
                    argument.IsPrefixOf(exit.Keyword))
                {
                    return exit;
                }
            }
            return null;
        }

        /// <summary>
        /// Removes the specified exit.
        /// </summary>
        /// <param name="exit">The exit.</param>
        /// <returns>true if remove succeeded</returns>
        public bool Remove(Exit exit)
        {
            return Remove(exit.Direction);
        }

        /// <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();
            bool found = false;

            buf.AppendLine();

            buf.AppendLine("-----------------------------");

            foreach (Exit exit in Values)
            {
                if (exit != null)
                {
                    buf.AppendLine(exit.ToString());
                    found = true;
                }
            }

            if (!found)
                buf.AppendLine("none");

            return buf.ToString();
        }

        #endregion [rgn]

        #region IXmlSerializable Members

        /// <summary>
        /// Converts an object into its XML representation.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Xml.XmlWriter"></see> stream to which the object is serialized.</param>
        public void WriteXml(XmlWriter writer)
        {
            if (Count == 0)
                return;

            foreach (Exit exit in Values)
            {
                writer.WriteStartElement("Direction");
                writer.WriteAttributeString("value", exit.Direction.ToString());
                if (exit.ToRoom != null)
                    writer.WriteAttributeString("to", exit.ToRoom.Id.ToString());
                writer.WriteEndElement();
            }
        }

        /// <summary>
        /// Generates an object from its XML representation.
        /// </summary>
        /// <param name="reader">The <see cref="T:System.Xml.XmlReader"></see> stream from which the object is deserialized.</param>
        public void ReadXml(XmlReader reader)
        {
            if (reader.IsEmptyElement)
                return;

            reader.ReadStartElement();

            while (reader.IsStartElement())
            {
                reader.ReadStartElement();
                reader.ReadEndElement();

                Direction dir = (Direction) Enum.Parse(typeof (Direction),
                                                       reader.GetAttribute("direction"));
                uint id = uint.Parse(reader.GetAttribute("to"));

                Add(new Exit(id, dir));
            }

            reader.ReadEndElement();
        }

        /// <summary>
        /// This property is reserved, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"></see> to the class instead.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Xml.Schema.XmlSchema"></see> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"></see> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"></see> method.
        /// </returns>
        public XmlSchema GetSchema()
        {
            return (null);
        }

        #endregion
    }
}