/***************************************************************************
 * Copyright (c) 2009 Digi International Inc., All Rights Reserved
 * 
 * This software contains proprietary and confidential information of Digi
 * International Inc.  By accepting transfer of this copy, Recipient agrees
 * to retain this software in confidence, to prevent disclosure to others,
 * and to make no use of this software other than that for which it was
 * delivered.  This is an unpublished copyrighted work of Digi International
 * Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 * prohibited.
 * 
 * Restricted Rights Legend
 *
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
 * Technical Data and Computer Software clause at DFARS 252.227-7031 or
 * subparagraphs (c)(1) and (2) of the Commercial Computer Software -
 * Restricted Rights at 48 CFR 52.227-19, as applicable.
 *
 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 *
 ***************************************************************************
 *
 * @Descr: FormChat Class
 *
 * @History:
 *    10/01/09:   gsarabia      initial version.
 *
 ***************************************************************************/

#region Namespace Inclusions

using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using Digi.ZigBee;

#endregion

namespace ChatApplication
{
    public partial class FormChat : Form
    {

        #region Variables

        XBeeDeviceManager MyXBeeDeviceManager = new XBeeDeviceManager("COM1", BaudRateEnum._9600);

        private Encoding InternalEncoding = Encoding.ASCII;

        #endregion

        #region Constructor

        public FormChat()
        {
            InitializeComponent();
            foreach (string PortName in SerialPort.GetPortNames())
            {
                ComboBoxCOMPort.Items.Add(PortName);
            }
            if (ComboBoxCOMPort.Items.Contains("COM1"))
            {
                ComboBoxCOMPort.Text = "COM1";
            }
            else
            {
                if (ComboBoxCOMPort.Items.Count > 0)
                {
                    ComboBoxCOMPort.SelectedIndex = 0;
                }
                else
                {
                    MessageBox.Show("There are no COM Ports detected on this computer.", "No COM Ports Installed", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                }
            }
        }

        #endregion

        #region ZigBee Receive Packet

        public void PacketReceived(XBeeDeviceManager sender, ulong? IEEEAddress, ushort? NetworkAddress, Rx_Options? Options, byte[] ReceivedData)
        {
            TextBoxLog.Text += "<- " + InternalEncoding.GetString(ReceivedData, 0, (int)ReceivedData.Length) + "\r\n";
        }

        #endregion

        #region Events

        private void ButtonSend_Click(object sender, EventArgs e)
        {
            if (TextBoxSend.Text != "")
                MyXBeeDeviceManager.Transmit(TextBoxSend.Text);
            TextBoxLog.Text += "-> " + TextBoxSend.Text + "\r\n";
            TextBoxSend.Text = "";
            TextBoxSend.Focus();
        }

        private void ButtonOpenPort_Click(object sender, EventArgs e)
        {
            MyXBeeDeviceManager = new XBeeDeviceManager(ComboBoxCOMPort.Text, BaudRateEnum._9600);
            MyXBeeDeviceManager.Open();
            MyXBeeDeviceManager.ReceivePacket += new XBeeDeviceManager.ReceivePacketEventHandler(PacketReceived);
            MyXBeeDeviceManager.DestinationAddress(MyXBeeDeviceManager.NetworkDevices[0].IEEEAddress);
            ButtonOpenPort.Enabled = false;
            ComboBoxCOMPort.Enabled = false;
            LabelCOMPort.Enabled = false;
            TextBoxLog.Enabled = true;
            TextBoxSend.Enabled = true;
            ButtonSend.Enabled = true;
        }

        #endregion
    }
}