using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SensorManager
{
    public partial class frmMain : Form
    {
        private const string MSG_ERROR_START = "Error - ";
        private const string MSG_NO_SENSOR_CONNECTED = "No sensor connected";
        LB_API2_Declarations.SDByte[] sensDesc;

        public frmMain()
        {
            InitializeComponent();
        }

        public void RefreshList()
        {
            // get the number of sensors...
            int cnt = LB_API2_Declarations.LB_SensorCnt();
            lstLB_SensorList.Items.Clear();
            if (cnt > 0)
            {
                // declare and array of sensor descriptions
                sensDesc = new LB_API2_Declarations.SDByte[cnt];
                // get the descriptoins
                int rslt = LB_API2_Declarations.LB_SensorList(ref sensDesc[0], cnt);
                // if we got descriptions then put them in the list box for all to see
                if (rslt >= 1)
                {
                    for (int i = 0; i < cnt; i++)
                    {
                        string strSensDesc = string.Concat(string.Format("{0,3:d}   {1,3:d}   ",
                                        sensDesc[i].DeviceIndex, sensDesc[i].DeviceAddress), sensDesc[i].SN());
                        lstLB_SensorList.Items.Add(strSensDesc);
                    }
                }
                else
                {
                    lstLB_SensorList.Items.Add(string.Concat(MSG_ERROR_START, rslt.ToString()));
                }
            }
            else
            {
                lstLB_SensorList.Items.Add(MSG_NO_SENSOR_CONNECTED);
            }
            if (lstLB_SensorList.Items.Count > 0)
            {
                lstLB_SensorList.SelectedIndex = 0;
                lblFromAddress.Text = sensDesc[0].DeviceAddress.ToString();
            }
        }

        private void btnRefreshList_Click(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void btnBlinkLED_Click(object sender, EventArgs e)
        {
            int rslt = LB_API2_Declarations.LB_BlinkLED_Addr(sensDesc[lstLB_SensorList.SelectedIndex].DeviceAddress);
        }

        private void btnChangeAddr_Click(object sender, EventArgs e)
        {
            if (lstLB_SensorList.SelectedIndex >= 0)
            {
                int newAddress = Convert.ToInt32(udNewAddress.Value);
                int oldAddrss = sensDesc[lstLB_SensorList.SelectedIndex].DeviceAddress;
                int rslt = LB_API2_Declarations.LB_ChangeAddress(oldAddrss, newAddress);
                if (rslt > 0)
                {
                    RefreshList();
                }
                else
                {
                    MessageBox.Show("Error in trying the change the address", "Sensor Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void lstLB_SensorList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstLB_SensorList.SelectedIndex>=0)
                lblFromAddress.Text = sensDesc[lstLB_SensorList.SelectedIndex].DeviceAddress.ToString();
        }
    }
}