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

namespace Grovewood
{
    public partial class Form1 : Form
    {
        public event EventHandler<ChattyUpdated> UpdateChatty;

        protected virtual void OnChattyUpdated(ChattyUpdated e)
        {
            UpdateChatty?.Invoke(this, e);
        }

        public Form1()
        {
            InitializeComponent();
            
            
        }





        private void Form1_Load(object sender, EventArgs e)
        {
            UpdateLabels();

        }


        private void button7_Click(object sender, EventArgs e)
        {
            Campaign cpn = new Campaign();
            cpn.Show();
            UpdateLabels();
        }

        public static void MakeMaster()
        {
            if (Directory.Exists("./xml") == false)
            {
                Directory.CreateDirectory("./xml");
            }
            //xml.CreateXML("./xml/Master.xml", null, "Master", "Master XML File for Grovewood", 10);
            XmlTextWriter saveFile = new XmlTextWriter("./xml/Master.xml", null);
            saveFile.WriteStartDocument();
            saveFile.WriteComment("Master XML File for Grovewood");
            saveFile.WriteStartElement("Master");
            saveFile.WriteComment("IRC Options");
            saveFile.WriteElementString("IRC_Server", "null");
            saveFile.WriteElementString("IRC_Port", "null");
            saveFile.WriteElementString("IRC_Channel", "null");
            saveFile.WriteComment("Campaign Options");
            saveFile.WriteElementString("CampaignName", "null");
            saveFile.WriteElementString("CampaignFile", "null");
            saveFile.WriteComment("Database Settings");
            saveFile.WriteElementString("SQL_ServerAddress", "null");
            saveFile.WriteElementString("SQL_ServerPort", "null");
            saveFile.WriteElementString("SQL_Table", "null");
            saveFile.WriteElementString("SQL_User", "null");
            saveFile.WriteElementString("SQL_Password", "null");
            saveFile.WriteComment("Logging Settings");
            saveFile.WriteElementString("LOG_Format", "1");
            saveFile.WriteElementString("LOG_Service", "1");
            saveFile.WriteElementString("LOG_Directory", "No Directory Selected");
            saveFile.WriteElementString("LOG_ChatBox", "1");
            saveFile.WriteElementString("LOG_Status", "2");
            saveFile.WriteComment("Log DEBUG");
            saveFile.WriteElementString("LOG_Debug", "False");
            saveFile.WriteComment("BOT");
            saveFile.WriteElementString("BOT_Name", "TolBot");  
            saveFile.WriteEndElement();
            saveFile.WriteEndDocument();
            saveFile.Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            IRC_Options io = new IRC_Options();
            io.Show();
        }

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

        private void button8_Click(object sender, EventArgs e)
        {
            Log_Reader lr = new Log_Reader();
            lr.Show();
        }

        public static void ErrorLog(string Error)
        {
            string LogDate = DateTime.Now.ToShortDateString();
            LogDate = LogDate.Replace('/', '-');
            string LogPath = @"./logs/" + LogDate + ".log";
            if (Directory.Exists("./logs") == false)
            {
                Directory.CreateDirectory("./logs");
            }

            if (!File.Exists(LogPath))
            {
                //create the file and write
                using (StreamWriter sw = File.CreateText(LogPath))
                {
                    sw.WriteLine("Log File for the Day of: " + DateTime.Now.ToShortDateString());
                }
            }

            using (StreamWriter sw = File.AppendText(LogPath))
            {
                sw.WriteLine(Error);
                Form1 frm1 = new Form1();
                frm1.lblErrors.Text = frm1.lblErrors.Text + 1;
            }

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Logging_Options lo = new Logging_Options();
            lo.Show();
            UpdateLabels();
        }

        private void UpdateLabels()
        {
            Form1 frm = Application.OpenForms["Form1"] as Form1;
            if (File.Exists("./xml/Master.xml"))
            {
                if ((xml.GetXML("./xml/Master.xml", "CampaignName") != "null") && (xml.GetXML("./xml/Master.xml", "CampaignFile") != "null"))
                {
                    frm.lblCampaign.ForeColor = Color.Green;
                }
                else
                {
                    frm.lblCampaign.ForeColor = Color.Red;
                }
                if (xml.GetXML("./xml/Master.xml", "LOG_Status") == "1")
                {
                    frm.lblLog.ForeColor = Color.Green;
                }
                else
                {
                    frm.lblLog.ForeColor = Color.Red;
                }
                if (Irc.ConStatus == true)
                {
                    frm.lblIRC.ForeColor = Color.Green;
                }
                else
                {
                    frm.lblIRC.ForeColor = Color.Red;
                }
            }
        }


        private void button5_Click(object sender, EventArgs e)
        {
            //Connect to IRC/SQL 
            //This will have to check for SQL connections perodicially encase a change is made from SQL to Text or Both formats.

            Thread t1 = new Thread(() =>
            {
                Irc.Connect();
            });

            t1.Start();
        }

        public static void Chatty(string Text)
        {
            //rtbChat.AppendText(Text + System.Environment.NewLine);
            Form1 frm = Application.OpenForms["Form1"] as Form1;
            if (frm.InvokeRequired)
            {
                frm.rtbChat.Invoke(new Action<string>(Chatty), new object[] { Text });
                return;
            }
            frm.rtbChat.AppendText(Text + Environment.NewLine);
            frm.UpdateLabels();
            //frm.rtbChat.AppendText(Text + Environment.NewLine);
        }


        public static void ChatAppend(string Appendage)
        {
            //Chatty(Appendage);
            Form1 frm = Application.OpenForms["Form1"] as Form1;
            if (frm.InvokeRequired)
            {
                frm.rtbChat.Invoke(new Action<string>(Chatty), new object[] { Appendage });
                return;
            }
            frm.rtbChat.AppendText(Appendage + Environment.NewLine);
            frm.UpdateLabels();
        }


        private static void AppendText(RichTextBox arg1, Color arg2, string arg3)
        {
            throw new NotImplementedException();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendCommand();
        }

        public class ChattyUpdated : EventArgs
        {
            public string TextToAppend { get; set; }
        }

        private void rtbChat_TextChanged(object sender, EventArgs e)
        {
            rtbChat.SelectionStart = rtbChat.Text.Length;
            rtbChat.ScrollToCaret();
        }

        private void TxtCMD_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                SendCommand();
            }

            
        }

        private void SendCommand()
        {
            //send the command.
            if (txtCMD.Text.StartsWith("!") == true)
            {
                //its a server command
                Irc.cmd = txtCMD.Text.TrimStart('!');
                txtCMD.Text = "";
            }
            else
            {
                //they're talking!
                Irc.cmd = txtCMD.Text;
                txtCMD.Text = "";
            }
        }
    }
}
