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

public class WinLovenseController : MonoBehaviour
{
    List<WinLovenseItem> uiList = new List<WinLovenseItem>();

    [SerializeField]
    public Transform content;
    [SerializeField]
    public Text title;
    [SerializeField]
    public InputField tokenInput;
    [SerializeField]
    GameObject newToysObj;
    private void Awake()
    {
        title.text = "toys list";
        LovenseWinBleTools.onGetToyEvent.AddListener(OnWinbleAddToy);
        LovenseWinBleTools.onGetToyScanEnd.AddListener(OnWinbleToyScanEnd);
    }

    // Start is called before the first frame update
    void Start()
    {
        for(int i = 0; i < uiList.Count;i++)
        {
            
            Destroy(uiList[i]);
        }
        content.DetachChildren();
        uiList.Clear();
        //title.text = "Searching toys...";
        //LovenseAndroidSDK.getInstance().SearchToys(onSearchToyListener);
        foreach(LovenseWinBleToy toy in LovenseWinBleTools.allToys)
        {
            if (newToysObj != null)
            {
                WinLovenseItem newView = GameObject.Instantiate(newToysObj, content).GetComponent<WinLovenseItem>();
                newView.gameObject.SetActive(true);
                newView.SetToys(toy);
                uiList.Add(newView);
            }
            else
            {
                //Debug.LogError("error");
            }
        }

    }

    private void OnDestroy()
    {
        LovenseWinBleTools.onGetToyEvent.RemoveListener(OnWinbleAddToy);
        LovenseWinBleTools.onGetToyScanEnd.RemoveListener(OnWinbleToyScanEnd);

    }

    public void OnWinbleAddToy(string id,LovenseWinBleToy toy) 
    {
        //GameObject newObject = GameObject.Instantiate(newToysObj, content);
        if (newToysObj != null)
        {
            WinLovenseItem newView = GameObject.Instantiate(newToysObj, content).GetComponent<WinLovenseItem>();
            newView.gameObject.SetActive(true);
            newView.SetToys(toy);
            uiList.Add(newView);
        }
        else
        {
            //Debug.LogError("error");
        }
    }

    public void OnWinbleToyScanEnd()
    {
        title.text = "toys list";
    }


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

    public void UpdateConnectStatus(string toyId, int status)
    {
    } 

    public void onStartScanClick()
    {

        title.text = "Searching toys...";
        LovenseWinBleTools.GetInstance().ScanDevices();
    }

    public void onStopScanClick()
    {
        title.text = "toys list";
        LovenseWinBleTools.GetInstance().StopDeviceScan();
    }

    public void ClickOnControl()
    {
        List<LovenseWinBleToy> toys = new List<LovenseWinBleToy>();
        foreach(WinLovenseItem item in uiList)
        {
            if (item.isOn())
            {
                toys.Add(item.GetData());
            }
        }
        if(toys.Count == 0 )
        {
            return;
        }
        WinbleGlobal.GetInstance().thisToys = toys;
        SceneManager.LoadScene("LovenseWinScene1");
    }
}
