using Microsoft.MixedReality.Toolkit;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ETInteraction : MonoBehaviour
{
    //public XvEyeFlowManage xvEyeFlowManage;
    public XvEyeInit xvEyeInit;

    public Transform head;


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //Debug.DrawRay(head.position, head.forward*20);
        //RaycastHit lefthit;
        ////Vector3 leftgazeDirection = leftgaze.position - head.position;
        //Vector3 leftgazeDirection = head.forward;
        //Ray leftRay = new Ray(head.position, leftgazeDirection);
        //if (Physics.Raycast(leftRay, out lefthit))
        //{
        //    if (lefthit.collider.gameObject.name == "plane")
        //    {
        //        GameObject.Find("leftHitpoint").transform.position = lefthit.point;
        //    }

        //}

        //if (xvEyeFlowManage.Recomgaze!=null)
        if (xvEyeInit.Recomgaze!=null)
        {
            //Vector3 recomgazeDirection = xvEyeFlowManage.Recomgaze.position - head.position;
            Vector3 recomgazeDirection = xvEyeInit.Recomgaze.position - head.position;

            //Vector3 leftgazeDirection = leftgaze.position - head.position;
            //Vector3 rightgazeDirection = rightgaze.position - head.position;

            #region ۶ע볡еcubeн
            Ray recomRay = new Ray(head.position, recomgazeDirection);
            //Ray leftRay = new Ray(head.position, leftgazeDirection);
            //Ray rightRay = new Ray(head.position, rightgazeDirection);

            RaycastHit recomhit;
            //RaycastHit lefthit;
            //RaycastHit righthit;

            if (Physics.Raycast(recomRay, out recomhit))
            {
                for (int i = 0; i < transform.childCount; i++)
                {
                    if (transform.GetChild(i).name == recomhit.collider.gameObject.name)
                    {
                        transform.GetChild(i).localScale = new Vector3(1, 1, 0.05f);
                    }
                    else
                    {
                        transform.GetChild(i).localScale = new Vector3(0.6f, 0.6f, 0.05f);
                    }
                }

            }
            else
            {
                for (int i = 0; i < transform.childCount; i++)
                {
                    if (transform.GetChild(i).localScale.x != 0.6f)
                    {
                        transform.GetChild(i).localScale = new Vector3(0.6f, 0.6f, 0.05f);
                    }
                }
            }
            #endregion
        }

        if (API.xslam_ready())
        {
            //Debug.Log($"head left pos:{Math.Round(head.GetChild(0).GetChild(0).position.x, 3)},{Math.Round(head.GetChild(0).GetChild(0).position.y, 3)},{Math.Round(head.GetChild(0).GetChild(0).position.z, 3)}");
            //Debug.Log($"head left rot:{head.GetChild(0).GetChild(0).eulerAngles}");
            //Debug.Log($"head right:{Math.Round(head.GetChild(0).GetChild(1).position.x, 3)},{Math.Round(head.GetChild(0).GetChild(1).position.y, 3)},{Math.Round(head.GetChild(0).GetChild(1).position.z, 3)}");
            //Debug.Log($"head right rot:{head.GetChild(0).GetChild(1).eulerAngles}");
        }
    }
}
