using System;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlClient;

using FilterAPI;

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

        private void Main_Shown(Object Sender, EventArgs Args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            Program.Auth = new Authentication();
            Program.SQL = new Database(Program.Auth);

            Text = String.Format("{0} Filter (Permissions Editor) :: Kobalt :: 2017", Program.Auth.ConfigValues["ServerName"]);
        }

        private void CurrentDomain_UnhandledException(Object Sender, UnhandledExceptionEventArgs Args) { File.WriteAllLines(String.Format("{0}{1}Crash.txt", AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "")), new String[] { ((Exception)Args.ExceptionObject).ToString() }); }

        private void Value_TextChanged(Object Sender, EventArgs Args)
        {
            using (SqlCommand Command = Program.SQL.Connection.CreateCommand())
            {
                Command.CommandText = String.Format("SELECT TOP 1 nCharNo FROM {0}..tCharacter WHERE sID = '{1}'", Program.Auth.ConfigValues["CharacterDB"], Value.Text);

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

                if (nCharNo != 0)
                {
                    Command.CommandText = String.Format("SELECT TOP 1 * FROM {0}..tPermissions WHERE nCharNo = '{1}'", Program.Auth.ConfigValues["FilterDB"], nCharNo);

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

                        bBypassChecks.Checked = Convert.ToBoolean(Reader["bBypassChecks"]);
                        bSendAdmin.Checked = Convert.ToBoolean(Reader["bSendAdmin"]);
                        bSendWorldMessages.Checked = Convert.ToBoolean(Reader["bSendWorldMessages"]);
                        bCanTrade.Checked = Convert.ToBoolean(Reader["bCanTrade"]);
                        bCanStorage.Checked = Convert.ToBoolean(Reader["bCanStorage"]);
                        bCanGuildStorage.Checked = Convert.ToBoolean(Reader["bCanGuildStorage"]);
                        bCanPickup.Checked = Convert.ToBoolean(Reader["bCanPickup"]);
                        bCanVend.Checked = Convert.ToBoolean(Reader["bCanVend"]);
                        bCanDrop.Checked = Convert.ToBoolean(Reader["bCanDrop"]);

                        bUnstuck.Checked = Convert.ToBoolean(Reader["bUnstuck"]);
                        bTown.Checked = Convert.ToBoolean(Reader["bTown"]);
                        bHidePlayers.Checked = Convert.ToBoolean(Reader["bHidePlayers"]);
                        bLeaveGuild.Checked = Convert.ToBoolean(Reader["bLeaveGuild"]);
                        bDivorce.Checked = Convert.ToBoolean(Reader["bDivorce"]);
                        bPortalNPC.Checked = Convert.ToBoolean(Reader["bPortalNPC"]);
                        bPortableNPC.Checked = Convert.ToBoolean(Reader["bPortableNPC"]);
                        bSpawnMob.Checked = Convert.ToBoolean(Reader["bSpawnMob"]);
                        bSpawnMobMulti.Checked = Convert.ToBoolean(Reader["bSpawnMobMulti"]);
                        bSpawnMobAll.Checked = Convert.ToBoolean(Reader["bSpawnMobAll"]);
                        bSpawnMobPlayer.Checked = Convert.ToBoolean(Reader["bSpawnMobPlayer"]);
                        bItem.Checked = Convert.ToBoolean(Reader["bItem"]);
                        bItemAll.Checked = Convert.ToBoolean(Reader["bItemAll"]);
                        bGiveItem.Checked = Convert.ToBoolean(Reader["bGiveItem"]);

                        bTp.Checked = Convert.ToBoolean(Reader["bTp"]);
                        bTpAll.Checked = Convert.ToBoolean(Reader["bTpAll"]);
                        bMap.Checked = Convert.ToBoolean(Reader["bMap"]);
                        bAbstate.Checked = Convert.ToBoolean(Reader["bAbstate"]);
                        bKQReward.Checked = Convert.ToBoolean(Reader["bKQReward"]);
                        bClearInv.Checked = Convert.ToBoolean(Reader["bClearInv"]);
                        bDisconnect.Checked = Convert.ToBoolean(Reader["bDisconnect"]);
                        bSetLevel.Checked = Convert.ToBoolean(Reader["bSetLevel"]);
                        bRunCommand.Checked = Convert.ToBoolean(Reader["bRunCommand"]);
                        bCanWalk.Checked = Convert.ToBoolean(Reader["bCanWalk"]);
                        bProduceItems.Checked = Convert.ToBoolean(Reader["bProduceItems"]);
                        bMigrateZone.Checked = Convert.ToBoolean(Reader["bMigrateZone"]);
                        bOtherCommands.Checked = Convert.ToBoolean(Reader["bOtherCommands"]);
                    }
                }
            }
        }

        private void Object_CheckedChanged(Object Sender, EventArgs Args)
        {
            CheckBox Column = (CheckBox)Sender;

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

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

                if (nCharNo != 0)
                {
                    Command.CommandText = String.Format("UPDATE {0}..tPermissions SET {1} = '{2}' WHERE nCharNo = '{3}'", Program.Auth.ConfigValues["FilterDB"], Column.Name, Column.Checked, nCharNo);
                    Command.ExecuteNonQuery();
                }
            }
        }
    }
}
