using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;

public class SwapControls : MonoBehaviour {
    [SerializeField]
    private VRTK_SDKManager sdkManager;
    [SerializeField]
    private GameObject leftHand;
    [SerializeField]
    private GameObject rightHand;
    [SerializeField]
    private VRTK_TransformFollow menuTransformFollow;
    [SerializeField]
    private VRTK_TransformFollow deformationPointTransformFollow;

    private GameObject _currentMainHand;



    private void Start()
    {
        _currentMainHand = leftHand;
        SetEnabledControls(leftHand, true);
        SetEnabledControls(rightHand, false);
    }

    /// <summary>
    /// Swap hand controls
    /// </summary>
    public void SwapHands()
    {
        SetEnabledControls(leftHand, _currentMainHand != leftHand);
        SetEnabledControls(rightHand, _currentMainHand != rightHand);
        _currentMainHand = (_currentMainHand == leftHand) ? rightHand : leftHand;

        var rightController = VRTK_DeviceFinder.GetControllerRightHand(true);
        var leftController = VRTK_DeviceFinder.GetControllerLeftHand(true);
        if (_currentMainHand == leftHand)
        {
            menuTransformFollow.gameObjectToFollow = rightController;
            deformationPointTransformFollow.gameObjectToFollow = leftController;
        } else
        {
            menuTransformFollow.gameObjectToFollow = leftController;
            deformationPointTransformFollow.gameObjectToFollow = rightController;
        }
        menuTransformFollow.enabled = false;
        menuTransformFollow.enabled = true;
        deformationPointTransformFollow.enabled = false;
        deformationPointTransformFollow.enabled = true;
    }

    private void SetEnabledControls(GameObject controls, bool enable)
    {
        VRTK_ControllerEvents deformingControls = controls.GetComponent<VRTK_ControllerEvents>();
        if (deformingControls != null)
        {
            deformingControls.enabled = enable;
        }

    }





}
