﻿using Lovense.UnityKit;
using Lovense.UnityKit.Dongle;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class DongleLovenseFeedback : MonoBehaviour
{
    List<LovenseDongleToy> toys;
    public DongleFeedbackItem cloneObj;
    public Transform root;
    private List<DongleFeedbackItem> uiList = new List<DongleFeedbackItem>();
    private void Start()
    {
        toys = DongleGlobal.GetInstance().thisToys;
        if (toys != null && toys.Count == 1)
        {
            InitToy();
        }
        else
        {
            InitToys();
        }
       // LovenseDongleTools.GetInstance().SetFeedbackMode(toy.id, true);
    }

    public void InitToy()
    {
        if (toys[0].isSupportDataReporting)
        {
            DongleFeedbackItem item = GameObject.Instantiate<DongleFeedbackItem>(cloneObj, root);
            if (item != null)
            {
                item.SetData(toys[0]);
                uiList.Add(item);
            }
        }
    }

    public void InitToys()
    {
        foreach(LovenseDongleToy toy in toys)
        {
            if (toy.isSupportDataReporting)
            {
                DongleFeedbackItem item = GameObject.Instantiate<DongleFeedbackItem>(cloneObj, root);
                if (item != null)
                {
                    item.SetData(toy);
                    uiList.Add(item);
                }
            }
        }
    }

    public void ClickOnBack()
    {
        SceneManager.LoadScene("LovenseDongle1");
    }

    private void OnDestroy()
    {
        for(int i=0;i < uiList.Count;i++)
        {
            DongleFeedbackItem item = uiList[i];
            item.OnParentDestroy();
        }
    }
}
