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

namespace Astronomy.UI
{
    public partial class FormConnectToDatabase : Form
    {
        private Windows.UI.HostList list;
        private string server = null;
        public FormConnectToDatabase()
        {
            InitializeComponent();
            ResourceService.Resources.LoadControlResources(this);
            list = new Windows.UI.HostList();
            list.Initialize();
            list.MultiSelect = false;
            list.HideSelection = false;
            list.Dock = DockStyle.Fill;
            panelCenter.Controls.Add(list);
        }


        internal string Server
        {
            get
            {
                return server;
            }
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            try
            {
                string s = serv;
                if (s != null)
                {
                    AstronomyDB.DataCreator.CreateDatabase(s);
                    server = s;
                    Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        string serv
        {
            get
            {
                string[] s = list.SelectedHosts;
                if (s == null)
                {
                    return null;
                }
                string str = s[0];
                while (true)
                {
                    if (str[0] == '\\')
                    {
                        str = str.Substring(1);
                        continue;
                    }
                    break;
                }
                return str;
            }
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            try
            {
                string s = serv;
                if (s != null)
                {
                    AstronomyDB.AstronomyWarehouse.GetInterface(s);
                    server = s;
                    Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
