using Connection.Class.GATT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WBP_Office_Vascular_API_Demo_Customer {

    public partial class Form_BLE_Discover : Form {
        public string BLE_ID { get; set; }
        public BLEAction BLEAction { get; set; }

        private delegate void DoActionHandler(ListBox lb, Action action);

        public Form_BLE_Discover() {
            InitializeComponent();
            Connection.BLE_Scanner.InitScan(new List<string>(new string[] { "Office Vascular" }));

            Connection.BLE_Scanner.DeviceAdded += OnDeviceAdded;
            Connection.BLE_Scanner.DeviceRemoved += OnDeviceRemoved;
            Connection.BLE_Scanner.SignalStrengthUpdated += _DeviceWatcher_SignalStrengthUpdated;
            Connection.BLE_Scanner.StartScan();
        }

        private void _DeviceWatcher_SignalStrengthUpdated(object sender, Dictionary<ulong, int> e) {
        }

        private void OnDeviceRemoved(object sender, DeviceRemovedEventArgs e) {
            UpdateUI(listBox1, () => {
                for (int i = 0; i < listBox1.Items.Count; i++) {
                    if (listBox1.Items[i].ToString().Contains(e.Device.Id)) {
                        listBox1.Items.Remove(listBox1.Items[i]);
                        return;
                    }
                }
            });
        }

        private void OnDeviceAdded(object sender, DeviceAddedEventArgs e) {
            UpdateUI(listBox1, () => listBox1.Items.Add($"{(e.Device.IsPaired ? "!" : "@")}{e.Device.Name} -- {e.Device.Id} -- {e.Device.Id}"));
        }

        public static void UpdateUI(ListBox tb, Action action) {
            //Determine whether this TextBox object is on the same thread.
            if (tb.InvokeRequired) {
                //When InvokeRequired is true, it means the code is running on a different thread, so delegation is required!

                DoActionHandler ph = new DoActionHandler(UpdateUI);
                tb.Invoke(ph, tb, action);
            } else {
                //It means it's on the same thread, so we can directly call this TextBox object
                action.Invoke();
            }
        }

        private void Button1_Click(object sender, EventArgs e) {
            if (listBox1.SelectedItem == null)
                return;
            BLE_ID = listBox1.SelectedItem.ToString().Split(new string[] { " -- " }, StringSplitOptions.RemoveEmptyEntries).Last();
            BLEAction = BLEAction.Connect;
            Close();
        }

        private void Button2_Click(object sender, EventArgs e) {
            BLEAction = BLEAction.Disconnect;
            Close();
        }

        private void Button3_Click(object sender, EventArgs e) {
            if (listBox1.SelectedItem == null)
                return;
            BLE_ID = listBox1.SelectedItem.ToString().Split(new string[] { " -- " }, StringSplitOptions.RemoveEmptyEntries).Last();
            BLEAction = BLEAction.Pair;
            Close();
        }

        private void Button4_Click(object sender, EventArgs e) {
            if (listBox1.SelectedItem == null)
                return;
            BLE_ID = listBox1.SelectedItem.ToString().Split(new string[] { " -- " }, StringSplitOptions.RemoveEmptyEntries).Last();
            BLEAction = BLEAction.Unpair;
            Close();
        }
    }

    public enum BLEAction {
        None,
        Connect,
        Disconnect,
        Pair,
        Unpair
    }
}