/
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.Collections.Generic;
using System.Text;
using Arthea.Connections.Players;
using Arthea.Continents.Areas.Characters;
using Arthea.Creation;
using Arthea.Environment;
using Arthea.Interfaces;
using Arthea.Scripts.Enums;

namespace Arthea.Scripts
{
    /// <summary>
    /// Implementation of a script list.
    /// </summary>
    public class ScriptList : List<Script>, CustomEditType
    {
        #region [rgn] Methods (3)

        // [rgn] Public Methods (3)

        /// <summary>
        /// Adds the specified script.
        /// </summary>
        /// <param name="script">The script.</param>
        public new void Add(Script script)
        {
            base.Add(script);
            if (script.Code != null)
            {
                script.Code.Attach();
            }
        }

        /// <summary>
        /// Handles the trigger.
        /// </summary>
        /// <param name="trig">The trigger.</param>
        /// <param name="from">The object with the script.</param>
        /// <param name="phrase">The phrase.</param>
        /// <param name="ch">The character who triggered the script.</param>
        /// <param name="arg1">The arg1.</param>
        /// <param name="arg2">The arg2.</param>
        public void HandleTrigger(TriggerType trig, Scriptable from, string phrase, Character ch, object arg1,
                                  object arg2)
        {
            foreach (Script script in this)
            {
                if (script.Type == trig && phrase.Contains(phrase))
                {
                    script.Interpret(from, ch, arg1, arg2);
                }
            }
        }

        /// <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()
        {
            if (Count == 0)
                return "none";

            StringBuilder buf = new StringBuilder();

            buf.AppendLine();
            buf.AppendLine("     Type     Id   Trigger");
            buf.AppendLine("----------------------------------");
            for (int i = 0; i < Count; i++)
            {
                buf.AppendFormat("{0,3}) ", i + 1);
                buf.AppendFormat("{0,-8} {1,-4} {2}",
                                 this[i].Type, this[i].Code.Id, this[i].Trigger);
            }
            return buf.ToString();
        }

        #endregion [rgn]

        #region CustomEditType Members

        /// <summary>
        /// Allows custom editing of this type by player.
        /// </summary>
        /// <param name="player">The player.</param>
        /// <param name="editor">The editor values.</param>
        /// <param name="argument">The argument.</param>
        public void CustomEdit(Player player, OlcField editor, String argument)
        {
            String arg = argument.FirstArg();

            switch (arg)
            {
                case "create":
                    Scriptable from;

                    if (!(editor.BaseObj is Scriptable) || (from = (editor.BaseObj as Scriptable)) == null)
                    {
                        player.WriteLine("You cannot create scripts for a {0}.", editor.Obj.GetType().Name.ToLower());
                        return;
                    }
                    ScriptCode code = new ScriptCode();
                    code.Id = ScriptCode.GetNextId();
                    Script script = from.CreateScript(code, argument);
                    Add(script);
                    player.Connection.Edit(script);
                    return;

                case "delete":
                    player.WriteLine("Function not implemented yet.");
                    return;

                case "add":

                    if (!(editor.BaseObj is Scriptable) || (from = (editor.BaseObj as Scriptable)) == null)
                    {
                        player.WriteLine("You cannot create scripts for a {0}.", editor.Obj.GetType().Name.ToLower());
                        return;
                    }

                    uint scriptId;

                    arg = argument.FirstArg();

                    if (!uint.TryParse(arg, out scriptId))
                    {
                        player.WriteLine("Invalid script code id.");
                        return;
                    }

                    code = Lists.Scripts[scriptId];
                    if (code == null)
                    {
                        player.WriteLine("No such script code");
                        return;
                    }

                    script = from.CreateScript(code, argument);

                    Add(script);

                    player.WriteLine("Script code added.");
                    return;

                case "remove":
                    int num;

                    if (!int.TryParse(argument, out num))
                    {
                        player.WriteLine("Delete which script? (provide a number)");
                        return;
                    }

                    if (num < 1 || num > Count)
                    {
                        player.WriteLine("No such script.");
                        return;
                    }

                    RemoveAt(num - 1);

                    player.WriteLine("Script removed.");
                    return;
                default:
                    player.WriteLine("Syntax: {0} create|delete|add|remove", editor.Name);
                    return;
            }
        }

        #endregion
    }
}