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

public class DrawArrow : DrawShape
{
    private RectTransform rectTransform_;

    private readonly List<Vector2> _vertices = new List<Vector2>(2);

    private void Awake()
    {
        rectTransform_ = GetComponent<RectTransform>();
    }

    public static Rect RectTransformToScreenSpace(RectTransform transform)
    {
        Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
        return new Rect((Vector2)transform.position - (size * 0.5f), size);
    }

    public override void StartDraw(Vector2 position)
    {
        _vertices.Add(position);
        _vertices.Add(position);

        rectTransform_.pivot = new Vector2(0, 0.5f);
        rectTransform_.position = position;
        Vector2 temp = rectTransform_.anchoredPosition; // без этой штуки не работает!
    }
    public override void StopDraw(Vector2 position)
    {
    }

    public override void UpdateShape(Vector2 newPosition)
    {
        _vertices[_vertices.Count - 1] = newPosition;

        var length = Vector2.Distance(_vertices[0], _vertices[1]);

        rectTransform_.sizeDelta = new Vector2(length, 20);

        float angle = Mathf.Atan2(_vertices[1].y - _vertices[0].y, _vertices[1].x - _vertices[0].x) * Mathf.Rad2Deg;
        transform.eulerAngles = new Vector3(0, 0, angle);
    }

    public override void Clear()
    {
    }
}