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

public class followCamera : MonoBehaviour
{
    private List<Vector3> pos;
    Transform[] objList;
    List<Transform> objZList=new List<Transform>();
    public float _dicCount;
    private float nDicCount;
    // Start is called before the first frame update
    void Start()
    {

        //  Invoke("checkMainPos", 2f);
        checkMainPos();
    }

    private float DICCOUNT
    {
        get
        {
            if(this.transform.localScale.x==0)
            {
                return 0;
            }
            else
            {

                return _dicCount / this.transform.localScale.x;
            }
        }
    }

    private void OnEnable()
    {
        if (GSXRManager.Instance)
        {
            this.transform.position = GSXRManager.Instance.head.position;
            this.transform.eulerAngles = new Vector3(0, GSXRManager.Instance.head.eulerAngles.y, 0);
        }
    }

    void checkMainPos()
    {
        if (objList==null)
        {
            this.nDicCount = DICCOUNT;
            pos = new List<Vector3>();
            objList = this.GetComponentsInChildren<Transform>(true);
            for (int i = 0; i < objList.Length; i++)
            {
                if(objList[i].parent==this.transform)
                {
                    pos.Add(new Vector3(objList[i].transform.localPosition.x, objList[i].transform.localPosition.y, objList[i].transform.localPosition.z));

                    objList[i].transform.localPosition = new Vector3(objList[i].localPosition.x, objList[i].localPosition.y, objList[i].localPosition.z + DICCOUNT);

                    objZList.Add(objList[i]);
                }
            }
        }
        if (this.nDicCount != DICCOUNT)
        {
            for (int i = 0; i < objZList.Count; i++)
            {
                objZList[i].transform.localPosition = new Vector3(pos[i].x, pos[i].y, pos[i].z + DICCOUNT);
            }
            this.nDicCount = DICCOUNT;
        }
    }

    float delayTime = 0f;
    float time = 0;
    bool isMove;
    Vector3 v3;
    float MOVETIME = 0.5f;
    float DEALYTIME = 1f;
    public float DISTANCE = 45;
    LeanTweenType leanTween = LeanTweenType.easeOutSine;
    // Update is called once per frame

    float jiaoduA;
    float jiaoduB;
    void checkEul()
    {
        /*
      /*  if (isMove)
        {
            time = time + Time.deltaTime;
            if (time > MOVETIME*2)
            {
                time = 0;
                isMove = false;
            }
        }*/
        jiaoduA = this.transform.eulerAngles.y;
        jiaoduB = GSXRManager.Instance.head.transform.eulerAngles.y;
        float ty = jiaoduA;

        float hty = jiaoduB;
        bool check = false;
        bool checkzf = false;
        float jiaodu;

        Vector3 ppv3 = this.transform.position;
        Vector3 hpv3 = GSXRManager.Instance.head.transform.position;
        float distancex = Mathf.Abs(ppv3.x - hpv3.x);
        float dis = DISTANCE / (distancex + 1);
        if (Mathf.Abs(ty - hty) < 180 && Mathf.Abs(ty - hty) > dis)
        {
            jiaodu = Mathf.Abs(ty - hty);
            check = true;
            if (ty > hty)
            {
                checkzf = true;
            }
            else
            {
                checkzf = false;
            }
        }
        else if (Mathf.Abs(ty - hty) > 180 && (360 - Mathf.Abs(ty - hty)) > dis)
        {
            jiaodu = (360 - Mathf.Abs(ty - hty));
            check = true;

            if (ty > hty)
            {
                checkzf = false;
            }
            else
            {
                checkzf = true;
            }
        }
        else
        {
            if (Mathf.Abs(ty - hty) < 180)
            {
                jiaodu = Mathf.Abs(ty - hty);
            }
            else
            {
                jiaodu = (360 - Mathf.Abs(ty - hty));
            }
        }
        //  GetAngle(SvrManager.Instance.head, this.transform);
        // this.transform.position = SvrManager.Instance.head.transform.position;
        if (check && !isMove)
        {
            //  delayTime = delayTime + Time.deltaTime;
            //   if (delayTime > DEALYTIME)
            //   {
            //   MovePos();
            if (checkzf)
            {
                //  Debug.Log("角度差值===》" + jiaodu);
                v3 = new Vector3(0, GSXRManager.Instance.head.transform.eulerAngles.y + dis, 0);

            }
            else
            {

                //    Debug.Log("角度差值===》" + -jiaodu);
                v3 = new Vector3(0, GSXRManager.Instance.head.transform.eulerAngles.y - dis, 0);
            }
      //    float a=  Mathf.Lerp(this.transform.eulerAngles.y, v3.y, Time.time);
        this.transform.rotation =    Quaternion.Lerp(this.transform.rotation,Quaternion.Euler(v3),0.1f);
            //  this.transform.Rotate(0,a,0);
            //   LTDescr lt = LeanTween.rotate(this.gameObject, v3, ((jiaodu - dis) / 150f));
            //    Invoke("isEndRotate", 0.3f);
            //  Debug.Log((this.transform.eulerAngles.y-v3.y));
            //    this.transform.eulerAngles = v3;
            // this.transform.eulerAngles = v3;
            //  MoveEul();
            //  }
            //  isMove = true;
        }
        else
        {
            delayTime = 0;
        }
    }
    void isEndRotate()
    {
        isMove = false;
    }

