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

public class FunctionPanel : MonoBehaviour
{
    [SerializeField]
    Slider vibrate, rotate, pump, thrusting, fingering,oscillate,setPosition, timeSec, runningTime, pauseTime,solace_thrust, solace_depth, solacePro_low, solacePro_high, solacePro_level;
    [SerializeField]
    GameObject solacePanel, solaceProPanel;
    [SerializeField]
    SolaceProFeedbackPanel solaceProFeedbackPanel;
    [SerializeField]
    Mission2FeedbackPanel mission2FeedbackPanel;
    Func<List<string>> getToysCallback;

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

    }

    public void InitSupport()
    {
        List<string> toys = getToysCallback();
        List<LovenseCommandType> supported = new List<LovenseCommandType>();
        foreach(string toyId in toys)
        {
            List<LovenseCommandType> temp = LovenseAndroidSDK.GetInstance().GetSupportedCommands(toyId);
            for(int i=temp.Count-1;i>=0;i--)
            {
                if(!supported.Contains(temp[i]))
                {
                    supported.Add(temp[i]);
                }
            }
        }
        vibrate.gameObject.SetActive(supported.Contains(LovenseCommandType.VIBRATE));
        rotate.gameObject.SetActive(supported.Contains(LovenseCommandType.ROTATE));
        pump.gameObject.SetActive(supported.Contains(LovenseCommandType.PUMP));
        thrusting.gameObject.SetActive(supported.Contains(LovenseCommandType.THRUSTRING));
        fingering.gameObject.SetActive(supported.Contains(LovenseCommandType.FINGERING));
        oscillate.gameObject.SetActive(supported.Contains(LovenseCommandType.OSCILLATE));
        setPosition.gameObject.SetActive(supported.Contains(LovenseCommandType.SET_POSITION));
        solacePanel.gameObject.SetActive(supported.Contains(LovenseCommandType.THRUST_AND_DEPTH));
        solaceProPanel.gameObject.SetActive(supported.Contains(LovenseCommandType.STROKE));
        if(supported.Contains(LovenseCommandType.STROKE))
        {
            solaceProFeedbackPanel.SetToyId(toys[0]);
            solaceProFeedbackPanel.gameObject.SetActive(true);
        }
        else
        {
            solaceProFeedbackPanel.gameObject.SetActive(false);
        }
        if (supported.Contains(LovenseCommandType.SET_TOUCHMODE))
        {
            mission2FeedbackPanel.SetToyId(toys[0]);
            mission2FeedbackPanel.gameObject.SetActive(true);
        }
        else
        {
            mission2FeedbackPanel.gameObject.SetActive(false);
        }
        

    }

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

    public void ClickOnSendFunctions()
    {
        List<LovenseCommand> commands = new List<LovenseCommand>();
        int vb = (int)(vibrate.value);
        int rt = (int)(rotate.value);
        int pm = (int)(pump.value);
        int th = (int)(thrusting.value);
        int fg = (int)(fingering.value);
        int osc = (int)oscillate.value;
        int pos = (int)setPosition.value;
        float ts = timeSec.value;
        float rt1 = runningTime.value;
        float pt = pauseTime.value;

        if (vb != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.VIBRATE;
            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 (osc != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.OSCILLATE;
            command.value = osc;
            commands.Add(command);
        }
        if (pos != 0)
        {
            LovenseCommand command = new LovenseCommand();
            command.type = LovenseCommandType.SET_POSITION;
            command.value = pos;
            commands.Add(command);
        }
        List<string> ids = getToysCallback();

        if (ids.Count == 0)
        {
            LovenseAndroidSDK.GetInstance().ShowToast("please select toys");
            return;
        }
        LovenseAndroidSDK.GetInstance().SendFunctions(ids.ToArray(), commands, ts, rt1, pt);
    }

    public void ClickOnSendSolaceCommand()
    {
        if (getToysCallback().Count == 1) {
            LovenseAndroidSDK.GetInstance().SendCommand(getToysCallback()[0], (LovenseCommand)(new LovenseSolaceCommand() { depth = (int)solace_depth.value, thrust = (int)solace_thrust.value }));
        }
    }

    public void ClickOnSendSolaceProCommand()
    {
        if (getToysCallback().Count == 1)
        {
            LovenseAndroidSDK.GetInstance().SendCommand(getToysCallback()[0], (LovenseCommand)(new LovenseSolaceProCommand() { strokeLow = (int)solacePro_low.value, strokeHigh = (int)solacePro_high.value,level = (int)solacePro_level.value }));
        }
    }

}
