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

public class LevelWall : MonoBehaviour
{
    public Vector2 position1;
    public Vector2 position2;

    public Vector3 worldPosition1;
    public Vector3 worldPosition2;

    private Vector2 screenPosition1;
    private Vector2 screenPosition2;

    public GameObject wallPrefab;

    private GameObject wallInstance;

    static Texture2D _rectTexture;
    public static Texture2D PointTexture
    {
        get
        {
            if (_rectTexture == null)
            {
                _rectTexture = new Texture2D(1, 1);
                _rectTexture.SetPixel(0, 0, Color.blue);
                _rectTexture.Apply();
            }

            return _rectTexture;
        }
    }

    public bool loadedFromDisk = false;

    void Start()
    {
        if (!loadedFromDisk)
        {
            worldPosition1 = Camera.main.ScreenToWorldPoint(position1);
            worldPosition2 = Camera.main.ScreenToWorldPoint(position2);
        }

        InstantiatePrefab();
        Create2DCollision();
    }


    private Vector3 getWorldTransformPosition(Vector3 worldPosition1, Vector3 worldPosition2)
    {
        Vector3 min = Vector3.Min(worldPosition1, worldPosition2);
        return min;
    }

    private void InstantiatePrefab()
    {
        //wallInstance = Instantiate(wallPrefab);
        // wallInstance.transform.SetParent(this.gameObject.transform);
        //wallInstance.transform.localPosition = Vector3.zero;

        /*
         * Vector2 vectorToTarget = worldPosition2 - worldPosition1;
        float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg - 90;

        var hypotenuse = Mathf.Sqrt(Mathf.Pow(worldPosition1.x - worldPosition2.x, 2) + Mathf.Pow(worldPosition1.y - worldPosition2.y, 2));

        var width = Vector2.Distance(worldPosition2, worldPosition1);
        var height = 10;

        if (angle >= 0)
        {
            transform.position = worldPosition1 - new Vector3(width, 0, 0);
        }
        else
        {
            transform.position = worldPosition2;
        }


        Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);

        wallInstance.transform.localScale = new Vector2(height, width);
        //transform.Translate(Mathf.Sign(angle) * width / 2, 0, 0);
        wallInstance.transform.rotation = q;

        /*BoxCollider2D boxCollider = wallInstance.GetComponent<BoxCollider2D>();
        boxCollider.size = new Vector2(width, height);*/
    }

    private void OnGUI()
    {
        Vector3 w1 = worldPosition1;
        Vector3 w2 = worldPosition2;
        w1.y = -w1.y;
        w2.y = -w2.y;

        Vector2 p1 = Camera.main.WorldToScreenPoint(w1);
        Vector2 p2 = Camera.main.WorldToScreenPoint(w2);

        DrawLine(p1, p2, 5);
    }

    private void DrawLine(Vector2 start, Vector2 end, int width)
    {
        Vector2 d = end - start;

        if (d.SqrMagnitude() < 0.01)
        {
            Destroy(this.gameObject);
            return;
        }

        float a = Mathf.Rad2Deg * Mathf.Atan(d.y / d.x);
        if (d.x < 0)
            a += 180;

        int width2 = (int)Mathf.Ceil(width / 2);

        GUIUtility.RotateAroundPivot(a, start);
        GUI.DrawTexture(new Rect(start.x, start.y - width2, d.magnitude, width), PointTexture);
        GUIUtility.RotateAroundPivot(-a, start);
    }

    private void Create2DCollision()
    {
        EdgeCollider2D collider = GetComponent<EdgeCollider2D>();
        Vector2[] points =
        {
            worldPosition1,
            worldPosition2
        };
        collider.points = points;
        // collider.offset = collider.size / 2;


        PolygonCollider2D polygonCollider = GetComponentInChildren<PolygonCollider2D>();
        Vector2[] polyPoints =
        {
            new Vector2(worldPosition1.x - 1, worldPosition1.y),
            new Vector2(worldPosition1.x - 1, worldPosition1.y),
            new Vector2(worldPosition2.x - 1, worldPosition2.y),
            new Vector2(worldPosition2.x + 1, worldPosition2.y)
        };

        polygonCollider.SetPath(0, polyPoints);
    }
}
