using System;
using System.Timers;
using System.Data.SqlClient;
using System.Collections.Generic;

using FilterAPI;

namespace FilterChatViewer
{
    internal class Program
    {
        public static String ServiceName = "Chat Viewer";

        public static Authentication Auth;
        public static Database SQL;

        private static Timer ChatViewTimer;

        private static List<Int64> TablenIDs = new List<Int64>();

        private static void Main()
        {
            Auth = new Authentication();
            SQL = new Database(Auth);

            Console.Title = String.Format("{0} Filter [Chat Viewer] :: Aeris.NET :: 2017/2018", Auth.ConfigValues["ServerName"]);
            
            if (!Program.SQL.TableEmpty(Program.Auth.ConfigValues["FilterDB"], "tGuild")) { TablenIDs.Add(Convert.ToInt64(SQL.GetValue(Program.Auth.ConfigValues["FilterDB"], "tGuild", "nID"))); }
            else { TablenIDs.Add(0); }

            if (!Program.SQL.TableEmpty(Program.Auth.ConfigValues["FilterDB"], "tAcademy")) { TablenIDs.Add(Convert.ToInt64(SQL.GetValue(Program.Auth.ConfigValues["FilterDB"], "tAcademy", "nID"))); }
            else { TablenIDs.Add(0); }

            if (!Program.SQL.TableEmpty(Program.Auth.ConfigValues["FilterDB"], "tParty")) { TablenIDs.Add(Convert.ToInt64(SQL.GetValue(Program.Auth.ConfigValues["FilterDB"], "tParty", "nID"))); }
            else { TablenIDs.Add(0); }

            if (!Program.SQL.TableEmpty(Program.Auth.ConfigValues["FilterDB"], "tWhisper")) { TablenIDs.Add(Convert.ToInt64(SQL.GetValue(Program.Auth.ConfigValues["FilterDB"], "tWhisper", "nID"))); }
            else { TablenIDs.Add(0); }

            if (!Program.SQL.TableEmpty(Program.Auth.ConfigValues["FilterDB"], "tNormal")) { TablenIDs.Add(Convert.ToInt64(SQL.GetValue(Program.Auth.ConfigValues["FilterDB"], "tNormal", "nID"))); }
            else { TablenIDs.Add(0); }

            if (!Program.SQL.TableEmpty(Program.Auth.ConfigValues["FilterDB"], "tShout")) { TablenIDs.Add(Convert.ToInt64(SQL.GetValue(Program.Auth.ConfigValues["FilterDB"], "tShout", "nID"))); }
            else { TablenIDs.Add(0); }

            if (!Program.SQL.TableEmpty(Program.Auth.ConfigValues["FilterDB"], "tRoar")) { TablenIDs.Add(Convert.ToInt64(SQL.GetValue(Program.Auth.ConfigValues["FilterDB"], "tRoar", "nID"))); }
            else { TablenIDs.Add(0); }

            ChatViewTimer = new Timer();
            ChatViewTimer.AutoReset = true;
            ChatViewTimer.Elapsed += ChatViewTimer_Elapsed;
            ChatViewTimer.Interval = 2000;
            ChatViewTimer.Start();

            Console.ReadLine();
        }

        private static void ChatViewTimer_Elapsed(Object Sender, ElapsedEventArgs Args)
        {
            ChatViewTimer.Stop();

            String[] Tables = new String[] { "tGuild", "tAcademy", "tParty", "tWhisper", "tNormal", "tShout", "tRoar" };

            for (Byte Counter = 0; Counter < 7; Counter++)
            {
                String Table = Tables[Counter];

                if (!Program.SQL.TableEmpty(Program.Auth.ConfigValues["FilterDB"], Table))
                {
                    Int64 nID = Convert.ToInt64(SQL.GetValue(Program.Auth.ConfigValues["FilterDB"], Table, "nID"));

                    using (SqlCommand Command = SQL.Connection.CreateCommand())
                    {
                        for (Int64 nIDCounter = TablenIDs[Counter]; nIDCounter < nID; nIDCounter++)
                        {
                            Command.CommandText = String.Format("SELECT * FROM {0}..{1} WHERE nID = {2} ORDER BY nID DESC", Program.Auth.ConfigValues["FilterDB"], Table, nIDCounter);

                            if (Table == "tGuild") { Console.ForegroundColor = ConsoleColor.Green; }
                            else if (Table == "tAcademy") { Console.ForegroundColor = ConsoleColor.Gray; }
                            else if (Table == "tParty") { Console.ForegroundColor = ConsoleColor.Blue; }
                            else if (Table == "tWhisper") { Console.ForegroundColor = ConsoleColor.Magenta; }
                            else if (Table == "tNormal") { Console.ForegroundColor = ConsoleColor.White; }
                            else if (Table == "tShout") { Console.ForegroundColor = ConsoleColor.Yellow; }
                            else if (Table == "tRoar") { Console.ForegroundColor = ConsoleColor.Cyan; }

                            using (SqlDataReader Reader = Command.ExecuteReader())
                            {
                                Reader.Read();

                                if (Table != "tWhisper") { Console.WriteLine("{0} || {1} || {2}: {3}", Convert.ToDateTime(Reader["dDate"]).ToString("HH:mm:ss"), Table.TrimStart('t'), Reader["sID"], Reader["sMessage"]); }
                                else { Console.WriteLine("{0} || {1} -> {2}: {3}", Convert.ToDateTime(Reader["dDate"]).ToString("HH:mm:ss"), Reader["sID"], Reader["sIDReceive"], Reader["sMessage"]); }

                                Reader.Close();
                            }
                        }
                    }

                    TablenIDs[Counter] = nID;
                }
            }

            ChatViewTimer.Start();
        }
    }
}
