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

/// <summary>
/// Сетка 
/// </summary>
public class Grid : MonoBehaviour
{
    //public GameObject grid;
    //public float Length = 40;
    public int Quantity = 20;
    //public bool isOn;
    public enum Axis { XY, XZ, YZ };
    public Axis axis = Axis.XZ;

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

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

    public void CreateGrid()
    {
        //if (transform.localScale.x > transform.localScale.z) Quantity = Convert.ToInt32(transform.localScale.x);
        //if (transform.localScale.z > transform.localScale.x) Quantity = Convert.ToInt32(transform.localScale.z);
        for (int i = 0; i < Quantity; i++)
        {
            for (float j = 0; j < 1; j += 0.1f)
            {
                var width = 0.01f;
                if (j == 0)
                    width = 0.1f;
                if (j == 0.5f) width = 0.05f;

                switch (axis)
                {
                    case Axis.YZ:
                        NewLine(new Vector3(0, 0, i + j), new Vector3(0, 0, i + j), new Vector3(0, Quantity * 2, i + j), "LineY_" + (i + j).ToString("0.#"), width, transform.gameObject);
                        NewLine(new Vector3(0, i + j, 0), new Vector3(0, i + j, 0), new Vector3(0, i + j, Quantity * 2), "LineZ_" + (i + j).ToString("0.#"), width, transform.gameObject);
                        break;
                    case Axis.XZ:
                NewLine(new Vector3(0, 0, i + j), new Vector3(0, 0, i + j), new Vector3(Quantity * 2, 0, i + j), "LineX_" + (i + j).ToString("0.#"), width, transform.gameObject);
                NewLine(new Vector3(i + j, 0, 0), new Vector3(i + j, 0, 0), new Vector3(i + j, 0, Quantity * 2), "LineZ_" + (i + j).ToString("0.#"), width, transform.gameObject);
                        break;
                    case Axis.XY:
                        NewLine(new Vector3(i + j, 0, 0), new Vector3(i + j, 0, 0), new Vector3(i + j, Quantity * 2, 0), "LineY_" + (i + j).ToString("0.#"), width, transform.gameObject);
                        NewLine(new Vector3(0, i + j, 0), new Vector3(0, i + j, 0), new Vector3(Quantity * 2, i + j, 0), "LineX_" + (i + j).ToString("0.#"), width, transform.gameObject);
                        break;
                }
            }
        }
        transform.position = new Vector3(0, 0.01f, 0);
    }  

    public static LineRenderer NewLine(Vector3 pos, Vector3 start, Vector3 end, string name_line, float width = 0.1f, GameObject parent = null)
    {
        var line = Instantiate(Resources.Load("GameObjects/Line", typeof(LineRenderer))) as LineRenderer;
        line.transform.position = pos;
        line.name = name_line;
        line.SetPosition(line.positionCount++, start);
        line.SetPosition(line.positionCount++, end);
        line.startColor = Color.gray;
        line.endColor = Color.gray;
        line.material.color = Color.gray;
        if(parent != null)line.transform.parent = parent.transform;
        //line.name = name_line;
        //line.transform.localScale = localScale;
        //line.transform.position = position;
        line.startWidth = width;
        return line;
    }
}
