using System;
using System.Windows.Forms;

namespace AgencySystem
{
    public partial class TestingPage : UserControl
    {
        private static TestingPage _instance;
        public static TestingPage Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new TestingPage();
                return _instance;
            }
        }
        
        public TestingPage()
        {
            InitializeComponent();
            TestPageVariable.ResultForTestPageVariable = "";
            TestPageVariable.UseTestPageVariable = false;
            DefaultScreen.initializeHeading(TestingPageHeading);
            DefaultScreen.initializePanel(TestingPagePanel);
            TestingPageHeading.Text = Properties.Resources.TestingPageHeading;
        }

        public static class TestPageVariable
        {
            public static bool UseTestPageVariable { get; set; }
            public static string ResultForTestPageVariable { get; set; }
        }

        /// <summary>
        /// Function to test database connection state
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void testDBConnection_Click(object sender, EventArgs e)
        {
            TestPageRichTextBox.Text = Convert.ToString(DatabaseLayout.myConnection.State);
        }

        private void switchBetweenResults()
        {
            if (TestPageVariable.ResultForTestPageVariable != "")
            {
                TestPageRichTextBox.Text = TestPageVariable.ResultForTestPageVariable;
                TestPageVariable.ResultForTestPageVariable = "";
            }
            else
            {
                TestPageRichTextBox.Text = Properties.Resources.AllGood;
            }
        }

        private void testDBConnectionOpen_Click(object sender, EventArgs e)
        {
            TestPageVariable.UseTestPageVariable = true;
            DatabaseLayout.ConnectionToDatabaseOpen();
            TestPageVariable.UseTestPageVariable = false;
            switchBetweenResults();
        }

        /// <summary>
        /// Function which tests closing the database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void testDBConnectionClose_Click(object sender, EventArgs e)
        {
            TestPageVariable.UseTestPageVariable = true;
            DatabaseLayout.ConnectionToDatabaseClose();
            TestPageVariable.UseTestPageVariable = false;
            switchBetweenResults();
        }

        /// <summary>
        /// Function which tests user in database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void testUserInDB_Click(object sender, EventArgs e)
        {
            string username = TestUsInDbTextBox.Text;
            bool count = UserLayout.IsUsernameInDatabase(username);
            if (count)
            {
                TestPageRichTextBox.Text = Properties.Resources.UserAlreadyExists;
            }
            else
            {
                TestPageRichTextBox.Text = Properties.Resources.UserNotExists;
            }
        }
        
    }
}
