using FOHEART.GlovePlugin;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AppearanceChangeLogic : FOGestureActionAdatper
{
    /// <summary>
    /// 识别到手势动作的时间点
    /// </summary>
    private float gestureActionTime = 0f;
    /// <summary>
    /// 当前识别到的手势动作
    /// </summary>
    private Gesture currentGesture;
    /// <summary>
    /// 左手接触物体的对象，可用于判断是否是手接触到了当前物体
    /// </summary>
    private GameObject handTouchObject;

    [Tooltip("可用材质")]
    public List<Material> materials;
    /// <summary>
    /// 当前使用的材质索引
    /// </summary>
    private int currentMaterialIndex = 0;


    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == handTouchObject)
        {
            doGestureAction();
        }
    }

    private void doGestureAction()
    {
        // 判断当前动作及识别时长
        if (currentGesture == Gesture.PRESS && Time.time - gestureActionTime < 0.03)
        {
            // 指定了材质
            if (materials != null && materials.Count > 0)
            {
                // 设置为当前材质
                Material currentMaterial = materials[currentMaterialIndex % materials.Count];
                GetComponent<Renderer>().material = currentMaterial;
                // 切换材质
                currentMaterialIndex++;
            }

            // 清空手势与时间点
            currentGesture = Gesture.NONE;
            gestureActionTime = 0f;
        }
    }

    public override void onHandPress(GameObject handTouchObject, GameObject gameObject)
    {
        // 按压手势
        this.currentGesture = Gesture.PRESS;
        this.gestureActionTime = Time.time;
        this.handTouchObject = handTouchObject;
    }
}