    bool ispMove;
    float pdelayTime = 0f;
    float ptime = 0;
    bool pisMove;
    Vector3 pv3;

    float PMOVETIME = 0.5f;
    float PDEALYTIME = 0.5f;
    float PDISTANCEX = 0.2f;
    public float PDISTANCEY = 0f;
    public float PDISTANCEYY = -0.3f;
    float PDISTANCEZ = 0.2f;
    float PDISTANCEZZ = 0.2f;
    LeanTweenType pleanTween = LeanTweenType.easeOutSine;
    float timeD = 0.1f;
    void checkPos()
    {

        Vector3 ppv3 = this.transform.position;
        Vector3 hpv3 = GSXRManager.Instance.head.transform.position;


        float distancey = ppv3.y - hpv3.y;
        float distancex = Mathf.Abs(ppv3.x - hpv3.x);
        float distancez = ppv3.z - hpv3.z;
        if (distancey > (PDISTANCEY+0.001f))
        {
            pv3 = new Vector3(ppv3.x, hpv3.y - PDISTANCEY, ppv3.z);


            //  LeanTween.move(this.gameObject, pv3, PMOVETIME).setEase(pleanTween);
           // this.transform.position = pv3;
            this.transform.position = Vector3.Lerp(this.transform.position, pv3, timeD);
            //    MoveEul();
            //   MovePos();
        }
        else if(distancey < (PDISTANCEYY-0.001f))
        {
            //    Debug.Log("4444444");
            pv3 = new Vector3(ppv3.x, (hpv3.y + PDISTANCEYY), ppv3.z);
            
            this.transform.position = Vector3.Lerp(this.transform.position, pv3, timeD);
            //      MoveEul();
        }
        ppv3 = this.transform.position;
        hpv3 = GSXRManager.Instance.head.transform.position;
        distancey = ppv3.y - hpv3.y;
        distancex = Mathf.Abs(ppv3.x - hpv3.x);
        distancez = ppv3.z - hpv3.z;
        if (distancex > PDISTANCEX)
        {
         //   Debug.Log("333333");
            if (ppv3.x > hpv3.x)
                pv3 = new Vector3(ppv3.x - (distancex - PDISTANCEX), ppv3.y , ppv3.z);
            else
                pv3 = new Vector3(ppv3.x + (distancex - PDISTANCEX), ppv3.y, ppv3.z);

            this.transform.position = Vector3.Lerp(this.transform.position, pv3, timeD);

            //  LeanTween.move(this.gameObject, pv3, PMOVETIME).setEase(pleanTween);
        //    this.transform.position = pv3;
            //  MovePos();
            //      MoveEul();
        }
        ppv3 = this.transform.position;
        hpv3 = GSXRManager.Instance.head.transform.position;
        distancey = ppv3.y - hpv3.y;
        distancex = Mathf.Abs(ppv3.x - hpv3.x);
        distancez = ppv3.z - hpv3.z;
        if (distancez > PDISTANCEZ)
        {
          //  Debug.Log("111111111");
             pv3 = new Vector3(ppv3.x, ppv3.y, ppv3.z - (distancez - PDISTANCEZ));


            this.transform.position = Vector3.Lerp(this.transform.position, pv3, timeD);
            //  LeanTween.move(this.gameObject, pv3, PMOVETIME).setEase(pleanTween);
          //  this.transform.position = pv3;
            //  MovePos();
          //  MoveEul();
        }
        else if (hpv3.z > ppv3.z && Mathf.Abs(distancez) > PDISTANCEZZ)
        {
          //  Debug.Log("22222222");
            pv3 = new Vector3(ppv3.x, ppv3.y, ppv3.z + (Mathf.Abs(distancez) - PDISTANCEZZ));
            this.transform.position = Vector3.Lerp(this.transform.position, pv3, timeD);
          //  this.transform.position = pv3;
        //    MoveEul();
        }
    }
    void Update()
    {
        checkEul();
        checkPos();
        checkMainPos();
        //pv3 = new Vector3(SvrManager.Instance.head.transform.position.x, 0, SvrManager.Instance.head.transform.position.z);
        //  LeanTween.move(this.gameObject, pv3, PMOVETIME).setEase(pleanTween);
        // this.transform.position = pv3;
    }

    void MovePos()
    {
      //  pdelayTime = 0;
         pv3 = new Vector3(GSXRManager.Instance.head.transform.position.x, 0, GSXRManager.Instance.head.transform.position.z);
          LeanTween.move(this.gameObject, pv3, PMOVETIME).setEase(pleanTween);
       // this.transform.position = pv3;
      //  pisMove = true;
    }

    void MoveEul()
    {
        if (!isMove)
        {
           // MovePos();
            delayTime = 0;
            v3 = new Vector3(0, GSXRManager.Instance.head.transform.eulerAngles.y, 0);
          //  LeanTween.rotateY(this.gameObject, v3.y, MOVETIME).setEase(leanTween);
         //   isMove = true;
        }
    }
}