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

        public string FileName = "";
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "n:\\logs\\";
            //openFileDialog1.InitialDirectory = LogDirectory;
            openFileDialog1.Filter = "Campaign files (*.cml)|*.cml|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //OpenFile(openFileDialog1.FileName);
                FileName = openFileDialog1.FileName;
                lblDM.Text = xml.GetXML(openFileDialog1.FileName, "DM_Alias");
                lblPlayers.Text = xml.GetXML(openFileDialog1.FileName, "CHARS");
                lblStartDate.Text = xml.GetXML(openFileDialog1.FileName, "CreationDate");
                lblCampaign.Text = xml.GetXML(openFileDialog1.FileName, "CampaignName");
                lblBot.Text = xml.GetXML(openFileDialog1.FileName, "Bot_Name");
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Save the selected file details to the primary XML File.

            //TODO
            if (File.Exists("./xml/Master.xml") == false)
            {
                //we'll need to create the XML, so call it.
                Form1.MakeMaster();
            }
                UpdateCampaign();
        }

        private void UpdateCampaign()
        {
            xml.UpdateXML("./xml/Master.xml", "CampaignName", lblCampaign.Text);
            if (FileName != "")
            {
                xml.UpdateXML("./xml/Master.xml", "CampaignFile", FileName);
            }
            //Form1.CallUpdate();
            this.Close();
        }

        private void Campaign_Load(object sender, EventArgs e)
        {
            //load the defaul xml.
            if ((File.Exists("./xml/Master.xml") == true) && (xml.GetXML("./xml/Master.xml", "CampaignName") != "null"))
            {
                //load the details
                string CampaignFile = xml.GetXML("./xml/Master.xml", "CampaignFile");
                lblDM.Text = xml.GetXML(CampaignFile, "DM_Alias");
                lblPlayers.Text = xml.GetXML(CampaignFile, "CHARS");
                lblStartDate.Text = xml.GetXML(CampaignFile, "CreationDate");
                lblCampaign.Text = xml.GetXML(CampaignFile, "CampaignName");
                lblBot.Text = xml.GetXML(CampaignFile, "Bot_Name");

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Form1.CallUpdate();
            this.Close();
        }

        private void lblStartDate_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            CampaignTool ct = new CampaignTool();
            ct.Show();
        }
    }
}
