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

public class PatternV2Panel : MonoBehaviour
{

    [SerializeField]
    public Text syncTimeText;
    [SerializeField]
    Slider intervalValue, intervalTime, loopTimes;

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

    Func<List<string>> getCurrentToys;
    private float syncTime;
    void Start()
    {
        LovenseWinBleTools.onPatternV2Event.AddListener(OnGetPatternV2Event);
    }

    private void OnGetPatternV2Event(PatternV2ActionType type, string id, int code)
    {
        if (type == PatternV2ActionType.SETUP)
        {
            if (code == 0)
            {
                Debug.Log("patternV2 status:Stoped");
            }
            else if (code == 1)
            {
                Debug.Log("patternV2 status:Pause");
            }
            else if (code == 2)
            {
                Debug.Log("patternV2 status:Playing");
            }
            else if (code == 3)
            {
                Debug.Log("patternV2 status:Playing, but the series value of PatternV2 is empty");
            }
        }
        else if (type == PatternV2ActionType.PLAY_OR_STOP)
        {
            Debug.Log("executed play/stop success");
        }
    }

    public void SetGetToysCallback(Func<List<string>> get)
    {
        getCurrentToys = get;
    }

    public void ClickOnSetup()
    {
        int startValue = 0;
        int startTime = 100;
        addPatternList = new List<PositionPatternValue>();
        for (int i = 0; i < loopTimes.value; i++)
        {
            startValue += (int)intervalValue.value;
            PositionPatternValue position = new PositionPatternValue();
            position.time = startTime;
            position.position = startValue % 100;
            if (position.position == 0)
            {
                position.position = 10;

            }
            addPatternList.Add(position);
            startTime += (int)intervalTime.value;
        }
        List<string> toys = getCurrentToys();
        LovenseWinBleTools.GetInstance().PatternV2SetUp(toys[0], addPatternList);
    }



    public void ClickOnStart()
    {
        List<string> toys = getCurrentToys();
        LovenseWinBleTools.GetInstance().PlayPatternV2(toys[0]);
    }

    public void ClickOnStop()
    {
        List<string> toys = getCurrentToys();
        LovenseWinBleTools.GetInstance().StopPatternV2(toys[0]);
    }
}

