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

namespace Sledgemapper
{
    public class State
    {
        public Tile SelectedTile { get; set; }
        public Tile HoveredTile { get; set; }
        public Wall SelectedWall { get; set; }
        public Overlay SelectedOverlay { get; set; }
        public Note SelectedNote { get; set; }
        public int TileSize { get; set; }
        public string CurrentTileId { get; set; }
        public string CurrentWallId { get; set; }
        public string CurrentOverlayId { get; set; }
        public InsertMode InsertMode;

        public State()
        {
            CurrentTileId = "";
            CurrentWallId = "";
            CurrentOverlayId = "";
            SelectedTile = new() { X = 1, Y = 1 };
            HoveredTile = new() { X = 1, Y = 1 };
            SelectedWall = new() { X = 1, Y = 1 };
            SelectedOverlay = new() { X = 1, Y = 1 };
            SelectedNote = new() { X = 1, Y = 1 };
            TileSize = 30;
        }

        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;
        }
    }
}
