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

public class Mission2FeedbackPanel : MonoBehaviour
{
    [SerializeField]
    ToggleGroup toggleGroup;
    [SerializeField]
    Slider levelSlider, strengthSlider;
    [SerializeField]
    Text modeText,levelStrength_all,positionText;
    [SerializeField]
    RectTransform bg, fg;
    [SerializeField]
    Text topBottom;


    Coroutine coroutine;

    string toyId;

    public void SetToyId(string toyId)
    {
        this.toyId = toyId;
    }

    // Start is called before the first frame update
    void Start()
    {
        LovenseAndroidSDK.GetInstance().SubscribeMission2ModeGotEvent(OnGetCurrentTouchModel);
        LovenseAndroidSDK.GetInstance().SubscribeMission2CurrentLevelGotEvent(OnGetCurrentTouchLevel);
        LovenseAndroidSDK.GetInstance().SubscribeMission2LevelValuesGotEvent(OnGetLevelStrengths);
        LovenseAndroidSDK.GetInstance().SubscribeMission2FeedbackDataGotEvent(OnGetMission2TouchData);

        LovenseAndroidSDK.GetInstance().AddMission2Listener(toyId);
    }

    private void OnDestroy()
    {
        LovenseAndroidSDK.GetInstance().UnsubscribeMission2ModeGotEvent(OnGetCurrentTouchModel);
        LovenseAndroidSDK.GetInstance().UnsubscribeMission2CurrentLevelGotEvent(OnGetCurrentTouchLevel);
        LovenseAndroidSDK.GetInstance().UnsubscribeMission2LevelValuesGotEvent(OnGetLevelStrengths);
        LovenseAndroidSDK.GetInstance().UnsubscribeMission2FeedbackDataGotEvent(OnGetMission2TouchData);
    }

    private void OnGetLevelStrengths(int value1, int value2, int value3)
    {
        levelStrength_all.text = "level1:"+ value1 + ",level2:" + value2 + ",level3:" + +value3;
    }

    private void OnGetCurrentTouchModel(TouchModeOfMission2 arg0)
    {
        modeText.text = arg0.ToString();
    }

    private void OnGetMission2TouchData(List<FeedBackDataForMission2> arg0)
    {
        if(coroutine != null)
        {
            StopCoroutine(coroutine);
        }
        coroutine = StartCoroutine(ShowTouch(arg0));
    }

    private IEnumerator ShowTouch(List<FeedBackDataForMission2> arg0)
    {
        int len = arg0.Count;
        for(int i=0;i < len;i++)
        {
            fg.sizeDelta = new Vector2(bg.rect.width * arg0[i].position / 100f, fg.rect.height);
            positionText.text = arg0[i].position+"";
            if (arg0[i].direction == 0)
            {
                topBottom.text = "↓↓";
            } else
            {
                topBottom.text = "↑↑";
            }
            yield return new WaitForSeconds(0.1f);
        }
    }
    private void OnGetCurrentTouchLevel(int arg0)
    {
        levelSlider.value = arg0;
    }

    public void ClickOnSetMode()
    {
        IEnumerable<Toggle> selected = toggleGroup.ActiveToggles();
        var enumerator = selected.GetEnumerator();
        if (enumerator.MoveNext())
        {
            Toggle currentToggle = enumerator.Current;
            string str = currentToggle.name;
            if (str == "Toggle0")
            {
                LovenseAndroidSDK.GetInstance().SendCommand(toyId, new LovenseCommand() { type = LovenseCommandType.SET_TOUCHMODE, value = (int)TouchModeOfMission2.Mode_Btn });
            }
            else if (str == "Toggle1")
            {
                LovenseAndroidSDK.GetInstance().SendCommand(toyId, new LovenseCommand() { type = LovenseCommandType.SET_TOUCHMODE, value = (int)TouchModeOfMission2.Mode_Touch_Deep });
            }
            else if (str == "Toggle2")
            {
                LovenseAndroidSDK.GetInstance().SendCommand(toyId, new LovenseCommand() { type = LovenseCommandType.SET_TOUCHMODE, value = (int)TouchModeOfMission2.Mode_Touch_Speed });
            }
            else if (str == "Toggle3")
            {
                LovenseAndroidSDK.GetInstance().SendCommand(toyId, new LovenseCommand() { type = LovenseCommandType.SET_TOUCHMODE, value = (int)TouchModeOfMission2.Mode_Feedback_Normal });
            }
            else if (str == "Toggle4")
            {
                LovenseAndroidSDK.GetInstance().SendCommand(toyId, new LovenseCommand() { type = LovenseCommandType.SET_TOUCHMODE, value = (int)TouchModeOfMission2.Mode_Feedback_Deep });
            }
        }
    }

    public void ClickOnGetMode()
    {
        LovenseAndroidSDK.GetInstance().SendCommandWithoutValue(toyId, LovenseCommandType.GET_TOUCHMODE);
    }

    public void ClickOnGetLevel()
    {
        LovenseAndroidSDK.GetInstance().SendCommandWithoutValue(toyId, LovenseCommandType.GET_TOUCH_LEVEL);
    }

    public void ClickOnSetLevel()
    {
        int level = (int)levelSlider.value;
        LovenseAndroidSDK.GetInstance().SendCommand(toyId, new LovenseCommand() { type = LovenseCommandType.SET_TOUCH_LEVEL, value = level });
    }

    public void ClickOnSetLevelsStrength()
    {
        int levelValue = (int)levelSlider.value;
        int strengthValue = (int)strengthSlider.value;
        LovenseAndroidSDK.GetInstance().SendCommand(toyId, (LovenseCommand)new LovenseMission2Command() { level=levelValue,strength = strengthValue });
    }

    public void ClickOnGetStrengths()
    {
        LovenseAndroidSDK.GetInstance().SendCommandWithoutValue(toyId, LovenseCommandType.GET_TOUCH_VALUE);
    }
}
