using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections.Generic;

using FilterAPI;
using FilterAPI.Networking;

using FilterController.LocalHandlers;
using FilterController.LocalNetworking;

namespace FilterController
{
    public partial class Main : Form
    {
        public Main() { InitializeComponent(); }

        private void Main_Shown(Object Sender, EventArgs Args)
        {
            Program.Auth = new Authentication();
            Program.SQL = new Database(Program.Auth);

            Program.LocalClient = new LocalClient();
            Program.LocalHandlers = new LocalHandlerLoader();

            Text = String.Format("{0} Filter [Controller] :: Aeris.NET :: 2017/2018", Program.Auth.ConfigValues["ServerName"]);
        }

        private void Value_TextChanged(Object Sender, EventArgs Args)
        {
            DataTable CharInfoTable = new DataTable();
            CharInfoTable.Columns.Add("CharactersID");
            CharInfoTable.Columns.Add("IsOnline");

            List<String> OnlinesIDs = Program.SQL.GetOnlinesIDs();

            using (SqlCommand Command = Program.SQL.Connection.CreateCommand())
            {
                Command.CommandText = String.Format("SELECT sID FROM {0}..tCharacter WHERE sID LIKE '%{1}%' AND bDeleted = '0'", Program.Auth.ConfigValues["CharacterDB"], Value.Text);

                using (SqlDataReader Reader = Command.ExecuteReader())
                {
                    while (Reader.Read()) { CharInfoTable.Rows.Add(new Object[] { Convert.ToString(Reader["sID"]), OnlinesIDs.Contains(Convert.ToString(Reader["sID"])) ? "Yes" : "No" }); }
                }
            }

            CharInfo.DataSource = CharInfoTable;
        }

        private void Disconnect_Click(Object Sender, EventArgs Args)
        {
            if (CharInfo.SelectedRows.Count != 1) { MessageBox.Show("Please select only one row!", Text, MessageBoxButtons.OK); }
            else
            {
                using (var LocalPacket = new Packet(4, 7))
                {
                    LocalPacket.PacketWriter.Write(CharInfo.SelectedRows[0].Cells[0].Value.ToString());
                    LocalPacket.PacketWriter.Write(false);
                    LocalPacket.PacketWriter.Write("Server");
                    LocalPacket.PacketWriter.Write("");

                    Program.LocalClient.SendPacket(LocalPacket);
                }

                MessageBox.Show("The character should of been kicked from the game.", Text, MessageBoxButtons.OK);
            }
        }

        private void Login_Disallow_Click(Object Sender, EventArgs Args)
        {
            if (CharInfo.SelectedRows.Count != 1) { MessageBox.Show("Please select only one row!", Text, MessageBoxButtons.OK); }
            else
            {
                using (SqlCommand Command = Program.SQL.Connection.CreateCommand())
                {
                    Command.CommandText = String.Format("SELECT TOP 1 nUserNo FROM {0}..tCharacter WHERE sID = '{1}'", Program.Auth.ConfigValues["CharacterDB"], CharInfo.SelectedRows[0].Cells[0].Value.ToString());

                    Int32 nUserNo = Convert.ToInt32(Command.ExecuteScalar());

                    Command.CommandText = String.Format("UPDATE {0}..{1} SET nAuthID = '3' WHERE nEMID = '{2}'", Program.Auth.ConfigValues["AccountDB"], Program.Auth.ConfigValues["AccountTable"], nUserNo);
                    Command.ExecuteNonQuery();

                    MessageBox.Show("The character's account has been set to \"The ID has been blocked.\"", Text, MessageBoxButtons.OK);
                }
            }
        }

        private void Login_Allow_Click(Object Sender, EventArgs Args)
        {
            if (CharInfo.SelectedRows.Count != 1) { MessageBox.Show("Please select only one row!", Text, MessageBoxButtons.OK); }
            else
            {
                using (SqlCommand Command = Program.SQL.Connection.CreateCommand())
                {
                    Command.CommandText = String.Format("SELECT TOP 1 nUserNo FROM {0}..tCharacter WHERE sID = '{1}'", Program.Auth.ConfigValues["CharacterDB"], CharInfo.SelectedRows[0].Cells[0].Value.ToString());

                    Int32 nUserNo = Convert.ToInt32(Command.ExecuteScalar());

                    Command.CommandText = String.Format("UPDATE {0}..{1} SET nAuthID = '1' WHERE nEMID = '{2}'", Program.Auth.ConfigValues["AccountDB"], Program.Auth.ConfigValues["AccountTable"], nUserNo);
                    Command.ExecuteNonQuery();

                    MessageBox.Show("The character's account is now able to login again.", Text, MessageBoxButtons.OK);
                }
            }
        }

        private void Block_LIP_Click(Object Sender, EventArgs Args)
        {
            if (CharInfo.SelectedRows.Count != 1) { MessageBox.Show("Please select only one row!", Text, MessageBoxButtons.OK); }
            else
            {
                using (SqlCommand Command = Program.SQL.Connection.CreateCommand())
                {
                    Command.CommandText = String.Format("SELECT TOP 1 nUserNo FROM {0}..tCharacter WHERE sID = '{1}'", Program.Auth.ConfigValues["CharacterDB"], CharInfo.SelectedRows[0].Cells[0].Value.ToString());

                    Int32 nUserNo = Convert.ToInt32(Command.ExecuteScalar());

                    Command.CommandText = String.Format("SELECT TOP 1 sIP FROM {0}..tUserIPs WHERE nUserNo = '{1}' ORDER BY dDate DESC", Program.Auth.ConfigValues["FilterDB"], nUserNo);

                    String sIP = Convert.ToString(Command.ExecuteScalar());

                    if (sIP == String.Empty) { MessageBox.Show("The character has never logged into the server?", Text, MessageBoxButtons.OK); }
                    else
                    {
                        Command.CommandText = String.Format("UPDATE {0}..tIPBlock SET bBlock = '1' WHERE sIP = '{1}'", Program.Auth.ConfigValues["FilterDB"], nUserNo);
                        Command.ExecuteNonQuery();
                    }
                }

                MessageBox.Show("The last IP-Address that the character's account used has been blocked.", Text, MessageBoxButtons.OK);
            }
        }
    }
}
