using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management;
using Microsoft.SqlServer.Management.Common;

namespace GenericConnection
{
   public partial class ServerConnectionProperties : Form
    {
        class MyReadOnlyStyle : DataGridViewCellStyle
        {
            internal MyReadOnlyStyle()
            {
                base.BackColor = Color.Beige;
            }
        }

        MyReadOnlyStyle myReadOnlyStyle = new MyReadOnlyStyle();
        SqlConnectionInfo serverConnectionInfo;
        ServerConnection serverConnection = null;
        int makeConnectionBottomSpace = 0;
        public ServerConnectionProperties()
        {
            InitializeComponent();
            serverConnection = new ServerConnection();
      
            ConnectionProperties.Columns.Add("ConnectionProperty", "Property");
            ConnectionProperties.Columns.Add("PropertyValue", "Value");
            ConnectionProperties.CellFormatting += new DataGridViewCellFormattingEventHandler(ConnectionProperties_CellFormatting);
            ConnectionProperties.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            ConnectionProperties.Columns[0].ReadOnly = true;
            ConnectionProperties.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            ConnectionProperties.CellEndEdit += new DataGridViewCellEventHandler(ConnectionProperties_CellEndEdit);
            ConnectionProperties.CellBeginEdit += new DataGridViewCellCancelEventHandler(ConnectionProperties_CellBeginEdit);
            foreach (MemberInfo mi in typeof(ServerConnection ).GetMembers())
            {
                if((mi.MemberType==MemberTypes.Property) || (mi.MemberType==MemberTypes.Field))
                {
                    bool ReadOnly = true;
                    if (mi.MemberType==MemberTypes.Property)
                    {
                        PropertyInfo pi = mi as PropertyInfo;
                        ReadOnly = !pi.CanWrite;
                        if (!pi.PropertyType.IsValueType && pi.PropertyType != typeof(string))
                        {
                            continue;
                        }
                    }
                    if (mi.MemberType == MemberTypes.Field)
                    {
                        FieldInfo fi = mi as FieldInfo;
                        
                        if (!fi.FieldType.IsValueType && fi.FieldType != typeof(string))
                        {
                            continue;
                        }
                    }
                    {
                        int row = ConnectionProperties.Rows.Add(mi.Name, mi);
                        if (ReadOnly)
                        {
                            ConnectionProperties.Rows[row].Cells[1].ReadOnly = true;
                            ConnectionProperties.Rows[row].Cells[1].Style = myReadOnlyStyle;
                        }
                        
                        
                    }
                }
            }
            makeConnectionBottomSpace =
                this.Height -
                (MakeConnection.Location.Y + MakeConnection.Height); 
        }

        Object currentMi = null;
        void ConnectionProperties_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
           currentMi = ConnectionProperties.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        }

        void ConnectionProperties_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
   
            if (e.ColumnIndex == 1)
            {
                string value = ConnectionProperties.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                ConnectionProperties.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = currentMi;
                PropertyInfo pi = currentMi as PropertyInfo;
                Type t = pi.GetValue(serverConnection, null).GetType();
                if (pi != null)
                {
                    if (t.IsValueType)
                    {

                        MethodInfo meth = pi.GetValue(serverConnectionInfo, null).GetType().GetMethod("Parse", new Type[] { typeof(string) });
                        pi.SetValue(serverConnection, meth.Invoke(t, new object[] { value }), null);
                    }
                    else
                    {
                        // must be a string
                        pi.SetValue(serverConnection, value, null);
                    }
                }
                else
                {
                    FieldInfo fi = currentMi as FieldInfo;
                    if (fi != null)
                    {
                        if (t.IsValueType)
                        {
                            MethodInfo meth = pi.GetValue(serverConnectionInfo, null).GetType().GetMethod("Parse", new Type[] { typeof(string) });
                            fi.SetValue(serverConnection, meth.Invoke(t, new object[] { value }));
                        }
                        else
                        {
                            // must be a string
                            fi.SetValue(serverConnection, value);
                        }
                    }
                }

            }
            //ConnectionProperties.Refresh();
        }

        void ConnectionProperties_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                MemberInfo mi = e.Value as MemberInfo;
                if (mi != null)
                {
                    PropertyInfo pi = mi as PropertyInfo;
                    if (pi != null)
                    {
                        e.Value = pi.GetValue(serverConnection, null);
                    }
                    else
                    {
                        FieldInfo fi = mi as FieldInfo;
                        if (fi != null)
                        {
                            e.Value = fi.GetValue(serverConnection);
                        }
                    }
                    if (serverConnection.ProcessID>0)
                    {
                        int a = 0;
                    }

                }
            }
            else
            {
                e.Value = ConnectionProperties.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; 
            }
        }

        private void ServerConnection_SizeChanged(object sender, EventArgs e)
        {
            MakeConnection.Location = new Point(
                MakeConnection.Location.X,
                this.Height - makeConnectionBottomSpace - MakeConnection.Height   );
            ConnectSuccess.Location = new Point(
                ConnectSuccess.Location.X,
                MakeConnection.Location.Y);
            int bSpace = MakeConnection.Location.Y - (ConnectionProperties.Location.Y
            + ConnectionProperties.Height);

            int hSpace = ConnectionProperties.Location.X;
            ConnectionProperties.Size = new Size(this.Size.Width - 2 * hSpace,
                MakeConnection.Location.Y - ConnectionProperties.Location.Y 
                );
        }

       private void MakeConnection_Click(object sender, EventArgs e)
       {
           serverConnection.Disconnect();
           bool ok = true;
           try
           {
               serverConnection.Connect();
           }
           catch
           {
               ok = false;
           }
           ConnectSuccess.Checked  = ok;
           ConnectionProperties.Refresh();
       }
    }
}