using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace DarkWynter.App
{
    public partial class DataBaseControl : UserControl
    {

        private System.Data.Odbc.OdbcConnection OdbcCon;
        private System.Data.Odbc.OdbcCommand OdbcCom;
        private System.Data.Odbc.OdbcDataReader OdbcDR;
        private string ConStr;
        private Form frmAbout;


        public DataBaseControl()
        {
            InitializeComponent();
            this.txtIP.Text = "localhost";
            this.txtPort.Text = "3306";
            this.txtDatabase.Text = "tesseract";
            this.txtUsername.Text = "darkwynter";
            this.txtPassword.Text = "wynter1";

        }


		private void btnConnect_Click(object sender, EventArgs e)
		{
			ConStr = "DRIVER={MySQL ODBC 3.51 Driver}" + 
                ";SERVER=" + txtIP.Text + 
                ";PORT=" + txtPort.Text + 
                ";DATABASE=" + txtDatabase.Text + 
                ";UID=" + txtUsername.Text + 
                ";PWD=" + txtPassword.Text + 
                ";OPTION=3";
			
            OdbcCon = new System.Data.Odbc.OdbcConnection(ConStr);
			btnListTables.Enabled = true;

			try
			{
				txtLog.AppendText("Openning connection...\r\n");
				if (OdbcCon.State == ConnectionState.Closed)
				{
					OdbcCon.Open();
				}
				txtLog.AppendText("Connection opened\r\n");
			}
			catch (System.Data.Odbc.OdbcException Ex)
			{
				txtLog.AppendText(Ex.Message + "\r\n");
				MessageBox.Show("Could not access the database.\r\nPlease make sure you completed the fields with the correct information and try again.\r\n\r\nMore details:\r\n" + Ex.Message, "Database connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void btnListTables_Click(object sender, EventArgs e)
		{
			if (OdbcCon.State == ConnectionState.Open)
			{
				OdbcCom = new System.Data.Odbc.OdbcCommand("SHOW TABLES", OdbcCon);
				OdbcDR = OdbcCom.ExecuteReader();
				txtLog.AppendText("Tables inside " + txtDatabase.Text + ":\r\n");
				while (OdbcDR.Read())
				{
					txtLog.AppendText(">> " + OdbcDR[0] + "\r\n");
				}
			}
		}

		private void btnDisconnect_Click(object sender, EventArgs e)
		{
			if (OdbcCon.State == ConnectionState.Open)
			{
				OdbcCon.Close();
			}
		}

    
    }
}