#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.Collections.Generic;
using System.Text;
using Arthea.Connections.Players;
using Arthea.Continents.Areas.Characters;
using Arthea.Continents.Areas.Items;
using Arthea.Continents.Areas.Rooms;
using Arthea.Creation;
using Arthea.Environment;
using Arthea.Interfaces;
namespace Arthea.Continents.Areas.Resets
{
    /// <summary>
    /// Implementation of a reset list.
    /// </summary>
    public class ResetList : List<Reset>, CustomEditType
    {
        #region [rgn] Methods (3)
        // [rgn] Public Methods (3)
        /// <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)
        {
            player.WriteLine("Function not implemented yet.");
        }
        /// <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.AppendLine();
            buf.AppendLine("-----------------------------");
            foreach (Reset reset in this)
            {
                buf.AppendLine(reset.ToString());
            }
            return buf.ToString();
        }
        /// <summary>
        /// Updates this instance.
        /// </summary>
        public void Update()
        {
            foreach (Reset reset in this)
            {
                Room room = Lists.Rooms[reset.Room];
                if (room == null)
                {
                    Log.Fatal("Bad reset for non existant room {0}.", reset.Room);
                    System.Environment.Exit(1);
                }
                if (reset is ItemReset)
                {
                    ItemReset ireset = reset as ItemReset;
                    ItemIndex item = Lists.ItemIndexes[ireset.Item];
                    if (item == null)
                    {
                        Log.Fatal("Bad reset for non existant item {0}.", ireset.Item);
                        System.Environment.Exit(1);
                    }
                    if (
                        room.Items.FindAll(delegate(Item obj) { return item.Id == obj.Id; }).Count >=
                        ireset.Count)
                        continue;
                    Item instance = new Item(item);
                    instance.Attach();
                    instance.Room = room;
                }
                else if (reset is CharReset)
                {
                    CharReset areset = reset as CharReset;
                    CharIndex ch = Lists.CharIndexes[areset.Character];
                    if (ch == null)
                    {
                        Log.Fatal("Bad reset for non existant character {0}.", areset.Character);
                        System.Environment.Exit(1);
                    }
                    if (room.Characters.FindAll(
                            delegate(Character a) { return ch.Id == a.Id; }).Count >=
                        areset.Count)
                        continue;
                    Character instance = new Character(ch);
                    instance.Attach();
                    instance.Room = room;
                }
            }
        }
        #endregion [rgn]
    }
}