using System;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Windows.Forms;

namespace MuEditor
{
    public class frmUserAdd : frmBase
    {
        private IContainer components;

        private Label label3;

        private MuEditor.TextBox txtAccount;

        private Label label1;

        private MuEditor.TextBox txtPwd;

        private CheckBox cbMD5;

        private Button btnOK;

        private Button btnClear;

        private Button btnClose;

        private GroupBox groupBox1;

        private static frmUserAdd form;

        static frmUserAdd()
        {
        }

        private frmUserAdd()
        {
            this.InitializeComponent();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtAccount.Text = "";
            this.txtPwd.Text = "";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (frmBase.Me_Conn.State == ConnectionState.Open)
            {
                string text = "SELECT memb_guid FROM MEMB_INFO WHERE memb___id = '{0}'";
                try
                {
                    if (!Validator.UserName(txtAccount.Text.Trim()))
                    {
                        Utils.WarningMessage("Error: Account names must be between 4-10 characters, and only contain numbers, letters or underscores.");
                    }
                    else if (!Validator.PassWord(txtPwd.Text.Trim()))
                    {
                        Utils.WarningMessage("Error: Passwords must be between 4-10 characters.");
                    }
                    else
                    {
                        text = string.Format(text, txtAccount.Text.Trim());
                        OleDbDataReader oleDbDataReader = new OleDbCommand(text, frmBase.Me_Conn).ExecuteReader();
                        if (oleDbDataReader.Read())
                        {
                            Utils.WarningMessage("Error: The account [" + txtAccount.Text.Trim() + "] already exists.");
                            oleDbDataReader.Close();
                        }
                        else
                        {
                            oleDbDataReader.Close();
                            object[] args = new object[5]
                            {
                            txtAccount.Text.Trim(),
                            txtPwd.Text.Trim(),
                            txtAccount.Text.Trim() + "@email.pl",
                            0,
                            1
                            };
                            text = string.Format("INSERT INTO dbo.MEMB_INFO(memb___id, memb__pwd, memb_name, mail_addr, fpas_ques, fpas_answ, sno__numb, post_code, bloc_code, ctl1_code) VALUES ('{0}', '{1}', 'EMU','{2}', 'question', 'answer', 0, '{4}', 0, 0)", args);
                            if (base.Me_ExecuteSQL(text))
                            {
                                Utils.InfoMessage("Account created.");
                            }
                            else
                            {
                                Utils.WarningMessage("Error: Account creation failed.");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Utils.WarningMessage("SQL：" + text + "\nError:" + ex.Message + "\nSource:" + ex.Source + "\nTrace:" + ex.StackTrace);
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && this.components != null)
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void frmUserAdd_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmUserAdd.form = null;
        }

        public static frmUserAdd GetInstance()
        {
            if (frmUserAdd.form == null)
            {
                frmUserAdd.form = new frmUserAdd();
            }
            return frmUserAdd.form;
        }

        private void InitializeComponent()
        {
            this.label3 = new System.Windows.Forms.Label();
            this.txtAccount = new MuEditor.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.txtPwd = new MuEditor.TextBox();
            this.cbMD5 = new System.Windows.Forms.CheckBox();
            this.btnOK = new System.Windows.Forms.Button();
            this.btnClear = new System.Windows.Forms.Button();
            this.btnClose = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.label3.Location = new System.Drawing.Point(7, 21);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(53, 13);
            this.label3.TabIndex = 1;
            this.label3.Text = "Account: ";
            // 
            // txtAccount
            // 
            this.txtAccount.IsOnlyNumber = false;
            this.txtAccount.Location = new System.Drawing.Point(67, 16);
            this.txtAccount.MaxLength = 15;
            this.txtAccount.Name = "txtAccount";
            this.txtAccount.Size = new System.Drawing.Size(108, 20);
            this.txtAccount.TabIndex = 2;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.label1.Location = new System.Drawing.Point(7, 50);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(59, 13);
            this.label1.TabIndex = 3;
            this.label1.Text = "Password: ";
            // 
            // txtPwd
            // 
            this.txtPwd.IsOnlyNumber = false;
            this.txtPwd.Location = new System.Drawing.Point(67, 46);
            this.txtPwd.MaxLength = 10;
            this.txtPwd.Name = "txtPwd";
            this.txtPwd.Size = new System.Drawing.Size(108, 20);
            this.txtPwd.TabIndex = 4;
            // 
            // cbMD5
            // 
            this.cbMD5.AutoSize = true;
            this.cbMD5.Checked = true;
            this.cbMD5.CheckState = System.Windows.Forms.CheckState.Checked;
            this.cbMD5.Enabled = false;
            this.cbMD5.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.cbMD5.Location = new System.Drawing.Point(87, 83);
            this.cbMD5.Name = "cbMD5";
            this.cbMD5.Size = new System.Drawing.Size(89, 17);
            this.cbMD5.TabIndex = 9;
            this.cbMD5.Text = "Support MD5";
            this.cbMD5.UseVisualStyleBackColor = true;
            // 
            // btnOK
            // 
            this.btnOK.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.btnOK.Location = new System.Drawing.Point(6, 83);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(74, 38);
            this.btnOK.TabIndex = 10;
            this.btnOK.Text = "Create";
            this.btnOK.UseVisualStyleBackColor = true;
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            // 
            // btnClear
            // 
            this.btnClear.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.btnClear.Location = new System.Drawing.Point(6, 127);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(77, 23);
            this.btnClear.TabIndex = 11;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            // 
            // btnClose
            // 
            this.btnClose.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.btnClose.Location = new System.Drawing.Point(89, 127);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(86, 23);
            this.btnClose.TabIndex = 12;
            this.btnClose.Text = "Close";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.BackColor = System.Drawing.Color.Silver;
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.btnClose);
            this.groupBox1.Controls.Add(this.txtAccount);
            this.groupBox1.Controls.Add(this.btnClear);
            this.groupBox1.Controls.Add(this.txtPwd);
            this.groupBox1.Controls.Add(this.cbMD5);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.btnOK);
            this.groupBox1.Location = new System.Drawing.Point(4, 4);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(182, 164);
            this.groupBox1.TabIndex = 67;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Account Details";
            // 
            // frmUserAdd
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.Silver;
            this.ClientSize = new System.Drawing.Size(192, 171);
            this.Controls.Add(this.groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmUserAdd";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Create New Account";
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.frmUserAdd_FormClosed);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }
    }
}