using SC.XR.Unity.Module_InputSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using XRTool.Util;

public class ModelManager : MonoBehaviour
{
    private void OnDestroy()
    {
        DispatcherBase.KeyUpDelegateUnRegister(OnKeyUpAction);

    }
    private void OnEnable()
    {
        DispatcherBase.KeyUpDelegateRegister(OnKeyUpAction);
        DispatcherBase.KeyDownDelegateRegister(OnKeyDownAction);
    }
    InputDevicePartBase part0;
    Vector3 modelPos;
    private void OnKeyDownAction(InputKeyCode keyCode, InputDevicePartBase part)
    {
        part0 = part;
        startPos = part.detectorBase.pointerBase.cursorBase.transform.position;
        this.transform.position = startPos;
        this.transform.eulerAngles = new Vector3(0,0,0) ;
        Debug.Log("物体开始生成" + startPos);
        this.GetComponent<MeshRenderer>().enabled = true;
        this.GetComponent<BoxCollider>().enabled = false;
        this.transform.parent = null;
        modelPos = this.transform.localPosition;
        isCreate = true;
        DispatcherBase.KeyDownDelegateUnRegister(OnKeyDownAction);
    }

    private void OnKeyUpAction(InputKeyCode keyCode, InputDevicePartBase part)
    {
        var obj = part.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject;//SDK里判断点击返回的物体
        if (isUp)
        {
            if (Mathf.Abs(this.transform.localScale.y) <= 0.01f)
            {
                Destroy(this.gameObject);
                return;
            }
            Debug.Log("物体平面生成完毕并消除");
            this.GetComponent<BoxCollider>().enabled = true;
            this.GetComponent<XBoundingBox>().enabled = true;
            this.GetComponent<XDragComponent>().enabled = true;
            this.enabled = false;
            return;
        }
        if (isCreate)
        {
            startPos =new Vector3( part.detectorBase.pointerBase.cursorBase.transform.position.x, part.detectorBase.pointerBase.cursorBase.transform.position.y-0.01f, part.detectorBase.pointerBase.cursorBase.transform.position.z);
            Debug.Log("物体平面生成完毕");
            if(Mathf.Abs(this.transform.localScale.x)<=0.01f)
            {
                Destroy(this.gameObject);
                return;
            }
            isUp = true;
            isCreate = false;
            modelPos = this.transform.localPosition;
        }
    }


    bool isUp;
    bool isCreate;
    Vector3 startPos;
    Vector3 movePos;

    public void Update()
    {
        if(isUp)
        {
            Vector3 start = GSXRManager.Instance.leftCamera.transform.InverseTransformPoint(startPos);
            Vector3 end = GSXRManager.Instance.leftCamera.transform.InverseTransformPoint(part0.detectorBase.pointerBase.cursorBase.transform.position);

            float dicy = Vector3.Distance(new Vector3(0, start.y, 0), new Vector3(0, end.y, 0))/2;
            if (start.y > part0.detectorBase.pointerBase.cursorBase.transform.position.y)
            {
                dicy = -dicy;
            }
            this.transform.localScale = new Vector3(this.transform.localScale.x, dicy, this.transform.localScale.z);
            this.transform.localPosition = new Vector3(this.transform.localPosition.x, modelPos.y+ dicy/2, this.transform.localPosition.z);
            Debug.Log("物体开始生成立体");
        }
        if(isCreate)
        {
          Vector3 start = GSXRManager.Instance.leftCamera.transform.InverseTransformPoint(startPos);
            Vector3 end = GSXRManager.Instance.leftCamera.transform.InverseTransformPoint(part0.detectorBase.pointerBase.cursorBase.transform.position);

            Debug.Log("物体平面生成中" + end);
            //   this.transform.
            float dicx = Vector3.Distance(new Vector3(start.x,0,0), new Vector3(end.x, 0, 0));
            if(start.x> part0.detectorBase.pointerBase.cursorBase.transform.position.x)
            {
                dicx = -dicx;
            }

            float dicz = Vector3.Distance(new Vector3(0, start.y, start.z), new Vector3(0, end.y, end.z));
            if (start.z < end.z)
            {
                dicz = -dicz;
            }

            this.transform.localScale = new Vector3(dicx, 0.01f, dicz);
            this.transform.localPosition = new Vector3(modelPos.x+dicx/2, this.transform.localPosition.y, modelPos.z+ dicz/2);
        }
    }
}