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

public class FunctionPanel : MonoBehaviour
{

    [SerializeField]
    public Slider vibrateFSlider, ratoteFSlider, pumpFSlider, thrustFSlider, fingerFSlider, suctionFSlider,strokeLowFSlider,strokeHeightFSlider,positionFSlider,oscillateFSlider, loopRunFSlider, loopPauseFSlider, timeSecFSlider;
    [SerializeField]
    GameObject btnPosition;
    Func<List<string>> getToysCallback;

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

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

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

    public void Init()
    {
        List<string> toys = getToysCallback();
        List<LovenseCommandType> support = new List<LovenseCommandType>();
        for(int i=0;i < toys.Count;i++)
        {
            List<LovenseCommandType> sup = LovenseDongleTools.GetInstance().GetSupportCommandsByType(LovenseDongleTools.GetInstance().GetTypeById(toys[i]));
            for(int j=0;j< sup.Count;j++)
            {
                if(!support.Contains(sup[j]))
                {
                    support.Add(sup[j]);
                }
            }
        }
        ratoteFSlider.gameObject.SetActive(support.Contains(LovenseCommandType.ROTATE));
        pumpFSlider.gameObject.SetActive(support.Contains(LovenseCommandType.PUMP));
        thrustFSlider.gameObject.SetActive(support.Contains(LovenseCommandType.THRUSTRING));
        fingerFSlider.gameObject.SetActive(support.Contains(LovenseCommandType.FINGERING));
        suctionFSlider.gameObject.SetActive(support.Contains(LovenseCommandType.SUCTION));
        strokeLowFSlider.gameObject.SetActive(support.Contains(LovenseCommandType.POSITION));
        strokeHeightFSlider.gameObject.SetActive(support.Contains(LovenseCommandType.POSITION));
        positionFSlider.gameObject.SetActive(support.Contains(LovenseCommandType.POSITION));
        btnPosition.gameObject.SetActive(support.Contains(LovenseCommandType.POSITION));
        oscillateFSlider.gameObject.SetActive(support.Contains(LovenseCommandType.OSCILLATE));
    }

    public void ClickOnSendFunction()
    {
        int vibrate = (int)vibrateFSlider.value;
        int ratote = (int)ratoteFSlider.value;
        int pump = (int)pumpFSlider.value;
        int thrust = (int)thrustFSlider.value;
        int finger = (int)fingerFSlider.value;
        int suction = (int)suctionFSlider.value;
        int strokeLow = (int)strokeLowFSlider.value;
        int strokeHeight = (int)strokeHeightFSlider.value;
        int osc = (int)oscillateFSlider.value;
        float timeSec = timeSecFSlider.value;
        float loopRunnintTime = loopRunFSlider.value;
        float loopPauseTime = loopPauseFSlider.value;
        List<LovenseCommand> commands = new List<LovenseCommand>();

        if (vibrate != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.VIBTATE;
            command.value = vibrate;
            commands.Add(command);
        }
        if (ratote != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.ROTATE;
            command.value = ratote;
            commands.Add(command);
        }
        if (pump != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.PUMP;
            command.value = pump;
            commands.Add(command);
        }
        if (thrust != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.THRUSTRING;
            command.value = thrust;
            commands.Add(command);
        }
        if (finger != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.FINGERING;
            command.value = finger;
            commands.Add(command);
        }
        if (suction != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.SUCTION;
            command.value = suction;
            commands.Add(command);
        }
        if(strokeHeight != 0)
        {
            LovenseSolaceProCommand command = new LovenseSolaceProCommand();
            command.type = LovenseCommandType.POSITION;
            command.strokeLow = strokeLow;
            command.strokeHeight = strokeHeight;
            commands.Add(command);
        }
        if (osc != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.OSCILLATE;
            command.value = osc;
            commands.Add(command);
        }

        List<string> toys = getToysCallback();
            LovenseDongleTools.GetInstance().SendFunctions(toys, commands, timeSec, loopRunnintTime, loopPauseTime);
    }

    public void ClickOnSetPosition()
    {
        int position = (int)positionFSlider.value;
        List<string> toys = getToysCallback();
        LovenseDongleTools.GetInstance().SetPosition(toys[0],position);
    }
}
