using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Grovewood
{
    class wiki
    {

        //TODO
        //Read IRC Data, and Parse.
        //Use this for resource for full-time rendering in RTF box: https://www.codeproject.com/Articles/4544/Insert-Plain-Text-and-Images-into-RichTextBox-at-R
        //Refrense Doku Parser programming for parsing of IRC -> Wiki

        public static string DM_Icon = "{{:campaigns:stota:dmicon-small.png?nolink&25|}}";
        public static string PL_Icon = "{{:campaigns:stota:playericon.png?nolink&25|}}";
        public static string BT_Icon = "{{:campaigns:stota:d20.png?nolink&25|}}";
        public static string OC_Icon = "{{:campaigns:stota:ooc.png?nolink&25|}}";
        public static string AC_Icon = "{{:campaigns:stota:action.png?nolink&25|*}}";

        public static string DoWiki(string text)
        {
            //string Final = "";
            //string[] Names = new string[Convert.ToInt32(listBox1.Items.Count)];
            string MasterFile = "./xml/Master.xml";
            string CampaignFile = xml.GetXML(MasterFile, "CampaignFile");
            string[] nameSplit = new string[2];

            //we will need to parse entire sentences, so yea, loop

            string Sentence = "";
            string[] MyText = text.Split(' ');
            int SentLength = MyText.Length;

            //build the sentence back and parse it.
            for (int i = 0; i <= SentLength - 1; i++)
            {
                if ((i == 2) && (MyText[i + 1].Contains('(') == true))
                {
                    Sentence = Sentence + OC_Icon;
                }

                if ((i == 2) && (text.Contains("ACTION") == false))
                {
                    //its a name
                    if (MyText[i].Contains(xml.GetXML(CampaignFile, "DM_Alias")))
                    {
                        Sentence = Sentence + DM_Icon + "<" + ReturnWikiName(MyText[i], false) + "> ";
                    }
                    else if (MyText[i].Contains(xml.GetXML(MasterFile, "BOT_Name")))
                    {
                        Sentence = Sentence + BT_Icon + "<" + ReturnWikiName(MyText[i], false) + "> ";
                    }
                    else
                    {
                        if (ReturnWikiName(MyText[i], true) == "true")
                        {
                            Sentence = Sentence + PL_Icon + "<" + ReturnWikiName(MyText[i], false) + "> ";
                        }

                        else if (MyText[i].Contains(xml.GetXML(CampaignFile, "Bot_Name")) == true)
                        {
                            Sentence = Sentence + BT_Icon + "<" + MyText[i] + "> ";
                        }
                        
                        else
                        {
                            Sentence = Sentence + "<" + ReturnWikiName(MyText[i], false) + "> ";
                        }

                    }
                }
                else
                {
                    Sentence = Sentence + ReturnWikiName(MyText[i], false).Replace("ACTION", "").Replace("", "") + " ";
                }
            }
            return Sentence;
        }

        //----------------------------
        // Action / OOC
        //----------------------------

        public static string AOC(string Text)
        {
            if (Text.StartsWith("*") == true)
            {
                //its an action
                Text = AC_Icon + " " + Text;
            }
            else if ((Text.StartsWith("(") == true) || (Text.StartsWith("<") == true)) //the < one is a Zio fix that numbnuts.
            {
                //its OOC
                Text = OC_Icon + Text;
            }
            return Text;
        }

        //----------------------------
        // Name Search
        //----------------------------

        public static string ReturnWikiName(string Text, bool CheckPlayer)
        {
            //string Final = "";
            //int WordCount = Text.Length;
            //string[] Words = Text.Split(' ');
            string CampaignFile = xml.GetXML("./xml/Master.xml", "CampaignFile");
            if (Text.Contains('*') == true)
            {
                //its an Action or OOC, so process it and dump it back

                return AOC(Text);
            }

            Text = Text.TrimStart('*');
            Text = Text.TrimStart('<');
            Text = Text.TrimEnd('>');
            //what is the name?
            //string Name = Words[2];

            int Players = Convert.ToInt32(xml.GetXML(CampaignFile, "CHARS"));

            for (int i = 0; i <= Players; i++)
            {
                if (xml.GetXML(CampaignFile, "CHAR" + i) == Text)
                {
                    if (CheckPlayer == true)
                    {
                        return "true";
                    }
                    Text = xml.GetXML(CampaignFile, "PAGE" + i);
                    return Text;
                }
            }


            return Text;
        }

        //----------------------------
        // FORMATTING CALLS
        //----------------------------

        public static void CallFormatting(string Type, string Text)
        {
            //Format 1 = Text
            //Format 2 = Wiki
            //Format 3 = Both
            string FileName = DateTime.Now.ToShortDateString();
            FileName = FileName.Replace('/', '-');
            //FileName = FileName + ".log";

            switch (Type)
            {
                case "1":
                    FileName = FileName + "_Text.log";
                    DoLog(FileName, Text);
                    break;
                case "2":
                    FileName = FileName + "_Wiki.log";
                    DoLog(FileName, DoWiki(Text));
                    break;
                case "3":
                    FileName = FileName + "_Text.log";
                    DoLog(FileName, Text);
                    FileName = FileName.Replace("_Text", "_Wiki");
                    DoLog(FileName, DoWiki(Text + " \\\\"));
                    break;
            }

        }

        public static void DoLog(string FileName, string Text)
        {
            if (xml.GetXML("./xml/Master.xml", "LOG_Status") == "1")
            {
                if ((xml.GetXML("./xml/Master.xml", "LOG_Service") == "1") || (xml.GetXML("./xml/Master.xml", "LOG_Service") == "3"))
                {
                    //we need to check for SQL as well.
                    if (Directory.Exists("./gamelogs") == false)
                    {
                        Directory.CreateDirectory("./gamelogs");
                    }

                    string LogPath = @"./gamelogs/" + FileName;

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

                    using (StreamWriter sw = File.AppendText(LogPath))
                    {
                        Text = Text.Replace("ACTION", "").Replace("", "") + " ";
                        sw.WriteLine(Text);
                    }
                }
                else
                {
                    //we're assuming its SQL.
                    if (((xml.GetXML("./xml/Master.xml", "LOG_Service") == "2") || 
                        (xml.GetXML("./xml/Master.xml", "LOG_Service") == "3")) && 
                        (sql.sqlcon == true))
                    {
                        //Do SQL Logging

                    }
                }
                //Test - Append the return to the Chatbot
                if (xml.GetXML("./xml/Master.xml", "LOG_Debug") == "True")
                {
                    Form1.ChatAppend("DEBUG: " + Text);
                }
            }
        }
    }
}
