using Microsoft.Xna.Framework;
using System;
using Sledgemapper.Shared.Entities;

namespace Sledgemapper
{
    public class State
    {
        public Tile _selectedTile = new() { X = 1, Y = 1 };
        public Tile _hoveredTile = new() { X = 1, Y = 1 };
        public Wall _selectedWall = new() { X = 1, Y = 1 };
        public Overlay _selectedOverlay = new() { X = 1, Y = 1 };
        public int _tileSize = 30;
        public string _currentTileId = "";
        public string _currentWallId = "";
        public string _currentOverlayId = "";
        public InsertMode _insertMode;

        public void SelectClosestWall(Point mousePosition)
        {
            var topLeft = new Point(_hoveredTile.X * _tileSize, _hoveredTile.Y * _tileSize);
            var bottomLeft = new Point(_hoveredTile.X * _tileSize, _hoveredTile.Y * _tileSize + _tileSize);
            var topRight = new Point(_hoveredTile.X * _tileSize + _tileSize, _hoveredTile.Y * _tileSize);
            var bottomRight = new Point(_hoveredTile.X * _tileSize + _tileSize, _hoveredTile.Y * _tileSize + _tileSize);
            var center = new Point(_hoveredTile.X * _tileSize + _tileSize / 2, _hoveredTile.Y * _tileSize + _tileSize / 2);

            if (Utils.PointInTri(mousePosition, topLeft, center, bottomLeft)) //left wall
            {
                _selectedWall.X = _hoveredTile.X;
                _selectedWall.Y = _hoveredTile.Y;
                _selectedWall.Rotation = 1;
            }
            else if (Utils.PointInTri(mousePosition, topRight, bottomRight, center)) //right wall
            {
                _selectedWall.X = _hoveredTile.X + 1;
                _selectedWall.Y = _hoveredTile.Y;
                _selectedWall.Rotation = 1;
            }
            else if (Utils.PointInTri(mousePosition, topLeft, topRight, center)) //top wall
            {
                _selectedWall.X = _hoveredTile.X;
                _selectedWall.Y = _hoveredTile.Y;
                _selectedWall.Rotation = 0;
            }
            else if (Utils.PointInTri(mousePosition, bottomLeft, center, bottomRight))  //bottom wall
            {
                _selectedWall.X = _hoveredTile.X;
                _selectedWall.Y = _hoveredTile.Y + 1;
                _selectedWall.Rotation = 0;
            }
        }
        public void SelectOverlay(Point mousePosition)
        {
            _selectedOverlay.X = _hoveredTile.X;
            _selectedOverlay.Y = _hoveredTile.Y;
            var q1 = Math.Pow(mousePosition.X - _hoveredTile.X * _tileSize, 2);
            var q2 = Math.Pow(_hoveredTile.Y * _tileSize - mousePosition.Y, 2);
            var s = Math.Sqrt(q1 + q2);

            if (s < _tileSize / 3)
            {
                _selectedOverlay.Intersection = true;
                return;
            }

            q1 = Math.Pow(mousePosition.X - (_hoveredTile.X + 1) * _tileSize, 2);
            s = Math.Sqrt(q1 + q2);
            if (s < _tileSize / 3)
            {
                _selectedOverlay.X = _selectedOverlay.X + 1;
                _selectedOverlay.Intersection = true;
                return;
            }

            //q1 = System.Math.Pow(mousePosition.X - (_hoveredTile.X + 1) * _tileSize, 2);
            q2 = Math.Pow((_hoveredTile.Y + 1) * _tileSize - mousePosition.Y, 2);
            s = Math.Sqrt(q1 + q2);
            if (s < _tileSize / 3)
            {
                _selectedOverlay.X = _selectedOverlay.X + 1;
                _selectedOverlay.Y = _selectedOverlay.Y + 1;
                _selectedOverlay.Intersection = true;
                return;
            }

            q1 = Math.Pow(mousePosition.X - _hoveredTile.X * _tileSize, 2);
            q2 = Math.Pow((_hoveredTile.Y + 1) * _tileSize - mousePosition.Y, 2);
            s = Math.Sqrt(q1 + q2);
            if (s < _tileSize / 3)
            {
                _selectedOverlay.X = _selectedOverlay.X;
                _selectedOverlay.Y = _selectedOverlay.Y + 1;
                _selectedOverlay.Intersection = true;
                return;
            }

            _selectedOverlay.Intersection = false;
        }
    }
}
