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

[AddComponentMenu("设备预制体")]
public class DragTest : MonoBehaviour
{
    public List<GameObject> listTargets = new List<GameObject>();
    /// <summary>
    /// 设备一列层数
    /// </summary>
    [Header("设备一列层数")]
    public int PerTier = 6;
    /// <summary>
    /// 设备一个区域列数
    /// </summary>
    [Header("设备一个区域列数")]
    public int PerRow = 5;

    public bool isExit = false;
    public bool isClicked = false;
    public bool isray;

    private void Update()
    {
        RaycastHit hit;
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 999, 1 << 6 | 1 << 8))
        {
            isray = true;
        }
        else isray = false;
        //Debug.Log(isray);
        if (isray) { Debug.Log(hit.transform.name); }
        if (isExit)
        {
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Vector3.Distance(Camera.main.transform.position, listTargets[0].transform.position));
            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace)/* + offset*/;
            transform.position = curPosition;

            if (Input.GetMouseButtonUp(0))
            {
                if (isray)
                {
                    bool isTarget = false;
                    foreach (GameObject item in listTargets)
                    {
                        if (GetOverGameObject(Camera.main.gameObject).Contains(item))
                        {
                            if (item.transform.childCount >= 1)
                            {
                                break;
                            }
                            isTarget = true;
                            transform.position = item.transform.position;
                            //设置父物体
                            transform.SetParent(item.transform, true);
                            isExit = false;


                            //判断当前为第几个子物体
                            int rank = item.transform.GetSiblingIndex() + 1;
                            int isarea = 1; int isrow = 1; int istier = 0;


                            for (int i = 1; i <= rank; i++)
                            {
                                if (istier < PerTier)
                                {
                                    istier++;
                                }
                                else
                                {
                                    if (isrow < PerRow)
                                    {
                                        isrow++;
                                    }
                                    else
                                    {
                                        isarea++;
                                        isrow = 1;
                                    }
                                    istier = 1;
                                }
                            }


                            //设备第几个区域
                            gameObject.GetComponent<Device>().mydata.area = isarea.ToString();
                            //设备第几列
                            gameObject.GetComponent<Device>().mydata.row = isrow.ToString();
                            //设备第几层
                            gameObject.GetComponent<Device>().mydata.tier = istier.ToString();
                            Debug.Log("第" + isarea + "区，第" + isrow + "列，第" + istier + "层");
                            gameObject.GetComponent<Device>().isPut = true;
                        }
                    }
                    if (isTarget == false)
                    {
                        isExit = false;
                        Destroy(gameObject);
                    }
                }
                else
                {
                    isExit = false;
                    Destroy(gameObject);
                }
            }
        }
    }

    //public void OnMouseUp()
    //{
    //    if (isray)
    //    {
    //        bool isTarget = false;
    //        foreach (GameObject item in listTargets)
    //        {
    //            if (GetOverGameObject(Camera.main.gameObject).Contains(item))
    //            {
    //                if (item.transform.childCount >= 1)
    //                {
    //                    break;
    //                }
    //                isTarget = true;
    //                transform.position = item.transform.position;
    //                //设置父物体
    //                transform.SetParent(item.transform, true);
    //                isExit = false;


    //                //判断当前为第几个子物体
    //                int rank = item.transform.GetSiblingIndex() + 1;
    //                int isarea = 1; int isrow = 1; int istier = 0;


    //                for (int i = 1; i <= rank; i++)
    //                {
    //                    if (istier < PerTier)
    //                    {
    //                        istier++;
    //                    }
    //                    else
    //                    {
    //                        if (isrow < PerRow)
    //                        {
    //                            isrow++;
    //                        }
    //                        else
    //                        {
    //                            isarea++;
    //                            isrow = 1;
    //                        }
    //                        istier = 1;
    //                    }
    //                }


    //                //设备第几个区域
    //                gameObject.GetComponent<Device>().mydata.area = isarea.ToString();
    //                //设备第几列
    //                gameObject.GetComponent<Device>().mydata.row = isrow.ToString();
    //                //设备第几层
    //                gameObject.GetComponent<Device>().mydata.tier = istier.ToString();
    //                Debug.Log("第" + isarea + "区，第" + isrow + "列，第" + istier + "层");
    //                gameObject.GetComponent<Device>().isPut = true;
    //            }
    //        }
    //        if (isTarget == false)
    //        {
    //            isExit = false;
    //            Destroy(gameObject);
    //        }
    //    }
    //    else
    //    {
    //        isExit = false;
    //        Destroy(gameObject);
    //    }
    //}
    //获取光标停留的3D物体
    public List<GameObject> GetOverGameObject(GameObject raycaster)
    {
        PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
        pointerEventData.position = Input.mousePosition;
        PhysicsRaycaster pr = raycaster.GetComponent<PhysicsRaycaster>();
        List<RaycastResult> results = new List<RaycastResult>();
        pr.Raycast(pointerEventData, results);
        List<GameObject> listObjs = new List<GameObject>();
        if (results.Count != 0)
        {
            foreach (RaycastResult item in results)
            {
                if (item.gameObject.layer != 8)
                    listObjs.Add(item.gameObject);
            }
        }
        return listObjs;
    }


    private void OnMouseDrag()
    {
        isExit = true;
    }
}

