﻿using Lovense.UnityKit.Android;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PatternPanel : MonoBehaviour
{

    [SerializeField]
    public Toggle vibrateTog, rotateTog, pumpTog, thrustTog, fingerTg,oscillateTg,posTg;

    [SerializeField]
    public Slider pattern_ms, pattern_sec;

    [SerializeField]
    public SliderItem addPatternObj;
    [SerializeField]
    public Transform patternContent;

    Func<List<string>> getToysCallback;

    List<SliderItem> addPatternList = new List<SliderItem>();

    // Start is called before the first frame update
    void Start()
    {
        InitSupport();
    }

    public void InitSupport()
    {
        List<string> toys = getToysCallback();
        List<LovenseCommandType> supported = new List<LovenseCommandType>();
        foreach (string toyId in toys)
        {
            List<LovenseCommandType> temp = LovenseAndroidSDK.GetInstance().GetSupportedCommands(toyId);
            for (int i = temp.Count - 1; i >= 0; i--)
            {
                if (!supported.Contains(temp[i]))
                {
                    supported.Add(temp[i]);
                }
            }
        }
        vibrateTog.gameObject.SetActive(supported.Contains(LovenseCommandType.VIBRATE));
        rotateTog.gameObject.SetActive(supported.Contains(LovenseCommandType.ROTATE));
        pumpTog.gameObject.SetActive(supported.Contains(LovenseCommandType.PUMP));
        thrustTog.gameObject.SetActive(supported.Contains(LovenseCommandType.THRUSTRING));
        fingerTg.gameObject.SetActive(supported.Contains(LovenseCommandType.FINGERING));
        oscillateTg.gameObject.SetActive(supported.Contains(LovenseCommandType.OSCILLATE));
        posTg.gameObject.SetActive(supported.Contains(LovenseCommandType.SET_POSITION));

    }

    public void SetGetToysCallback(Func<List<string>> arg0)
    {
        getToysCallback = arg0;
    }

    public void OnSendPattern()
    {
            List<LovenseCommandType> typeList = new List<LovenseCommandType>();
            if (vibrateTog.isOn)
            {
                typeList.Add(LovenseCommandType.VIBRATE);
            }
            if (rotateTog.isOn)
            {
                typeList.Add(LovenseCommandType.ROTATE);
            }
            if (pumpTog.isOn)
            {
                typeList.Add(LovenseCommandType.PUMP);
            }
            if (thrustTog.isOn)
            {
                typeList.Add(LovenseCommandType.THRUSTRING);
            }
            if (fingerTg.isOn)
            {
                typeList.Add(LovenseCommandType.FINGERING);
            }
            if (posTg.isOn)
            {
                typeList.Add(LovenseCommandType.SET_POSITION);
            }


        List<int> addList = new List<int>();
            for (int i = 0; i < addPatternList.Count; i++)
            {
                int add = addPatternList[i].GetValue();
                addList.Add(add);
            }

            if (addList.Count < 1)
            {
                LovenseAndroidSDK.GetInstance().ShowToast("please add the pattern");
                return;
            }

        List<string> ids = getToysCallback();
                //for (int i = 0; i < checkItems.Count; i++)
                //{
                //    if (checkItems[i].IsCheck())
                //    {
                //        ids.Add(checkItems[i].GetToyId());
                //    }
                //}
        if (ids.Count == 0)
        {
            LovenseAndroidSDK.GetInstance().ShowToast("please select toys");
            return;
        }
        LovenseAndroidSDK.GetInstance().SendPattern(ids.ToArray(), typeList, addList, pattern_sec.value, (int)pattern_ms.value);
    }

    public void AddPattern()
    {
        if (patternContent.childCount - 2 >= 50)
        {
            LovenseAndroidSDK.GetInstance().ShowToast("must be less than 50");
            return;
        }
        SliderItem newPattern = Instantiate(addPatternObj, patternContent);
        addPatternList.Add(newPattern);
        newPattern.gameObject.SetActive(true);
        newPattern.transform.SetSiblingIndex(patternContent.childCount - 2);
    }

    public void ReducePattern()
    {
        if (addPatternList.Count > 0)
        {
            SliderItem del = addPatternList[addPatternList.Count - 1];
            del.transform.SetParent(null);
            Destroy(del);
            addPatternList.RemoveAt(addPatternList.Count - 1);
        }
    }

}
