﻿using Lovense.UnityKit.Dongle;
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()
    {
        LovenseDongleTools.onPatternV2Event.AddListener(OnGetPatternV2Event);
    }

    private void OnGetPatternV2Event(LovenseDongleTools.PatternV2ActionType type,string id, int code)
    {
        if(type == LovenseDongleTools.PatternV2ActionType.SETUP)
        {
            if (type == LovenseDongleTools.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 == LovenseDongleTools.PatternV2ActionType.PLAY_OR_STOP)
            {
                Debug.Log("executed play/stop success");
            }
        } else if(type == LovenseDongleTools.PatternV2ActionType.PLAY_OR_STOP)
        {
            Debug.Log("executed play/stop success");
        }
    }

    public void SetCurrentToyCallback(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();
        LovenseDongleTools.GetInstance().PatternV2SetUp(toys[0], addPatternList);
    }

   

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

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

}
