using Assets.Scripts.Devices.Ant;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class DeviceController : PFUIPanel
{
    public Toggle toggle;
    private float timer = 1.0f;
    protected override void Awake()
    {
        AntConnector.Instance();
    }

    // Start is called before the first frame update
    protected override void Start()
    {
        base.Start();
        Button button = this.transform.Find("Button").GetComponent<Button>();
        button.onClick.AddListener(() =>
        {
            UIManager.ShowHomePanel();
        });

        this.toggle = this.transform.Find("Toggle").GetComponent<Toggle>();
        this.toggle.onValueChanged.AddListener(s =>
        {
            if (s)
            {
                this.toggle.transform.GetComponentInChildren<UnityEngine.UI.Text>().color = Color.green;
            }
            else
            {
                this.toggle.transform.GetComponentInChildren<UnityEngine.UI.Text>().color = Color.gray;
            }
        });
    }

    // Update is called once per frame
    void Update()
    {
        timer -= Time.deltaTime;
        if (timer <= 0)
        {
            this.toggle.isOn = AntConnector.Instance().IsAvailable;

            //foreach (var item in AntConnector.Instance().discoveredDevices)
            //{
            //    if (!devices.ContainsKey(item.DeviceNumber))
            //    {
            //        var dev1 = Instantiate(device);
            //        dev1.transform.SetParent(this.grid.transform);
            //        var s1 = dev1.GetComponent<Assets.Scripts.UI.Devicea>();
            //        s1.Device = item;

            //        devices.Add(item.DeviceNumber, dev1);
            //    }
            //}

            timer = 1.0f;
        }
    }
}
