using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Grovewood
{
    public partial class Database_Settings : Form
    {
        public Database_Settings()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (File.Exists("./xml/Master.xml") == true)
            {
                UpdateMaster();
                this.Close();
            }
            else
            {
                Form1.MakeMaster();
                UpdateMaster();
                this.Close();
            }
        }

        private void UpdateMaster()
        {
            string MasterFile = "./xml/Master.xml";
            xml.UpdateXML(MasterFile, "SQL_ServerAddress", txtSvr.Text);
            xml.UpdateXML(MasterFile, "SQL_ServerPort", txtPort.Text);
            xml.UpdateXML(MasterFile, "SQL_Table", txtTable.Text);
            //-
            xml.UpdateXML(MasterFile, "SQL_User", txtUser.Text);
            xml.UpdateXML(MasterFile, "SQL_Password", txtPass.Text);
        }

        private void Database_Settings_Load(object sender, EventArgs e)
        {
            if ((File.Exists("./xml/Master.xml") == true) && (xml.GetXML("./xml/Master.xml", "SQL_ServerAddress") != "null"))
            {
                //load the details
                string MasterFile = "./xml/Master.xml";
                txtSvr.Text = xml.GetXML(MasterFile, "SQL_ServerAddress");
                txtPort.Text = xml.GetXML(MasterFile, "SQL_ServerPort");
                txtTable.Text = xml.GetXML(MasterFile, "SQL_Table");
                //-
                txtUser.Text = xml.GetXML(MasterFile, "SQL_User");
                txtPass.Text = xml.GetXML(MasterFile, "SQL_Password");
            }
        }
    }
}
