Korero – Rev 2

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using Korero.Communication;
using Korero.Properties;
using Korero.Selection;
using Korero.Utilities;
using Serilog;

namespace Korero.Economy
{
    public partial class PaymentForm : Form
    {
        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private readonly CancellationToken _cancellationToken;

        private readonly CancellationTokenSource _cancellationTokenSource;

        private readonly MainForm _mainForm;

        private readonly MqttCommunication _mqttCommunication;

        private AvatarSelectionForm _avatarSelectionForm;

        private GroupSelectionForm _groupSelectionForm;

        #endregion

        #region Constructors, Destructors and Finalizers

        public PaymentForm()
        {
            InitializeComponent();

            _cancellationTokenSource = new CancellationTokenSource();
            _cancellationToken = _cancellationTokenSource.Token;
        }

        public PaymentForm(MainForm mainForm, MqttCommunication mqttCommunication) : this()
        {
            _mainForm = mainForm;
            _mqttCommunication = mqttCommunication;

            _mqttCommunication.NotificationReceived += MqttCommunication_NotificationReceived;
        }

        public PaymentForm(string firstName, string lastName, MainForm mainForm, MqttCommunication mqttCommunication) :
            this(mainForm, mqttCommunication)
        {
            textBox2.InvokeIfRequired(firstNameTextBox =>
            {
                textBox4.InvokeIfRequired(lastNameTextBox =>
                {
                    firstNameTextBox.Text = firstName;
                    lastNameTextBox.Text = lastName;
                });
            });

            tabControl1.InvokeIfRequired(tabControl => { tabControl.SelectedTab = tabPage1; });
        }

        public PaymentForm(string groupName, MainForm mainForm, MqttCommunication mqttCommunication) : this(mainForm,
            mqttCommunication)
        {
            textBox1.InvokeIfRequired(groupNameTextBox => { groupNameTextBox.Text = groupName; });

            tabControl1.InvokeIfRequired(tabControl => { tabControl.SelectedTab = tabPage2; });
        }

        /// <summary>
        ///     Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }

            _mqttCommunication.NotificationReceived -= MqttCommunication_NotificationReceived;
            base.Dispose(disposing);
        }

        #endregion

        #region Event Handlers
        private void PaymentForm_Load(object sender, EventArgs e)
        {
            Utilities.WindowState.FormTracker.Track(this);
        }
        private void MqttCommunication_NotificationReceived(object sender, MqttNotificationEventArgs e)
        {
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (_avatarSelectionForm != null)
            {
                return;
            }

            _avatarSelectionForm = new AvatarSelectionForm(_mainForm.MqttCommunication);
            _avatarSelectionForm.AvatarSelected += AvatarSelectionForm_AvatarSelected;
            _avatarSelectionForm.Closing += AvatarSelectionForm_Closing;
            _avatarSelectionForm.Show();
        }

        private void AvatarSelectionForm_AvatarSelected(object sender, AvatarSelectedEventArgs e)
        {
            textBox2.InvokeIfRequired(firstNameTextBox =>
            {
                textBox4.InvokeIfRequired(lastNameTextBox =>
                {
                    firstNameTextBox.Text = e.AvatarSelection.FirstName;
                    lastNameTextBox.Text = e.AvatarSelection.LastName;
                });
            });
        }

        private void AvatarSelectionForm_Closing(object sender, CancelEventArgs e)
        {
            if (_avatarSelectionForm == null)
            {
                return;
            }

            _avatarSelectionForm.AvatarSelected -= AvatarSelectionForm_AvatarSelected;
            _avatarSelectionForm.Closing -= AvatarSelectionForm_Closing;
            _avatarSelectionForm.Dispose();
            _avatarSelectionForm = null;
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            textBox2.InvokeIfRequired(firstNameTextBox =>
            {
                if (string.IsNullOrEmpty(firstNameTextBox.Text))
                {
                    return;
                }

                textBox4.InvokeIfRequired(lastNameTextBox =>
                {
                    if (string.IsNullOrEmpty(lastNameTextBox.Text))
                    {
                        return;
                    }

                    textBox3.InvokeIfRequired(async amountBox =>
                    {
                        if (!int.TryParse(amountBox.Text, out var amount))
                        {
                            return;
                        }

                        var data = new Dictionary<string, string>
                        {
                            {"command", "pay"},
                            {"group", Settings.Default.Group},
                            {"password", Settings.Default.Password},
                            {"amount", amount.ToString()},
                            {"firstname", firstNameTextBox.Text},
                            {"lastname", lastNameTextBox.Text},
                            {"entity", "avatar"}
                        };

                        var callback =
                            await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);

                        if (callback == null || !callback.Success)
                        {
                            if (callback != null)
                            {
                                Log.Warning("Command {Command} has failed with {Error}.",
                                    callback.Command, callback.Error);
                            }
                        }
                    });
                });
            });
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if (_groupSelectionForm != null)
            {
                return;
            }

            _groupSelectionForm = new GroupSelectionForm(_mqttCommunication);
            _groupSelectionForm.GroupSelected += GroupSelectionForm_GroupSelected;
            _groupSelectionForm.Closing += GroupSelectionForm_Closing;
            _groupSelectionForm.Show();
        }

        private void GroupSelectionForm_GroupSelected(object sender, GroupSelectedEventArgs e)
        {
            textBox1.InvokeIfRequired(nameTextBox => { nameTextBox.Text = e.GroupSelection.Name; });
        }

        private void GroupSelectionForm_Closing(object sender, CancelEventArgs e)
        {
            if (_groupSelectionForm == null)
            {
                return;
            }

            _groupSelectionForm.Closing -= GroupSelectionForm_Closing;
            _groupSelectionForm.Dispose();
            _groupSelectionForm = null;
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            textBox1.InvokeIfRequired(groupNameTextBox =>
            {
                if (string.IsNullOrEmpty(groupNameTextBox.Text))
                {
                    return;
                }

                textBox5.InvokeIfRequired(async amountBox =>
                {
                    if (!int.TryParse(amountBox.Text, out var amount))
                    {
                        return;
                    }

                    var data = new Dictionary<string, string>
                    {
                        {"command", "pay"},
                        {"group", Settings.Default.Group},
                        {"password", Settings.Default.Password},
                        {"amount", amount.ToString()},
                        {"target", groupNameTextBox.Text},
                        {"entity", "group"}
                    };

                    var callback =
                        await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);

                    if (callback == null || !callback.Success)
                    {
                        if (callback != null)
                        {
                            Log.Warning("Command {Command} has failed with {Error}.",
                                callback.Command, callback.Error);
                        }
                    }
                });
            });
        }

        #endregion


    }
}

Generated by GNU Enscript 1.6.5.90.