using ActionGame.GameArtifacts.Models.Characters;
using ActionGame.GameArtifacts.Models.Game;
using ActionGame.GameResources.Models;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;

namespace ActionGame.GameArtifacts.Intelligence.EnemyAttack
{
    class PlayerFacer
    {
        private IBattleObject user;
        private GameState gameState;


        public PlayerFacer(IBattleObject user, GameState gameState)
        {
            this.user = user;
            this.gameState = gameState;
        }


        public void FacePlayerIfInArea()
        {
            List<Vector2> directions = new List<Vector2>();
            directions.Add(new Vector2(0, 1));
            directions.Add(new Vector2(0, -1));
            directions.Add(new Vector2(1, 0));
            directions.Add(new Vector2(-1, 0));

            foreach(Vector2 direction in directions)
            {
                Int32 x = user.WorldPosition.PositionX;
                Int32 y = user.WorldPosition.PositionY;

                if (direction.Y == 1)
                {
                    y += 1;
                }
                else if (direction.Y == -1)
                {
                    y -= TileSet.DRAW_HEIGHT;
                }
                else if (direction.X == -1)
                {
                    x -= TileSet.DRAW_WIDTH;
                    y -= TileSet.DRAW_HEIGHT;
                }
                else if (direction.X == 1)
                {
                    x += TileSet.DRAW_WIDTH;
                    y -= TileSet.DRAW_HEIGHT;
                }

                Rectangle rect = new Rectangle(x, y, TileSet.DRAW_WIDTH, TileSet.DRAW_HEIGHT);

                if (rect.Intersects(gameState.Player.GetDamageCollisionArea()))
                {
                    user.CharacterSet.SetDirection(direction);

                    return;
                }
                
            }
        }


        public Boolean PlayerIsInRange()
        {
            Int32 x = user.WorldPosition.PositionX;
            Int32 y = user.WorldPosition.PositionY;

            Vector2 direction = user.CharacterSet.GetDirection();

            if (direction.Y == 1)
            {
                y += 1;
            }
            else if (direction.Y == -1)
            {
                y -= TileSet.DRAW_HEIGHT;
            }
            else if (direction.X == -1)
            {
                x -= TileSet.DRAW_WIDTH;
                y -= TileSet.DRAW_HEIGHT;
            }
            else if (direction.X == 1)
            {
                x += TileSet.DRAW_WIDTH;
                y -= TileSet.DRAW_HEIGHT;
            }

            Rectangle rect = new Rectangle(x, y, TileSet.DRAW_WIDTH, TileSet.DRAW_HEIGHT);

            if (rect.Intersects(gameState.Player.GetDamageCollisionArea()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }
}
