using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.UIElements.VisualElement;


/// <summary>
/// 创建线条
/// </summary>
public class CreateLine : MonoBehaviour
{
    public Transform A5;
    public Transform B5;
    public Transform A6;
    public Transform B6;
    public Transform A7;
    public Transform B7;
    public Transform A7_1;
    public Transform B7_1;
    public Transform A7_2;
    public Transform B7_2;
    public List<Transform> list7 = new List<Transform>();

    /// <summary>
    /// 端口边距
    /// </summary>
    [Header("端口边距")] public float edge_Distance = 1;

    [Header("不同高度集"), SerializeField] List<Transform> _vectors_tall = new List<Transform>();

    /// <summary>
    /// 不同高度集
    /// </summary>
    [HideInInspector] public List<Vector3> vectors_tall = new List<Vector3>();

    public float lineWidth = 0.1f; // 线的粗细
    public Color lineColor = Color.white; // 线的颜色

    private LineRenderer lineRenderer; // LineRenderer 组件

    /// <summary>
    /// 当前创建的空物体
    /// </summary>
    private GameObject go;


    private void Awake()
    {
        for (int i = 0; i < _vectors_tall.Count; i++)
            vectors_tall.Add(_vectors_tall[i].position);

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


    }

    // Update is called once per frame
    void Update()
    {

    }

    /// <summary>
    /// 生成LineGUID
    /// </summary>
    public static class UniqueGuidGenerator
    {
        private static HashSet<string> generatedGuids = new HashSet<string>();

        public static string GenerateUniqueGuid()
        {
            string guidString;
            do
            {
                guidString = Guid.NewGuid().ToString("N");
            } while (!generatedGuids.Add(guidString));

            return guidString;
        }
    }

    public void init()
    {
        go = new GameObject("EmptyObject");
        lineRenderer = go.GetComponent<LineRenderer>();
        if (!lineRenderer) lineRenderer = go.AddComponent<LineRenderer>();
        //lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
        lineRenderer.material = Resources.Load<Material>("emission");
        lineRenderer.startWidth = lineWidth;
        lineRenderer.endWidth = lineWidth;
        lineRenderer.startColor = lineColor;
        lineRenderer.endColor = lineColor;

        lineRenderer.useWorldSpace = false; // 将坐标设置为局部坐标
        lineRenderer.positionCount = (numSegments + 1) * 2;

        UpdateCylinder();

    }
    public float radius = 1f; // 圆柱体半径
    public float height = 1f; // 圆柱体高度
    public int numSegments = 32; // 圆柱体分段数
    void UpdateCylinder()
    {
        float segmentAngle = 2 * Mathf.PI / numSegments;
        float currentAngle = 0f;

        for (int i = 0; i <= numSegments; i++)
        {
            float x = Mathf.Cos(currentAngle);
            float z = Mathf.Sin(currentAngle);
            Vector3 topPoint = new Vector3(x * radius, height / 2f, z * radius);
            Vector3 bottomPoint = new Vector3(x * radius, -height / 2f, z * radius);

            lineRenderer.SetPosition(i * 2, topPoint);
            lineRenderer.SetPosition(i * 2 + 1, bottomPoint);

            currentAngle += segmentAngle;
        }
    }

    /// <summary>
    /// 同行(分同层、不同层两种)
    /// </summary>
    /// <param name="A"></param>
    /// <param name="B"></param>
    /// <param name="hierarchy">区</param>
    public void Creat5(Transform A, Transform B, int hierarchy = 0)
    {
        init();
        List<Vector3> vector3s = new List<Vector3>();
        Vector3 v1 = A.position;
        Vector3 v2 = A.position + new Vector3(edge_Distance, 0, 0);
        //同层 V3=V4   不同层 V3!=V4
        Vector3 v3 = v2 + new Vector3(0, 0, B.position.z - v2.z);
        Vector3 v4 = v3 + new Vector3(0, B.position.y - v3.y, 0);
        Vector3 v5 = B.position;

        vector3s.Add(v1);
        vector3s.Add(v2);
        vector3s.Add(v3);
        vector3s.Add(v4);
        vector3s.Add(v5);

        lineRenderer.positionCount = 5;

        lineRenderer.SetPositions(vector3s.ToArray());
    }

    /// <summary>
    /// 同列
    /// </summary>
    /// <param name="A"></param>
    /// <param name="B"></param>
    /// <param name="hierarchy">高度层级</param>
    public void Creat6(Transform A, Transform B, int hierarchy)
    {
        init();
        List<Vector3> vector3s = new List<Vector3>();
        Vector3 v1 = A.position;
        Vector3 v2 = A.position + new Vector3(edge_Distance, 0, 0);
        Vector3 v3 = v2 + new Vector3(0, vectors_tall[hierarchy].y - v2.y, 0);
        Vector3 v4 = v3 + new Vector3(B.position.x - v3.x + edge_Distance, 0, 0);
        Vector3 v5 = v4 + new Vector3(0, B.position.y - v4.y, 0);
        Vector3 v6 = B.position;

        vector3s.Add(v1);
        vector3s.Add(v2);
        vector3s.Add(v3);
        vector3s.Add(v4);
        vector3s.Add(v5);
        vector3s.Add(v6);

        lineRenderer.positionCount = 6;

        lineRenderer.SetPositions(vector3s.ToArray());
    }


    /// <summary>
    /// 不同行(分同列、不同列两种)
    /// </summary>
    /// <param name="A"></param>
    /// <param name="B"></param>
    /// <param name="hierarchy">区</param>
    public void Creat7(Transform A, Transform B, int hierarchy)
    {
        init();
        List<Vector3> vector3s = new List<Vector3>();
        Vector3 v1 = A.position;
        Vector3 v2 = A.position + new Vector3(edge_Distance, 0, 0);
        Vector3 v3 = v2 + new Vector3(0, vectors_tall[hierarchy].y - v2.y, 0);
        Vector3 v4 = v3 + new Vector3(B.position.x - v3.x + edge_Distance, 0, 0);
        Vector3 v5 = v4 + new Vector3(0, 0, B.position.z - v4.z);
        Vector3 v6 = v5 + new Vector3(0, B.position.y - v5.y, 0);
        Vector3 v7 = B.position;

        vector3s.Add(v1);
        vector3s.Add(v2);
        vector3s.Add(v3);
        vector3s.Add(v4);
        vector3s.Add(v5);
        vector3s.Add(v6);
        vector3s.Add(v7);

        lineRenderer.positionCount = 7;

        lineRenderer.SetPositions(vector3s.ToArray());
    }

    [ContextMenu("同行")]
    public void F5()
    {
        Creat5(A5, B5);
    }

    [ContextMenu("6点")]
    public void F6()
    {
        Creat6(A6, B6, 2);
    }

    [ContextMenu("不同行")]
    public void F7()
    {
        //Creat7(A7, B7, 1);
        for (int i = 0; i < list7.Count; i++)
        {
            if (i != 0 && i % 2 != 0)
            {
                Creat7(list7[i - 1], list7[i], (i + 1) / 2);
            }
        }
    }

}
