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

public class LovenseDongleController : MonoBehaviour
{
    private static LovenseDongleController Instance;

    [SerializeField]
    public VerticalLayoutGroup  toyVlg;

    [SerializeField]
    public Slider timeSecPSlider, oneTimeSlider;

    

    public GameObject usbDetailsPanel;
    [SerializeField]
    public GameObject toysScrollView,toyObj;
    [SerializeField]
    public Text errorText, usbDetails;
    [SerializeField]
    public GameObject errorPanel;
    public List<ScrollItemClick> uiList = new List<ScrollItemClick>();

    public static LovenseDongleController GetInstancce()
    {
        return Instance;
    }
    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        errorPanel.SetActive(false);
        foreach (Transform child in toyVlg.transform)
        {
            Destroy(child.gameObject);
        }
        uiList.Clear();
        for (int i=0;i < LovenseDongleTools.allLovenseToys.Count;i ++)
        {
            LovenseDongleToy toy = LovenseDongleTools.allLovenseToys[i];
            GameObject newObj = GameObject.Instantiate(toyObj, toyVlg.transform);
            newObj.SetActive(true);
            ScrollItemClick sItem = newObj.GetComponent<ScrollItemClick>();
            sItem.SetToyData(toy);
            uiList.Add(sItem);
        }
        LovenseDongleTools.onGetToyEvent.AddListener(onGetToy);
        LovenseDongleTools.onUsbInfoGetEvent.AddListener(OnUsbInfoGet);
        LovenseDongleTools.onResultCodeEvent.AddListener(OnErrorCodeResult);
        //LovenseDongleTools.onConnectChangeEvent.AddListener(OnConnectedChange);
        //LovenseDongleTools.onGetToyDataReportingEnabled.AddListener(OnGetToyReportingEnabled);
        LovenseDongleTools.GetInstance().GetHIDInfo();
    }

    private void OnDestroy()
    {
        LovenseDongleTools.onGetToyEvent.RemoveListener(onGetToy);
        LovenseDongleTools.onUsbInfoGetEvent.RemoveListener(OnUsbInfoGet);
        LovenseDongleTools.onResultCodeEvent.RemoveListener(OnErrorCodeResult);
    }


    public void onGetToy(LovenseDongleToy data)
    {
        GameObject newObj = GameObject.Instantiate(toyObj, toyVlg.transform);
        newObj.SetActive(true);
        ScrollItemClick sItem = newObj.GetComponent<ScrollItemClick>();
        sItem.SetToyData(data);
        uiList.Add(sItem);
    }

    public void OnUsbInfoGet(LovenseUsbData data)
    {
        usbDetails.text = "usb:" + data.deviceType + ",batchId:" + data.batchId;
    }

    private void OnErrorCodeResult(int code)
    {

        if (code == 99)
        {
            ShowError("haven no dongle.");
        }
        else if (code == 100)
        {
            ShowError("code:" + 100 + ",device init completed.");
        }
        else if (code == 200) 
        {
            //ShowError("code:" + result.code + ",Invalid Command.");
        }
        else if (code == 201) //
        {
        }
        else if (code == 202) //
        {
        }
        else if (code == 206) //ɨɹ
        {
        }
        else if (code == 400)
        {
            ShowError("code:" + code + ",the command is not support.");
        }
        else if (code == 401)
        {
            ShowError("code:" + code + ",the params is error.");
        }
        else if (code == 402)
        {
            ShowError("code:" + code + ",the json format is error");
        }
        else if (code == 403)
        {
            ShowError("code:" + code + ",the toy is not connected.");
        }
        else if (code == 404)
        {
            ShowError("code:" + code + ",the device is not not exists.");
        }
        else if (code == 406)
        {
            ShowError("code:" + code + ",it's full on max connectting times");
        }
        else if (code == 501)
        {
            ShowError("code:" + code + ",scanning now,part of command is not support");
        }
        else if (code == 505)
        {
            ShowError("code:" + code + ",other errors");
        }
    }

    public void CloseErrorPanel()
    {
        errorText.text = "";
        errorPanel.SetActive(false);
    }

    public void ShowError(string msg)
    {
        errorPanel.SetActive(true);
        errorText.text = msg;
        
    }

    public void OnSearch()
    {
        LovenseDongleTools.GetInstance().GetConnectedToys();

        LovenseDongleTools.GetInstance().SearchToys();
    }

    public void OnStopSearch()
    {
        LovenseDongleTools.GetInstance().StopSearch();
    }

    public void OnControlClick()
    {
        List<LovenseDongleToy> datas = GetSelected();
        if(datas.Count ==0)
        {
            ShowError("please select toy first.");
            return;
        }
        if (LovenseDongleTools.GetInstance().isScanning())
        {
            ShowError("scanning toys now!please try later.");
            return;
        }
        DongleGlobal.GetInstance().thisToys = datas;
        SceneManager.LoadScene("LovenseDongle1");
    }

    private List<LovenseDongleToy> GetSelected()
    {
        List<LovenseDongleToy> datas = new List<LovenseDongleToy>();
        for (int i = 0; i < uiList.Count; i++)
        {
            if (uiList[i].IsCheck())
            {
                datas.Add(uiList[i].GetToyData());
            }
        }
        return datas;
    }

  
    public void OnChooseToysClick()
    {
        toysScrollView.SetActive(true);
    }
}
