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

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

    [SerializeField]
    public Toggle vibrateTog, ratoteTog, pumpTog, thrustTog, fingerTg;

    [SerializeField]
    public Slider pattern_ms, pattern_sec;

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


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

    Func<List<string>> getToysCallback;

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

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

    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);
        }
    }

    public void OnSendPattern()
    {
            List<LovenseCommandType> typeList = new List<LovenseCommandType>();
            if (vibrateTog.isOn)
            {
                typeList.Add(LovenseCommandType.VIBTATE);
            }
            if (ratoteTog.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);
            }


            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;
            }
            else
            {
                LovenseWinBleTools.GetInstance().SendPattern(getToysCallback(), typeList, addList, pattern_sec.value, (int)pattern_ms.value);
            }
    }


}
