using System;
using System.Windows.Forms;

namespace HwComWrapperNETGui.UserControls
{
    // ReSharper disable LocalizableElement
    public partial class MsgCat82 : UserControl
    {
        #region Constructors and Destructors

        public MsgCat82()
        {
            this.InitializeComponent();

            this.button01.Click += this.OnButtonChangeSignalClicked;
            this.button02.Click += this.OnButtonChangePDUClicked;
            this.button03.Click += (sender, args) => this.HandleButtonClick(this.ButtonChangeMessageClicked);
            this.button04.Click += this.OnButtonChangeControlClicked;
        }

        #endregion

        #region Public Events

        /// <summary>
        ///     Occurs when the button for COM_ChangeControlReq was clicked. 
        /// </summary>
        public event EventHandler<ChangeSignalPDUControlEventArgs> ButtonChangeControlClicked;

        /// <summary>
        ///     Occurs when the button for COM_ChangeMsgReq was clicked. 
        /// </summary>
        public event EventHandler ButtonChangeMessageClicked;

        /// <summary>
        ///     Occurs when the button for COM_ChangePDUReq was clicked. 
        /// </summary>
        public event EventHandler<ChangeSignalPDUControlEventArgs> ButtonChangePDUClicked;

        /// <summary>
        ///     Occurs when the button for COM_ChangeSignalReq was clicked. 
        /// </summary>
        public event EventHandler<ChangeSignalPDUControlEventArgs> ButtonChangeSignalClicked;

        #endregion

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

        private void OnButtonChangeControlClicked(object sender, EventArgs e)
        {
            var handler = this.ButtonChangeControlClicked;
            if (handler == null)
            {
                return;
            }

            var id = Convert.ToUInt64(this.numericUpDownControlId.Value);

            // ChangeControl in byte
            var byteLength = Convert.ToUInt16(this.numericUpDownControlLength.Value);
            var value = this.numericUpDownControlValue.Value.GetByteArray(byteLength);
            if (value == null)
            {
                return;
            }

            handler(this, new ChangeSignalPDUControlEventArgs(id, byteLength, value));
        }

        private void OnButtonChangePDUClicked(object sender, EventArgs e)
        {
            var handler = this.ButtonChangePDUClicked;
            if (handler == null)
            {
                return;
            }

            var id = Convert.ToUInt64(this.numericUpDownPDUId.Value);

            // ChangePdu in Byte
            var byteLength = Convert.ToUInt16(this.numericUpDownPDULength.Value);
            var value = this.numericUpDownPDUValue.Value.GetByteArray(byteLength);
            if (value == null)
            {
                return;
            }

            handler(this, new ChangeSignalPDUControlEventArgs(id, byteLength, value));
        }

        private void OnButtonChangeSignalClicked(object sender, EventArgs e)
        {
            var handler = this.ButtonChangeSignalClicked;
            if (handler == null)
            {
                return;
            }

            var id = Convert.ToUInt64(this.numericUpDownSignalId.Value);

            // Change Signal in bit!
            var bitLength =  Convert.ToUInt16(this.numericUpDownSignalLength.Value);

            // GetByteArray must be called with the count of bytes
            var value = this.numericUpDownSignalValue.Value.GetByteArray(bitLength.GetByteLength());
            if (value == null)
            {
                return;
            }

            handler(this, new ChangeSignalPDUControlEventArgs(id, bitLength, value));
        }

        #endregion
    }
}