/
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.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Arthea;
using Arthea.Commands.Admin;
using Arthea.Environment;

namespace ArtheaGUI
{
    public partial class MainForm : Form
    {
        #region [rgn] Fields (3)

        private static readonly object GuiLock = new object();
        private readonly bool reboot = false;
        private Thread serverThread;

        #endregion [rgn]

        #region [rgn] Constructors (1)

        /// <summary>
        /// Constructs the main form.
        /// </summary>
        /// <param name="reboot">true if rebooting</param>
        public MainForm(bool reboot)
        {
            InitializeComponent();
            this.reboot = reboot;
            Console.SetOut(new TextBoxWriter(txtOutput));
        }

        #endregion [rgn]

        #region [rgn] Methods (6)

        // [rgn] Private Methods (6)

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StringBuilder buf = new StringBuilder();
            buf.Append(Server.Instance.Version);
            buf.AppendFormat(" (GUI v{0}.{1})", Environment.Version.Major,
                             Environment.Version.Minor);
            buf.AppendLine();
            buf.AppendLine(Server.Instance.About);

            MessageBox.Show(buf.ToString(), "About", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void lockToolStripMenuItem_Click(object sender, EventArgs e)
        {
            switch (lockToolStripMenuItem.Text)
            {
                case "&Lock":
                    Globals.LockDown = true;
                    lockToolStripMenuItem.Text = "Un&lock";
                    break;

                case "Un&lock":
                    Globals.LockDown = false;
                    lockToolStripMenuItem.Text = "&Lock";
                    break;
            }
        }

        private void portToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PortsDialog dlg = new PortsDialog();

            dlg.ShowDialog(this);

            if (dlg.DialogResult == DialogResult.OK)
            {
                lock (GuiLock)
                {
                    Globals.PortNumber = dlg.Port;
                    RebootRecovery.Port = dlg.RecoveryPort;
                }
            }

            dlg.Dispose();
        }

        private void runtimeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            switch (runtimeToolStripMenuItem.Text)
            {
                case "&Start":
                case "Re&start":
                    serverThread = new Thread(StartServer);
                    serverThread.Start();
                    runtimeToolStripMenuItem.Text = "&Stop";
                    lockToolStripMenuItem.Visible = true;
                    break;

                case "&Stop":
                    serverThread.Abort("...server stopped.");
                    runtimeToolStripMenuItem.Text = "Re&start";
                    lockToolStripMenuItem.Visible = false;
                    GC.Collect();
                    break;
            }
        }

        private void StartServer()
        {
            Server.Instance.Start(reboot);
        }

        #endregion [rgn]
    }

    /// <summary>
    /// Implements a text box writer
    /// </summary>
    public class TextBoxWriter : TextWriter
    {
        #region [rgn] Fields (1)

        private readonly RichTextBox textBox;

        #endregion [rgn]

        #region [rgn] Constructors (1)

        /// <summary>
        /// Constructs a text box writer
        /// </summary>
        /// <param name="textBox"></param>
        public TextBoxWriter(RichTextBox textBox)
        {
            this.textBox = textBox;
        }

        #endregion [rgn]

        #region [rgn] Properties (1)

        /// <summary>
        /// Gets the encoding for this textbox
        /// </summary>
        public override Encoding Encoding
        {
            get { return Encoding.ASCII; }
        }

        #endregion [rgn]

        #region [rgn] Methods (17)

        // [rgn] Public Methods (16)

        /// <summary>
        /// Write a string to the text box
        /// </summary>
        /// <param name="value"></param>
        public override void Write(object value)
        {
            AppendText(value);
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        /// <param name="value">what to append</param>
        public override void Write(bool value)
        {
            AppendText(value);
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        /// <param name="value">what to append</param>
        public override void Write(char value)
        {
            AppendText(value);
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        public override void Write(char[] buffer)
        {
            AppendText(new string(buffer));
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        public override void Write(char[] buffer, int index, int count)
        {
            AppendText("error: unimplemented method");
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        /// <param name="value">what to append</param>
        public override void Write(decimal value)
        {
            AppendText(value);
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        /// <param name="value">what to append</param>
        public override void Write(double value)
        {
            AppendText(value);
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        /// <param name="value">what to append</param>
        public override void Write(float value)
        {
            AppendText(value);
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        /// <param name="value">what to append</param>
        public override void Write(int value)
        {
            AppendText(value);
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        /// <param name="value">what to append</param>
        public override void Write(long value)
        {
            AppendText(value);
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        public override void Write(string format, object arg0)
        {
            AppendText(string.Format(format, arg0));
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        public override void Write(string format, object arg0, object arg1)
        {
            AppendText(string.Format(format, arg0, arg1));
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        public override void Write(string format, object arg0, object arg1, object arg2)
        {
            AppendText(string.Format(format, arg0, arg1, arg2));
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        public override void Write(string format, params object[] arg)
        {
            AppendText(string.Format(format, arg));
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        /// <param name="value">what to append</param>
        public override void WriteLine(object value)
        {
            AppendText(value);
            AppendText(Environment.NewLine);
        }

        /// <summary>
        /// Write a value to the text box
        /// </summary>
        /// <param name="value">what to append</param>
        public override void WriteLine(string value)
        {
            AppendText(value);
            AppendText(Environment.NewLine);
        }

        // [rgn] Private Methods (1)

        private void AppendText(object text)
        {
            if (textBox.InvokeRequired)
            {
                OutputCallBack c = AppendText;
                textBox.FindForm().Invoke(c, new object[] {text});
            }
            else
            {
                textBox.AppendText(text.ToString());
            }
        }

        #endregion [rgn]

        #region Nested type: OutputCallBack

        private delegate void OutputCallBack(object text);

        #endregion
    }
}