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

public class RemoteEventItem : MonoBehaviour
{
    private bool isConnected = false;
    [SerializeField]
    public Text functionName;

    [SerializeField]
    public Toggle keyDown, keyPressed, keyUp;

    [SerializeField]
    public Toggle keyDown1, keyPressed1, keyUp1;

    [SerializeField]
    public Slider vibrateSlider, functionSlider, batterySlider, shakeSlider;
    EventToy thisToy;
    [SerializeField]
    public Text toyName;
    [SerializeField]
    public Text ShakeTimeText;
    private int shakeTimes = 0;
    public void SetData(EventToy toy)
    {
        isConnected = true;
        thisToy = toy;
        toyName.text = thisToy.name+"-"+toy.id;

        if(thisToy.type == "max")
        {
            functionName.text = "inflation";
            functionSlider.maxValue = 3;
        } else if(thisToy.type == "nora")
        {
            functionName.text = "rotation";
            functionSlider.maxValue = 20;
        }
        batterySlider.value = thisToy.battery;
        LovenseRemote.eventOfButton.AddListener(OnEventOfButton);
        LovenseRemote.eventOfFunction.AddListener(OnEventOfFunction);
        LovenseRemote.eventOfShakeFrequencyChanged.AddListener(OnShakeFrequencyChanged);
        LovenseRemote.eventOfEveryShake.AddListener(OnEveryShake);
        LovenseRemote.eventOfToyStatus.AddListener(OnEventOfToyStatus);
    }

    public void SetSconnected(bool isConnected)
    {
        this.isConnected = isConnected;
    }
    
    private void OnEventOfToyStatus(string ip,string id,bool connected)
    {
        if(id != thisToy.id)
        {
            return;
        }
    }
    private void OnShakeFrequencyChanged(string ip,string id,float value)
    {
        if (id != thisToy.id)
        {
            return;
        }
        shakeSlider.value = value;
    }

    private void OnEveryShake(string ip,string id)
    {
        if(id != thisToy.id)
        {
            return;
        }
        shakeTimes++;
        ShakeTimeText.text = "Shake:" + shakeTimes;
    }

    private void OnEventOfFunction(string ip,string id,FunctionValue functionValue)
    {
        if (id != thisToy.id)
        {
            return;
        }
        if(functionValue.type == FunctionType.VIBRATE)
        {
            vibrateSlider.value = functionValue.value;
        }
        if(functionValue.type == FunctionType.ROTATE)
        {
            functionSlider.value = functionValue.value;
        }
        if(functionValue.type == FunctionType.INFLATION)
        {
            functionSlider.value = functionValue.value;
        }
        if(functionValue.type == FunctionType.BATTERY)
        {
            batterySlider.value = functionValue.value;
        }
    }

    private void OnEventOfButton(string ip,string id,int target,ButtonActionType type)
    {
        if (id != thisToy.id)
        {
            return;
        }
        if(target == 0 )
        {
            if(type == ButtonActionType.BUTTON_DOWN)
            {
                keyDown.isOn = true;
            } else if(type == ButtonActionType.BUTTON_UP)
            {
                keyUp.isOn = true;
            } else if(type == ButtonActionType.BUTTON_PRESSED)
            {
                keyPressed.isOn = true;
            }
        } else if(target == 1)
        {
            if (type == ButtonActionType.BUTTON_DOWN)
            {
                keyDown1.isOn = true;
            }
            else if (type == ButtonActionType.BUTTON_UP)
            {
                keyUp1.isOn = true;
            }
            else if (type == ButtonActionType.BUTTON_PRESSED)
            {
                keyPressed1.isOn = true;
            }
        }
    }

    private void OnDestroy()
    {
        LovenseRemote.eventOfButton.RemoveListener(OnEventOfButton);
        LovenseRemote.eventOfFunction.RemoveListener(OnEventOfFunction);
        LovenseRemote.eventOfShakeFrequencyChanged.RemoveListener(OnShakeFrequencyChanged);
        LovenseRemote.eventOfToyStatus.RemoveListener(OnEventOfToyStatus);
        LovenseRemote.eventOfEveryShake.RemoveListener(OnEveryShake);
    }
}
