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 Logging_Options : Form
    {
        public Logging_Options()
        {
            InitializeComponent();
        }

        string AbortStatus = "0";
        private void Logging_Options_Load(object sender, EventArgs e)
        {
            //if the Master file doesn't exist load defaults
            if (File.Exists("./xml/Master.xml") == true)
            {
                LoadFile();
            }
            else
            {
                //load a set of defaults
                rdoFormatPlain.Checked = true;
                rdoSvcFile.Checked = true;
                rdoChatText.Checked = true;
                rdoStatusOff.Checked = true;
            }
        }

        private void SaveFile()
        {
            string MasterFile = "./xml/Master.xml";
            string Format = "1";
            string Service = "1";
            string LogDir = lblLogDir.Text;
            string Chat = "1";
            string Status = "1";


            //Logging Format
            if (rdoFormatBoth.Checked == true)
            {
                Format = "3";
            }
            else if (rdoFormatWiki.Checked == true)
            {
                Format = "2";
            }
            else
            {
                Format = "1";
            }

            //Logging Service
            if (rdoSvcBoth.Checked == true)
            {
                Service = "3";
                if (LogDir.Contains("No Directory Selected") == true)
                {
                    MessageBox.Show("Please Select a Directory before continueing.");
                    AbortStatus = "1";

                }
                LogDir = lblLogDir.Text;
            }
            else if (rdoSvcSQL.Checked == true)
            {
                Service = "2";
                LogDir = "No Directory Selected";
            }
            else
            {
                Service = "1";
                if (LogDir.Contains("No Directory Selected") == true)
                {
                    MessageBox.Show("Please Select a Directory before continueing.");
                    AbortStatus = "1";

                }
                LogDir = lblLogDir.Text;
            }

            //Chat Box View
            if (rdoChatWiki.Checked == true)
            {
                Chat = "2";
            }
            else
            {
                Chat = "1";
            }

            //Status
            if (rdoStatusOff.Checked == true)
            {
                Status = "2";
            }
            else
            {
                Status = "1";
            }

            //Update the Master File
            if (File.Exists(MasterFile) == false)
            {
                Form1.MakeMaster();
            }

            xml.UpdateXML(MasterFile, "LOG_Format", Format);
            xml.UpdateXML(MasterFile, "LOG_Service", Service);
            xml.UpdateXML(MasterFile, "LOG_Directory", LogDir);
            xml.UpdateXML(MasterFile, "LOG_ChatBox", Chat);
            xml.UpdateXML(MasterFile, "LOG_Status", Status);
            if (chkDebug.Checked == true)
            {
                xml.UpdateXML(MasterFile, "LOG_Debug", "True");
            }
            else
            {
                xml.UpdateXML(MasterFile, "LOG_Debug", "False");

            }
        }

        private void LoadFile()
        {
            //lets break this down by formats
            string MasterFile = "./xml/Master.xml";
            string Format = xml.GetXML(MasterFile, "LOG_Format");
            string Service = xml.GetXML(MasterFile, "LOG_Service");
            string LogDir = xml.GetXML(MasterFile, "LOG_Directory");
            string Chat = xml.GetXML(MasterFile, "LOG_ChatBox");
            string Status = xml.GetXML(MasterFile, "LOG_Status");

            switch (Format)
            {
                case "1":
                    rdoFormatPlain.Checked = true;
                    break;
                case "2":
                    rdoFormatWiki.Checked = true;
                    break;
                case "3":
                    rdoFormatBoth.Checked = true;
                    break;
            }

            switch (Service)
            {
                case "1":
                    rdoSvcFile.Checked = true;
                    lblLogDir.Text = LogDir;
                    break;
                case "2":
                    rdoSvcFile.Checked = true;
                    lblLogDir.Text = "No Directory Selected";
                    break;
                case "3":
                    rdoSvcBoth.Checked = true;
                    lblLogDir.Text = LogDir;
                    break;
            }

            switch (Chat)
            {
                case "1":
                    rdoChatText.Checked = true;
                    break;
                case "2":
                    rdoChatWiki.Checked = true;
                    break;
            }

            switch (Status)
            {
                case "1":
                    rdoStatusOn.Checked = true;
                    break;
                case "2":
                    rdoStatusOff.Checked = true;
                    break;
            }

            if (xml.GetXML(MasterFile, "LOG_Debug") == "True")
            {
                chkDebug.Checked = true;
            }
            else
            {
                chkDebug.Checked = false;
            }

        }

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

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFile();
            if (AbortStatus == "1")
            {
                //don't close the form
            }
            else
            {
                //Form1.CallUpdate();
                this.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string saveFileLocation = folderBrowserDialog1.SelectedPath;
                lblLogDir.Text = saveFileLocation;
            }

        }
    }
}
