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

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

    [SerializeField]
    public Text deviceName, deviceId, deviceRssi, deviceStatus;
    [SerializeField]
    public Toggle toggle;
    [SerializeField]
    public bool isShow = true;

    LovenseiOSToy toy;

    void Start()
    {
        if (!isShow)
        {
            this.gameObject.SetActive(false);
        }
    }

    private void LateUpdate()
    {
      
    }

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

    public void SetToys(LovenseiOSToy ty)
    {
        this.toy = ty;
        deviceName.text = ty.name;
        deviceId.text = ty.identifier;
        deviceRssi.text = ty.rssi+"";
        deviceStatus.text = (ty.isConnected? "connected" : "not connected");
        

        if (this.toy.isConnected)
        {
            deviceStatus.text = ("connected");
        } else
        {
            deviceStatus.text = ("not connected");

        }
        toggle.gameObject.SetActive(true);
        LovenseiOSSDK.oniOSConnectEvent.RemoveListener(onToysConnectEvent);
        LovenseiOSSDK.oniOSConnectEvent.AddListener(onToysConnectEvent);

    }

    public void onToysConnectEvent(string toyId, int status, string msg)
    {
        if (this.toy != null && toyId == this.toy.identifier)
        {
            if (status == 0)
            {
                SetConnectStatus(false);

            }
            else if (status == 1)
            {
                SetConnectStatus(true);
            }
            else if (status == -1)
            {
                SetConnectStatus(false);
            }
        }
    }

   

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

    public void onClick()
    {
        iOSGlobal.GetInstance().thisToy = this.toy;
        SceneManager.LoadScene("LovenseiOSScene1");
    }

    public void OnCheckClick(bool status)
    {
        
    }


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

    public string GetToyId()
    {
        if (this.toy != null)
        {
            return this.toy.identifier;
        }
        return "";
    }

    public string GetToyName()
    {
        if (this.toy != null)
        {
            return this.toy.name;
        }
        return "";
    }


}
