using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace AgencySystem
{
    public class DatabaseLayout
    {
        /// <summary>
        /// Initial string for database connection
        /// </summary>
        private static MySqlConnection _myConnection = new MySqlConnection("Server=127.127.127.127;Port=3306;Database=agency;Uid=root;Pwd=");
        public static MySqlConnection myConnection
        {
            get
            {
                return _myConnection;
            }

            set
            {
                _myConnection = value;
            }
        }
        

        /// <summary>
        /// Function which opens database connection
        /// </summary>
        public static void ConnectionToDatabaseOpen()
        {
            if (myConnection.State == System.Data.ConnectionState.Closed)
            {
                try
                {
                    myConnection.Open();
                }
                catch (Exception e)
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.ErrorOccured;
                    Default.ErrorReporting(e);
                    if (TestingPage.TestPageVariable.UseTestPageVariable == true)
                    {
                        TestingPage.TestPageVariable.ResultForTestPageVariable = e.ToString();
                    }
                }
                finally
                {
                    Application.DoEvents();
                }
            }
        }

        /// <summary>
        /// Function which closes database connection
        /// </summary>
        public static void ConnectionToDatabaseClose()
        {
            if (myConnection.State == System.Data.ConnectionState.Open)
            {
                try
                {
                    myConnection.Close();
                }
                catch (Exception e)
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.ErrorOccured;
                    Default.ErrorReporting(e);
                    if (TestingPage.TestPageVariable.UseTestPageVariable == true)
                    {
                        TestingPage.TestPageVariable.ResultForTestPageVariable = e.ToString();
                    }
                }
                finally
                {
                    Application.DoEvents();
                }
            }
        }
    }
}
