using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ISIM
{
    public class ShowDialogs
    {
        public class InputDialog
        {
            /// <summary>
            /// Displays a dialog with a prompt and textbox where the user can enter information
            /// </summary>
            /// <param name="title">Dialog title</param>
            /// <param name="promptText">Dialog prompt</param>
            /// <param name="value">Sets the initial value and returns the result</param>
            /// <returns>Dialog result</returns>
            public static DialogResult Show(string title, string promptText, ref string value)
            {
                Form form = new Form();
                Label label = new Label();
                TextBox textBox = new TextBox();
                Button buttonOk = new Button();
                Button buttonCancel = new Button();

                form.Text = title;
                label.Text = promptText;
                textBox.Text = value;

                buttonOk.Text = "OK";
                buttonCancel.Text = Global.LangManager.Translate("cancel");
                buttonOk.DialogResult = DialogResult.OK;
                buttonCancel.DialogResult = DialogResult.Cancel;

                label.SetBounds(9, 20, 372, 13);
                textBox.SetBounds(12, 36, 372, 20);
                buttonOk.SetBounds(228, 72, 75, 23);
                buttonCancel.SetBounds(309, 72, 75, 23);

                label.AutoSize = true;
                textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
                buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
                buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

                form.ClientSize = new Size(396, 107);
                form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
                form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
                form.FormBorderStyle = FormBorderStyle.FixedDialog;
                form.StartPosition = FormStartPosition.CenterParent;
                form.MinimizeBox = false;
                form.MaximizeBox = false;
                form.AcceptButton = buttonOk;
                form.CancelButton = buttonCancel;

                DialogResult dialogResult = form.ShowDialog();
                value = textBox.Text;
                return dialogResult;
            }


            public static DialogResult Show(string title, string promptText, ref string value, ref string value2)
            {
                Form form = new Form();
                Label label = new Label();
                TextBox textBox = new TextBox();
                TextBox authRequest = new TextBox();
                Button buttonOk = new Button();
                Button buttonCancel = new Button();

                form.Text = title;
                label.Text = promptText;
                textBox.Text = value;
                authRequest.Text = value2;

                buttonOk.Text = "OK";
                buttonCancel.Text = Global.LangManager.Translate("cancel");
                buttonOk.DialogResult = DialogResult.OK;
                buttonCancel.DialogResult = DialogResult.Cancel;

                label.SetBounds(9, 20, 372, 13);
                textBox.SetBounds(12, 36, 372, 20);
                authRequest.SetBounds(12, 60, 372, 20);
                buttonOk.SetBounds(228, 85, 75, 23);
                buttonCancel.SetBounds(309, 85, 75, 23);

                label.AutoSize = true;
                textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
                authRequest.Anchor = textBox.Anchor | AnchorStyles.Right;
                buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
                buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

                form.ClientSize = new Size(396, 120);
                form.Controls.AddRange(new Control[] { label, textBox, authRequest, buttonOk, buttonCancel });
                form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
                form.FormBorderStyle = FormBorderStyle.FixedDialog;
                form.StartPosition = FormStartPosition.CenterParent;
                form.MinimizeBox = false;
                form.MaximizeBox = false;
                form.AcceptButton = buttonOk;
                form.CancelButton = buttonCancel;

                DialogResult dialogResult = form.ShowDialog();
                value = textBox.Text;
                return dialogResult;
            }
        }

        public class MessageDialog : Form
        {
            private string id;

            private Dictionary<MessageBoxIcon, Icon> icons;

            public MessageDialog(string id)
            {
                icons = new Dictionary<MessageBoxIcon, Icon>();
                icons.Add(MessageBoxIcon.Error, SystemIcons.Error);
                icons.Add(MessageBoxIcon.Information, SystemIcons.Information);
                icons.Add(MessageBoxIcon.None, null);
                icons.Add(MessageBoxIcon.Question, SystemIcons.Question);
                icons.Add(MessageBoxIcon.Warning, SystemIcons.Warning);

                this.id = id;
            }

            public void Show(string text, string title, MessageBoxIcon icon)
            {
                Label label = new Label();
                PictureBox pic = new PictureBox();
                Button button = new Button();

                pic.Size = new Size(0, 0);

                if (icons[icon] != null)
                {
                    pic.Image = icons[icon].ToBitmap();
                    pic.Size = new Size(pic.Image.Width, pic.Image.Height);
                }

                label.Text = text;
                label.Font = new Font(FontFamily.GenericSansSerif, 10f);
                label.MaximumSize = new Size(400 - pic.Size.Width - 20, 0);
                label.AutoSize = true;

                int textHeight = 0;
                using (Graphics g = this.CreateGraphics())
                {
                    textHeight = (int)g.MeasureString(label.Text, label.Font, label.MaximumSize.Width).Height;
                }

                button.Text = "OK";
                button.Click += button_Click;

                this.Text = title;
                this.ClientSize = new Size(400, textHeight + button.Height + 50);
                this.FormBorderStyle = FormBorderStyle.FixedDialog;
                this.StartPosition = FormStartPosition.CenterScreen;
                this.ControlBox = false;
                this.AcceptButton = button;
                this.BackColor = Color.White;
                this.FormClosing += form_FormClosing;


                label.SetBounds(pic.Width + 20, 20, label.Width, label.Height);
                pic.SetBounds(10, this.ClientSize.Height / 3, pic.Width, pic.Height);
                button.SetBounds(this.ClientSize.Width - button.Width - 10, this.ClientSize.Height - button.Height - 10, button.Width, button.Height);

                this.Controls.AddRange(new Control[] { label, pic, button });

                if (Global.dnd)
                    this.WindowState = FormWindowState.Minimized;

                this.Show();

                Global.OpenForms.messageDialogList.Add(id, this);
            }

            void form_FormClosing(object sender, FormClosingEventArgs e)
            {
                Global.OpenForms.messageDialogList.Remove(id);
            }

            void button_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
}
