using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Windows; using System.Windows.Controls; namespace CryptoCalc { public partial class MainWindow : Window { private readonly Random rand = new(); private readonly DBInteraction DB = new(); private readonly DBInteraction transactionDB = new(); private readonly DBInteraction walletDB = new(); private readonly Transaction genericT = new(); private readonly Wallet genericW = new(); private List walletList = new(); private Wallet selectedWallet = new(); public MainWindow() { InitializeComponent(); //Create the DB file if none exist DB.CreateDB(); //Create tables in DB if none exist transactionDB.ExecuteQuery(genericT, genericT.CreateTable(genericW.DBTableName)); walletDB.ExecuteQuery(genericW, genericW.CreateTable()); } private void SaveRandomTransactionButton_Click(object sender, RoutedEventArgs e) { transactionDB.SaveData(DummyTransaction()); } private void SaveRandomWalletButton_Click(object sender, RoutedEventArgs e) { walletDB.SaveData(DummyWallet()); } private void SaveTransactionButton_Click(object sender, RoutedEventArgs e) { transactionDB.SaveData ( new Transaction( (DateTime)insertTransactionDatePicker.SelectedDate, selectedWallet.Index, inputCurrency.Text, Convert.ToDecimal(inputAmount.Text, CultureInfo.InvariantCulture), inputType.Text ) ); } private void SaveWalletButton_Click(object sender, RoutedEventArgs e) { walletDB.SaveData( new Wallet( (DateTime)inputWalletCreationDate.SelectedDate, inputWalletPlatform.Text, inputWalletName.Text, inputWalletCurrency.Text, inputWalletNote.Text ) ); } private void PrintToConsoleButton_click(object sender, RoutedEventArgs e) { string query = $"select * from {genericT.DBTableName}"; IEnumerable transactions = transactionDB.ReturnQuery(genericT, query); foreach (Transaction t in transactions) { Debug.WriteLine($"{DBClasses.GetLocalTimeFromUnixTime(t.UnixTime)} *** {t.Currency} - {Util.CombineDecimal(t.Amount, t.AmountDecimal)}"); } } private void ShowTransactionsOfCurrencyButton_Click(object sender, RoutedEventArgs e) { string query = $"SELECT * from {genericT.DBTableName} WHERE {nameof(genericT.Currency)}=\'{currencyText.Text}\'"; IEnumerable transactions = transactionDB.ReturnQuery(genericT, query); transactionsFoundListBox.Items.Clear(); foreach (Transaction x in transactions) { transactionsFoundListBox.Items.Add(x.FullInfo); } } private void Show_Wallets_Click(object sender, RoutedEventArgs e) { selectedWallet = null; walletList.Clear(); string query = $"SELECT * from {genericW.DBTableName}"; IEnumerable wallets = walletDB.ReturnQuery(genericW, query); listBox_Wallets.Items.Clear(); foreach (Wallet wallet in wallets) { walletList.Add(wallet); listBox_Wallets.Items.Add($"{wallet.Name}-{wallet.Currency}-{wallet.Balance}"); } } private Transaction DummyTransaction() { Transaction t = new(); t.WalletID = selectedWallet.Index; t.SaveUnixTimeNow(); t.Currency = "SOL"; (t.Amount, t.AmountDecimal) = Util.SplitDecimal(Convert.ToDecimal(30m * (decimal)rand.NextDouble())); t.TransactionType = "BUY"; t.Platform = "Firi"; t.Note = "Test"; return t; } private Wallet DummyWallet() { Wallet w = new(); w.SaveUnixTimeNow(); w.UnixTimeCreated = DBClasses.GetUnixTime(new DateTime(rand.Next(2011, DateTime.Now.Year), rand.Next(1, 12), rand.Next(1, 28))); w.Platform = "Ledger"; w.Name = "DefaultBTCWallet"; w.Currency = "BTC"; w.Balance = 0;//Convert.ToSingle(30 * rand.NextDouble()); w.DefaultWallet = 1; w.Note = "Main BTC Wallet"; return w; } private void InputAmount_LostFocus(object sender, RoutedEventArgs e) { //inputAmount.Text = inputAmount.Text.Replace(".", ","); inputAmount.Text = inputAmount.Text.Replace(",", "."); } private void CurrencyText_LostFocus(object sender, RoutedEventArgs e) { currencyText.Text = currencyText.Text.ToUpper(); } private void InputCurrency_LostFocus(object sender, RoutedEventArgs e) { inputCurrency.Text = inputCurrency.Text.ToUpper(); } private void ListBox_Wallets_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { if (listBox_Wallets.Items.Count > 0) { if (listBox_Wallets.SelectedIndex < walletList.Count) { selectedWallet = walletList[listBox_Wallets.SelectedIndex]; if (selectedWallet != null) { Debug.WriteLine($"{selectedWallet.Name} {selectedWallet.Balance}"); } } } else { Debug.WriteLine("No wallet selected yet"); } } } }