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

public class FunctionPanel : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField]
    public Slider vibrate, ratote, pump, thrusting, fingering,position,strokeLow,strokeHeight, oscillate,timeSec, runningTime, pauseTime;
    [SerializeField]
    GameObject patternV2Button;

    Func<List<string>> getToysCallback;
    bool completed = false;

    void Start()
    {
        if(getToysCallback != null)
        {
            OnToysChoose();
        }
        completed = true;
    }
    public void SetGetToysCallback(Func<List<string>> arg0)
    {
        getToysCallback = arg0;
        if(completed)
        {
            OnToysChoose();
        }
    }

    private void OnToysChoose()
    {
        List<string> toys = getToysCallback();
        List<LovenseCommandType> commandTypes = new List<LovenseCommandType>();

        for (int i = 0; i < toys.Count; i++)
        {
            List<LovenseCommandType> temp = LovenseWinBleTools.GetInstance().GetSupportById(getToysCallback()[0]);
            foreach (LovenseCommandType commandType in temp)
            {
                if (!commandTypes.Contains(commandType))
                {
                    commandTypes.Add(commandType);
                }
            }
        }
        if (commandTypes.Contains(LovenseCommandType.POSITION))
        {
            patternV2Button.SetActive(true);
        }
        else
        {
            patternV2Button.SetActive(false);
        }
        ratote.gameObject.SetActive(commandTypes.Contains(LovenseCommandType.ROTATE));
        pump.gameObject.SetActive(commandTypes.Contains(LovenseCommandType.PUMP));
        thrusting.gameObject.SetActive(commandTypes.Contains(LovenseCommandType.THRUSTRING));
        fingering.gameObject.SetActive(commandTypes.Contains(LovenseCommandType.FINGERING));
        strokeLow.gameObject.SetActive(commandTypes.Contains(LovenseCommandType.POSITION));
        strokeHeight.gameObject.SetActive(commandTypes.Contains(LovenseCommandType.POSITION));
        position.gameObject.SetActive(commandTypes.Contains(LovenseCommandType.POSITION));
        oscillate.gameObject.SetActive(commandTypes.Contains(LovenseCommandType.OSCILLATE));
    }

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

    public void ClickOnSendFunction()
    {
        List<LovenseCommand> commands = new List<LovenseCommand>();
        int vb = (int)(vibrate.value);
        int rt = (int)(ratote.value);
        int pm = (int)(pump.value);
        int th = (int)(thrusting.value);
        int fg = Convert.ToInt16(fingering.value);
        int low = Convert.ToInt16(strokeLow.value);
        int height = Convert.ToInt16(strokeHeight.value);
        int osc = Convert.ToInt16(oscillate.value);
        float ts = timeSec.value;
        float rt1 = runningTime.value;
        float pt = pauseTime.value;
            if (vb != 0)
            {
                LovenseCommand command = new LovenseCommand();
                command.type = LovenseCommandType.VIBTATE;
                command.value = vb;
                commands.Add(command);
            }

            if (rt != 0)
            {
                LovenseCommand command = new LovenseCommand();
                command.type = LovenseCommandType.ROTATE;
                command.value = rt;
                commands.Add(command);
            }

            if (pm != 0)
            {
                LovenseCommand command = new LovenseCommand();
                command.type = LovenseCommandType.PUMP;
                command.value = pm;
                commands.Add(command);
            }

            if (th != 0)
            {
                LovenseCommand command = new LovenseCommand();
                command.type = LovenseCommandType.THRUSTRING;
                command.value = th;
                commands.Add(command);
            }

            if (fg != 0)
            {
                LovenseCommand command = new LovenseCommand();
                command.type = LovenseCommandType.FINGERING;
                command.value = fg;
                commands.Add(command);
            }
            if (height != 0)
            {
            LovenseSolaceProCommand command = new LovenseSolaceProCommand();
                command.type = LovenseCommandType.POSITION;
                command.strokeLow = low;
                command.strokeHigh = height;
                commands.Add(command);
            }
            if (osc != 0)
            {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.OSCILLATE;
            command.value = osc;
            commands.Add(command);
        }
        LovenseWinBleTools.GetInstance().SendFunctions(getToysCallback(), commands, ts, rt1, pt);
    }

    public void ClickOnPosition()
    {
        int pos = (int)position.value;
        LovenseWinBleTools.GetInstance().SetPosition(getToysCallback()[0],pos);
    }

}
