﻿using System;
using System.Collections.Generic;
using Roguelike.Core.GameMap;
using Roguelike.Systems;
using Roguelike.Entities.Monsters;
using Roguelike.Entities.Interfaces;
using RogueSharp;

namespace Roguelike.Entities.Behaviours {
    class StandardMoveAndAttack : IBehaviour {

        public bool Act(Monster monster, CommandSystem commandSystem) {
            DungeonMap dungeonMap = Game.DungeonMap;
            Player player = Game.Player;
            FieldOfView monsterFov = new FieldOfView(dungeonMap);
            if (!monster.TurnsAlerted.HasValue) {
                monsterFov.ComputeFov(monster.x, monster.y, monster.Awareness, true);
                if (monsterFov.IsInFov(player.x, player.y)) {
                    monster.TurnsAlerted = 1;
                }
            }
            if (monster.TurnsAlerted.HasValue) {
                Cell bestMove = Game.PathfindingSystem.GetBestMoveTowardsTargetCell(dungeonMap.GetCell(monster.x, monster.y), dungeonMap.GetCell(player.x, player.y), monster);
                if (bestMove == null) {
                    Game.MessageLog.Add(monster.Name + " waits for a turn");
                } else {
                    commandSystem.MoveMonster(monster, bestMove, false);
                }
            }
            monster.TurnsAlerted++;
            if (monster.TurnsAlerted > 15) {
                monster.TurnsAlerted = null;
            }
            return true;
        }
    }
}