#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
#region Includes
using System;
using System.IO;
using System.Xml.Serialization;
using Arthea.Connections.Players;
using Arthea.Continents.Areas;
using Arthea.Continents.Areas.Rooms.Enums;
using Arthea.Creation.Attributes;
using Arthea.Database.Interfaces;
using Arthea.Environment;
#endregion
namespace Arthea.Continents
{
    /// <summary>
    /// Implements a continent
    /// </summary>
    public class Continent : Indexed
    {
        #region [rgn] Fields (4)
        [EditIgnore] private AreaList areas = new AreaList();
        private RoomType defaultAreaType = RoomType.Inside;
        private string fileName;
        private string name;
        #endregion [rgn]
        #region [rgn] Constructors (2)
        /// <summary>
        /// Initializes a new instance of the <see cref="Continent"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        public Continent(string name)
        {
            this.name = name;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Continent"/> class.
        /// </summary>
        public Continent()
        {
        }
        #endregion [rgn]
        #region [rgn] Properties (5)
        /// <summary>
        /// Gets or sets the areas.
        /// </summary>
        /// <value>The areas.</value>
        [XmlIgnore]
        public AreaList Areas
        {
            get { return areas; }
            set { areas = value; }
        }
        /// <summary>
        /// Gets or sets the default area type.
        /// </summary>
        /// <value>The default area type.</value>
        public RoomType DefaultAreaType
        {
            get { return defaultAreaType; }
            set { defaultAreaType = value; }
        }
        /// <summary>
        /// Gets or sets the name of the file.
        /// </summary>
        /// <value>The name of the file.</value>
        [XmlIgnore]
        public string FileName
        {
            get { return Paths.ContinentDir + fileName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    throw new Exception("File name cannot be empty.");
                if (Persistance.XmlFileExists(FileName)
                    && fileName != value)
                {
                    File.Move(Persistance.XmlFileName(FileName),
                              Persistance.XmlFileName(Paths.ContinentDir + value));
                }
                fileName = value;
            }
        }
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        /// <summary>
        /// Gets the id.
        /// </summary>
        /// <value>The id.</value>
        public uint Id
        {
            get { return (uint) name.GetHashCode(); }
        }
        #endregion [rgn]
        #region [rgn] Methods (5)
        // [rgn] Public Methods (5)
        /// <summary>
        /// Attaches this instance.
        /// </summary>
        public void Attach()
        {
            Lists.Continents.Add(this);
        }
        /// <summary>
        /// Creates a continent 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)
        {
            Continent continent = new Continent();
            continent.FileName = string.Format("continent{0}", Lists.Continents.Count);
            if (!argument)
            {
                continent.Name = continent.FileName;
            }
            else
            {
                continent.Name = argument;
            }
            continent.Attach();
            player.WriteLine("Continent created.");
            player.Connection.Edit(continent);
        }
        /// <summary>
        /// Releases this instance.
        /// </summary>
        public void Release()
        {
            Lists.Continents.Remove(this);
        }
        /// <summary>
        /// Saves this instance.
        /// </summary>
        public void Save()
        {
            Persistance.Save(FileName, 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;
        }
        #endregion [rgn]
    }
}