using Lovense.UnityKit;
using Lovense.UnityKit.WinBle;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class WinLovenseItem : MonoBehaviour
{
    // Start is called before the first frame update

    [SerializeField]
    public Text deviceName, deviceType, deviceAddress, deviceStatus,deviceBattery;
    [SerializeField]
    public Toggle toggle;

  

    LovenseWinBleToy toy;

    void Awake()
    {
        LovenseWinBleTools.onConnectChangeEvent.AddListener(OnToysConnectedChanged);
        LovenseWinBleTools.onGetBatteryEvent.AddListener(OnGetToysBattery);
    }

    public void OnToysConnectedChanged(string id,bool connected)
    {
        if(toy != null && id == toy.id)
        {
            deviceStatus.text = connected ? "True" : "False";
        }
    }

    public void OnGetToysBattery(string id,string battery)
    {
        if (toy != null && id == toy.id)
        {
            deviceBattery.text =battery;
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void FixedUpdate()
    {
       
    }

    public void SetToys(LovenseWinBleToy ty)
    {
        this.toy = ty;
        deviceName.text = ty.name;
        deviceType.text = ty.typeName;
        deviceAddress.text = ty.address;
        deviceStatus.text = ty.isConnected?"True":"False";
        deviceBattery.text = ty.battery;
        //toggle.gameObject.SetActive(true);
        if (toy.isConnected)
        {
            LovenseWinBleTools.GetInstance().GetBattery(ty.id);
        }


    }

    public void onConnectFunc(string toysId, string status)
    {
        
        //if(toysId != this.toys.toyId)
        //{
        //    return;
        //}
        //switch (status)
        //{
        //    case LovenseAndroidSDK.STATE_CONNECTING:
        //        //Debug.LogError("connecting");
        //        deviceStatus.text = "connecting";
        //        break;
        //    case LovenseAndroidSDK.STATE_CONNECTED:
        //        //EventBus.getDefault().post(new ToyConnectEvent(1, toyId));
        //        //ConnectStatusChange connect = new ConnectStatusChange();
        //        //connect.toyId = toysId;
        //        //connect.status = 1;
        //        //onConnectStatusChange.Invoke(connect);
        //        LovenseAndroidSDK.getInstance().UpdateToysStatus(toysId, 1);
        //        //Debug.LogError("connected");

        //        break;
        //    case LovenseAndroidSDK.STATE_FAILED:
        //        //EventBus.getDefault().post(new ToyConnectEvent(-1, toyId));
        //        //ConnectStatusChange disconnect = new ConnectStatusChange();
        //        //disconnect.toyId = toysId;
        //        //disconnect.status = -1;
        //        //onConnectStatusChange.Invoke(disconnect);
        //        deviceStatus.text = "disconnected";
        //        LovenseAndroidSDK.getInstance().UpdateToysStatus(toysId, 0);

        //        break;
        //    case LovenseAndroidSDK.SERVICE_DISCOVERED:
        //        deviceStatus.text = "connected";
        //        LovenseAndroidSDK.getInstance().UpdateToysStatus(toysId, 1);
        //        break;
        //}
    }

    public void onConnectErrorFunc(string code, string msg)
    {

        //LovenseAndroidSDK.getInstance().ShowToast("error" + code + ":" + msg);
    }

    public void SetConnectStatus(bool isConnect)
    {
        deviceStatus.text = (isConnect ? "connected" : "not connected");
    }

    public void onClick()
    {
        List<LovenseWinBleToy> temp = new List<LovenseWinBleToy>();
        temp.Add(this.toy);
        WinbleGlobal.GetInstance().thisToys = temp;
        SceneManager.LoadScene("LovenseWinScene1");
    }

    public bool isOn()
    {
        return this.toggle.isOn;
    }

    public LovenseWinBleToy GetData()
    {
        return this.toy;
    }

    public void OnCheckClick(bool status)
    {
        if(toggle.isOn)
        {
            //LovenseAndroidSDK.getInstance().connectToy(this.toys.toyId, onConnectListener);
        }
        else
        {
            //LovenseAndroidSDK.getInstance().disConnect(this.toys.toyId);
            //deviceStatus.text = "disconnected";
        }
    } 
}
