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;
using System.Xml;

namespace Grovewood
{
    public partial class CampaignTool : Form
    {
        string FileName = "";
        int CharCount = 0;
        public CampaignTool()
        {
            InitializeComponent();
        }

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

        private void button3_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;
                txtDungeonMaster.Text = xml.GetXML(openFileDialog1.FileName, "DM_Alias");
                //lblPlayers.Text = xml.GetXML(openFileDialog1.FileName, "CHARS");
                dateStart.Text = xml.GetXML(openFileDialog1.FileName, "CreationDate");
                txtCampaignName.Text = xml.GetXML(openFileDialog1.FileName, "CampaignName");
                txtBot.Text = xml.GetXML(openFileDialog1.FileName, "Bot_Name");
                txtDMWiki.Text = xml.GetXML(openFileDialog1.FileName, "DM_Player");

                //Populate the Character List
                CharCount = Convert.ToInt32(xml.GetXML(openFileDialog1.FileName, "CHARS"));
                for (int i = 0; i <= CharCount; i++)
                {
                    cmbCharacters.Items.Add(xml.GetXML(openFileDialog1.FileName, "CHAR" + i));
                }
            }

            
        }

        private void cmbCharacters_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Make sure its not a 0 index
            if (cmbCharacters.SelectedIndex <= CharCount)
            {
                int SelectedCharacter = cmbCharacters.SelectedIndex;
                txtCharName.Text = xml.GetXML(FileName, "CHAR" + SelectedCharacter);
                txtCharWiki.Text = xml.GetXML(FileName, "PAGE" + SelectedCharacter);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (cmbCharacters.SelectedIndex <= CharCount)
            {
                int SelectedCharacter = cmbCharacters.SelectedIndex;
                xml.UpdateXML(FileName, "CHAR" + SelectedCharacter, txtCharName.Text);
                xml.UpdateXML(FileName, "PAGE" + SelectedCharacter, txtCharWiki.Text);

                //Reload the values to make sure they stuck
                txtCharName.Text = xml.GetXML(FileName, "CHAR" + SelectedCharacter);
                txtCharWiki.Text = xml.GetXML(FileName, "PAGE" + SelectedCharacter);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int SelectedCharacter = cmbCharacters.SelectedIndex;
            if (MessageBox.Show("Are you sure you would like to delete this character?", "Delete Character", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                xml.UpdateXML(FileName, "CHAR" + SelectedCharacter, "Deleted");
                xml.UpdateXML(FileName, "PAGE" + SelectedCharacter, "Deleted");

                //rebuild the combo box without the deleted entry
                //clear the combo box
                cmbCharacters.Items.Clear();
                for (int i = 0; i <= CharCount; i++)
                {
                    if (xml.GetXML(FileName, "CHAR" + i) == "Deleted")
                    {
                        //skip the entry
                    }
                    else
                    {
                        cmbCharacters.Items.Add(xml.GetXML(FileName, "CHAR" + i));
                    }
                }

            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (File.Exists(FileName) == false)
            {
                MessageBox.Show("Please save the Campaign before adding characters.", "Error");
            }
            else
            {
                if ((txtCharName.Text == null) || (txtCharWiki.Text == null))
                {
                    MessageBox.Show("You must enter both a Character Name, and a Wiki Page value to add this character.", "Error");
                }
                else
                {
                    if (MessageBox.Show("Add the Character " + txtCharName.Text + "?", "Add Character", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        //write the values to the system.
                        cmbCharacters.Items.Add(txtCharName.Text);
                        CharCount = Convert.ToInt32(xml.GetXML(FileName, "CHARS")) + 1;
                        xml.AppendElement(FileName, "CHAR" + CharCount, txtCharName.Text);
                        xml.AppendElement(FileName, "PAGE" + CharCount, txtCharWiki.Text);
                        xml.UpdateXML(FileName, "CHARS", CharCount.ToString());
                    }
                }
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd1 = new SaveFileDialog();
            sfd1.InitialDirectory = "n:\\logs\\";
            sfd1.Filter = "Campaign Files (*.cml)|*cml|All Files (*.*)|*.*";
            sfd1.FilterIndex = 2;
            sfd1.RestoreDirectory = true;

            if (sfd1.ShowDialog() == DialogResult.OK)
            {
                if (File.Exists(sfd1.FileName) == true)
                {
                    //we're jus' updating
                    xml.UpdateXML(sfd1.FileName, "CampaignName", txtCampaignName.Text);
                    xml.UpdateXML(sfd1.FileName, "CreationDate", dateStart.Value.ToString());
                    xml.UpdateXML(sfd1.FileName, "DM_Alias", txtDungeonMaster.Text);
                    xml.UpdateXML(sfd1.FileName, "DM_Player", txtDMWiki.Text);
                    xml.UpdateXML(sfd1.FileName, "Bot_Name", txtBot.Text);
                    xml.UpdateXML(sfd1.FileName, "CHARS", CharCount.ToString());
                    //todo Character Processing
                    ProcessCombo("Update", sfd1.FileName);
                }
                else
                {
                    //we're making it
                    XmlTextWriter saveFile = new XmlTextWriter(sfd1.FileName, null);
                    saveFile.WriteStartDocument();
                    saveFile.WriteComment("Campagin File");
                    saveFile.WriteStartElement("Campaign");
                    saveFile.WriteComment("Primary Campaign Details");
                    saveFile.WriteElementString("CampaignName", txtCampaignName.Text);
                    saveFile.WriteElementString("CreationDate", dateStart.Value.ToString());
                    saveFile.WriteComment("Dungeon Master");
                    saveFile.WriteElementString("DM_Alias", txtDungeonMaster.Text);
                    saveFile.WriteElementString("DM_Player", txtDMWiki.Text);
                    saveFile.WriteComment("BOT");
                    saveFile.WriteElementString("Bot_Name", txtBot.Text);
                    saveFile.WriteComment("Wiki Fields and Other Information");
                    saveFile.WriteElementString("CHARS", "-1"); 
                    saveFile.WriteEndElement();
                    saveFile.WriteEndDocument();
                    saveFile.Close();
                }
            }
        }

        private void ProcessCombo(string Type, string FileName)
        {
            //Type will be Update or New
            if (Type == "Update")
            {
                string CharName = "";
                string WikiValue = "";

                int RealChar = 0;
                for (int i = 0; i <= CharCount; i++)
                {
                    //pull the values
                    if (i > cmbCharacters.Items.Count -1)
                    {
                        break;
                    }
                    else
                    {
                        cmbCharacters.SelectedIndex = i;
                    }
                    CharName = xml.GetXML(FileName, "CHAR" + i);
                    WikiValue = xml.GetXML(FileName, "PAGE" + i);

                    if (CharName != "Deleted")
                    {
                        xml.UpdateXML(FileName, "CHAR" + RealChar, txtCharName.Text);
                        xml.UpdateXML(FileName, "PAGE" + RealChar, txtCharWiki.Text);
                        RealChar++;
                    }
                }

                xml.UpdateXML(FileName, "CHARS", RealChar.ToString());
            }
            else
            {
                //we don't need to do anything at this point in time due to adding characters auto adding
            }
        }
    }
}
