using System;
using System.Windows.Forms;

namespace HwComWrapperNETGui.UserControls
{
    public partial class MsgCat00 : UserControl
    {
        #region Constructors and Destructors

        public MsgCat00()
        {
            this.InitializeComponent();

            this.button01.Click += (sender, args) => this.HandleButtonClick(this.ButtonGetVersionClicked);
            this.button02.Click += (sender, args) => this.HandleButtonClick(this.ButtonGetConnectedHardwareClicked);
            this.button05.Click += this.OnButtonLockHardwareClicked;
            this.button06.Click += this.OnButtonUnlockHardwareClicked;
        }
        
        #endregion

        #region Public Events

        /// <summary>
        ///     Occurs when the get button for COM_GetConnectedHwReq was clicked. 
        /// </summary>
        public event EventHandler ButtonGetConnectedHardwareClicked;
        /// <summary>
        ///     Occurs when the get button for COM_VersionReq was clicked. 
        /// </summary>
        public event EventHandler ButtonGetVersionClicked;

        /// <summary>
        ///     Occurs when the get button for COM_LockHwReq was clicked. 
        /// </summary>
        public event EventHandler<LockHardwareEventArgs> ButtonLockHardwareClicked;

        /// <summary>
        ///     Occurs when the get button for COM_UnlockHwReq was clicked. 
        /// </summary>
        public event EventHandler<LockHardwareEventArgs> ButtonUnlockHardwareClicked;

        #endregion

        #region Methods

        private void HandleButtonClick(EventHandler handler)
        {
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        private void OnButtonLockHardwareClicked(object sender, EventArgs eventArgs)
        {
            var handler = this.ButtonLockHardwareClicked;
            if (handler != null)
            {
                handler(
                    this,
                    new LockHardwareEventArgs
                    {
                        HardwareId = Convert.ToByte(this.numericUpDownHwID.Value),
                        Timeout = Convert.ToByte(this.numericUpDownTimeout.Value)
                    });
            }
        }

        private void OnButtonUnlockHardwareClicked(object sender, EventArgs eventArgs)
        {
            var handler = this.ButtonUnlockHardwareClicked;
            if (handler != null)
            {
                handler(
                    this,
                    new LockHardwareEventArgs
                    {
                        HardwareId = Convert.ToByte(this.numericUpDownHwID.Value),
                        Timeout = 0
                    });
            }
        }

        #endregion
    }
}