﻿using Lovense.UnityKit.Remote;
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,positionFSlider, strokeLowFSlider, strokeHeightFSlider, fingerFSlider, suctionFSlider,oscillateFSlider, loopRunFSlider, loopPauseFSlider, timeSecFSlider;
    // Start is called before the first frame update
    [SerializeField]
    public GameObject positionBtn;

    Func<RemoteAppData> getCurrentRemote;
    Func<List<LovenseRemoteToy>> getCurrentToys;
    void Start()
    {
    }

    public void SetCurrentRemoteCallback(Func<RemoteAppData> get)
    {
        getCurrentRemote = get;
    }

    public void SetCurrentToyCallback(Func<List<LovenseRemoteToy>> get )
    {
        getCurrentToys = get;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
     
    public void InitSupport()
    {
        List<LovenseCommandType> types = new List<LovenseCommandType>();
        List<LovenseRemoteToy> toys = getCurrentToys();
        foreach(LovenseRemoteToy toy in toys)
        {
            List<LovenseCommandType> support = LovenseRemote.GetInstance().GetSupportCommandsByName(toy.name);
            int len = support.Count;
            for(int i=0;i < len;i++)
            {
                if(!types.Contains(support[i]))
                {
                    types.Add(support[i]);
                }
            }
        }
        ratoteFSlider.gameObject.SetActive(types.Contains(LovenseCommandType.ROTATE));
        pumpFSlider.gameObject.SetActive(types.Contains(LovenseCommandType.PUMP));
        thrustFSlider.gameObject.SetActive(types.Contains(LovenseCommandType.THRUSTRING));
        strokeLowFSlider.gameObject.SetActive(types.Contains(LovenseCommandType.POSITION));
        strokeHeightFSlider.gameObject.SetActive(types.Contains(LovenseCommandType.POSITION));
        positionFSlider.gameObject.SetActive(types.Contains(LovenseCommandType.POSITION));
        fingerFSlider.gameObject.SetActive(types.Contains(LovenseCommandType.FINGERING));
        suctionFSlider.gameObject.SetActive(types.Contains(LovenseCommandType.SUCTION)); 
        positionBtn.SetActive(types.Contains(LovenseCommandType.POSITION));
        oscillateFSlider.gameObject.SetActive(types.Contains(LovenseCommandType.OSCILLATE));

    }


    public void OnSendFunctionClick()
    {
        int vibrate = (int)vibrateFSlider.value;
        int ratote = (int)ratoteFSlider.value;
        int pump = (int)pumpFSlider.value;
        int thrust = (int)thrustFSlider.value;
        int strokeLow = (int)strokeLowFSlider.value;
        int strokeHeight = (int)strokeHeightFSlider.value;
        int finger = (int)fingerFSlider.value;
        int suction = (int)suctionFSlider.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.VIBRATE;
            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.strokeHigh = strokeHeight;
            commands.Add(command);
        }
        if (osc != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.OSCILLATE;
            command.value = osc;
            commands.Add(command);
        }
        LovenseRemote.GetInstance().SendFunctions(getCurrentToys(), commands, timeSec, loopRunnintTime, loopPauseTime, 1);
    }

    public void OnSendPositionClick()
    {
        int position = (int)positionFSlider.value;
        List<LovenseRemoteToy> toys = getCurrentToys();
        LovenseRemote.GetInstance().SendPositionCommand(toys, position);
        
    }
}
