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

using FilterAPI;
using FilterAPI.CustomCurrency;

namespace FilterCurrencyGiver
{
    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);
            Program.Currencies = new CurrencyLoader(Program.Auth);

            foreach (Currency C in Program.Currencies.Currencies) { Useable_Currencies.Items.Add(C.CurrencyName); }

            Useable_Currencies.SelectedIndex = 0;

            Text = String.Format("{0} Filter [Currency Giver] :: Aeris.NET :: 2017/2018", 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)
        {
            Currency C = Program.Currencies.Currencies.Where(CurrencyID => CurrencyID.CurrencyID == Useable_Currencies.SelectedIndex).FirstOrDefault();

            DataTable UserInfoTable = new DataTable();
            UserInfoTable.Columns.Add("Text");
            UserInfoTable.Columns.Add("Currency");

            using (SqlCommand Command = Program.SQL.Connection.CreateCommand())
            {
                if (C.IsCharacter) { Command.CommandText = String.Format("SELECT nCharNo, sID FROM {0}..tCharacter WHERE sID LIKE '%{1}%'", Program.Auth.ConfigValues["CharacterDB"], Value.Text); }
                else { Command.CommandText = String.Format("SELECT nEMID, sUsername FROM {0}..{1} WHERE sUsername LIKE '%{2}%'", Program.Auth.ConfigValues["AccountDB"], Program.Auth.ConfigValues["AccountTable"], Value.Text); }

                using (SqlDataReader Reader = Command.ExecuteReader())
                {
                    while (Reader.Read())
                    {
                        if (C.IsCharacter) { UserInfoTable.Rows.Add(new Object[] { Convert.ToString(Reader["sID"]), Program.SQL.GetAmountForCC(C.TableName, "nCharNo", Convert.ToInt32(Reader["nCharNo"])) }); }
                        else { UserInfoTable.Rows.Add(new Object[] { Convert.ToString(Reader["sUsername"]), Program.SQL.GetAmountForCC(C.TableName, "nUserNo", Convert.ToInt32(Reader["nEMID"])) }); }
                    }
                }
            }

            UserInfo.DataSource = UserInfoTable;
        }

        private void Give_Click(Object Sender, EventArgs Args)
        {
            if (UserInfo.SelectedRows.Count != 1) { MessageBox.Show("Please select only one row!", Text, MessageBoxButtons.OK); }
            else
            {
                Currency C = Program.Currencies.Currencies.Where(CurrencyID => CurrencyID.CurrencyID == Useable_Currencies.SelectedIndex).FirstOrDefault();

                String SelectedValue = Convert.ToString(UserInfo.SelectedRows[0].Cells[0].Value);

                if (C.IsCharacter) { Program.SQL.AddAmountForCC(C.TableName, Convert.ToInt64(Amount.Value), "nCharNo", Program.SQL.GetnCharNoFromsID(SelectedValue)); }
                else { Program.SQL.AddAmountForCC(C.TableName, Convert.ToInt64(Amount.Value), "nUserNo", Program.SQL.GetnEMIDFromsUsername(SelectedValue)); }
            }

            Value_TextChanged(null, null);
        }
    }
}
