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

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

    public Vector3 worldPosition1;
    public Vector3 worldPosition2;
    private Vector2 screenPosition1;
    private Vector2 screenPosition2;

    public bool loadedFromDisk = false;

    static Texture2D _redTexture;
    public static Texture2D RedTexture
    {
        get
        {
            if (_redTexture == null)
            {
                _redTexture = new Texture2D(1, 1);
                _redTexture.SetPixel(0, 0, Color.red);
                _redTexture.Apply();
            }

            return _redTexture;
        }
    }

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

        transform.position = getWorldTransformPosition(worldPosition1, worldPosition2);

        Create2DCollision();
    }

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

    public void OnGUI()
    {
        screenPosition1 = Camera.main.WorldToScreenPoint(worldPosition1);
        screenPosition2 = Camera.main.WorldToScreenPoint(worldPosition2);

        var rect = GetScreenRect(screenPosition1, screenPosition2);
        DrawScreenRect(rect, new Color(0.8f, 0.8f, 0.95f, 0.25f));
        DrawScreenRectBorder(rect, 2, new Color(0.8f, 0.8f, 0.95f));
    }

    public static Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2)
    {
        // Move origin from bottom left to top left
        screenPosition1.y = Screen.height - screenPosition1.y;
        screenPosition2.y = Screen.height - screenPosition2.y;
        // Calculate corners
        var topLeft = Vector3.Min(screenPosition1, screenPosition2);
        var bottomRight = Vector3.Max(screenPosition1, screenPosition2);
        // Create Rect
        return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
    }

    public static void DrawScreenRectBorder(Rect rect, float thickness, Color color)
    {
        // Top
        DrawScreenRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color);
        // Left
        DrawScreenRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color);
        // Right
        DrawScreenRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color);
        // Bottom
        DrawScreenRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color);
    }

    public static void DrawScreenRect(Rect rect, Color color)
    {
        GUI.color = color;
        GUI.DrawTexture(rect, RedTexture);
        GUI.color = Color.red;
    }

    private void Create2DCollision()
    {
        BoxCollider2D collider = GetComponent<BoxCollider2D>();
        collider.size = new Vector2(Mathf.Abs(worldPosition1.x - worldPosition2.x), Mathf.Abs(worldPosition1.y - worldPosition2.y));
        collider.offset = collider.size / 2;
    }
}
