﻿using System.Collections.Generic;
using UnityEngine;

namespace FOHEART.GlovePlugin
{
    public class FOGestureActionAdatper : MonoBehaviour, IFOGestureAction
    {

        public virtual void onHandGesture(Gesture leftHandGesture, GameObject leftHandTouchObject, GameObject leftHandAdjacentItem, Gesture rightHandGesture, GameObject rightHandTouchObject, GameObject rightHandAdjacentItem)
        {
            onLeftHandGesture(leftHandGesture, leftHandTouchObject, leftHandAdjacentItem);
            onRightHandGesture(rightHandGesture, rightHandTouchObject, rightHandAdjacentItem);
        }

        public virtual void onRightHandGesture(Gesture currentGesture, GameObject handTouchObject, GameObject gameObject)
        {
            // 按压
            if (currentGesture == Gesture.PRESS)
            {
                onHandPress(handTouchObject, gameObject);
            }
        }

        public virtual void onLeftHandGesture(Gesture currentGesture, GameObject handTouchObject, GameObject gameObject)
        {
            // 地点标记、射线标记
            if (currentGesture == Gesture.MARK_POINT)
            {
                handleMarkPointGesture();
            }
            // 前往标记地点
            else if (currentGesture == Gesture.GO_POINT)
            {
                onGoPoint(lastPointPosition, lastPointMarkTime);
            }
        }


        public virtual LineRenderer getRayLine()
        {
            return null;
        }

        public virtual GameObject getRayLineAnchorObj()
        {
            return null;
        }

        public virtual void onMarkPoint(Vector3 point)
        {
        }

        public virtual void onGoPoint(Vector3 point, float time)
        {

        }

        public virtual void onHandPress(GameObject handTouchObject, GameObject gameObject)
        {
        }




        /// <summary>
        /// 最后标记地点方位
        /// </summary>
        private Vector3 lastPointPosition = Vector3.zero;
        /// <summary>
        /// 最后标记地点的时刻
        /// </summary>
        private float lastPointMarkTime = 0f;

        private void handleMarkPointGesture()
        {
            if (getRayLine() == null || getRayLineAnchorObj() == null)
            { 
                return;
            }

            // 上次射线还未隐藏
            CancelInvoke("hideRayLine");

            // 射线指示的方位
            lastPointPosition = Vector3.zero;
            lastPointMarkTime = 0f;
            // 射线(线段模拟)起始位置及方向
            Vector3 anchorV3 = getRayLineAnchorObj().transform.position;
            Vector3 fwd3 = getRayLineAnchorObj().transform.forward;
            // 执行射线检测
            if (Physics.Raycast(anchorV3, fwd3, out RaycastHit raycastHit, 20000f))
            {
                // 移动并显示线段
                getRayLine().SetPosition(0, getRayLine().transform.InverseTransformPoint(anchorV3));
                getRayLine().SetPosition(1, getRayLine().transform.InverseTransformPoint(raycastHit.point));
                // 记录射线碰撞的位置及时刻
                lastPointPosition = raycastHit.point;
                lastPointMarkTime = Time.time;
            }
            else
            {
                hideRayLine();
            }

            onMarkPoint(lastPointPosition);

            Invoke(nameof(hideRayLine), 0.5f);
        }



        private void hideRayLine()
        {
            getRayLine().SetPosition(0, Vector3.zero);
            getRayLine().SetPosition(1, Vector3.zero);
        }
    }
}
