using System.Collections.Generic;
using RPG_Fight_Test.Assets.Scripts.Weapons;
using UnityEngine;
using UnityEngine.EventSystems;

namespace RPG_Fight_Test.Assets.Scripts.Creatures {
    public class Skeleton : Creature {
        private List<Creature> combatants;

        private void Start() {
            Dex = Random.Range(0, 20);
            movementRate = 20;
            CreatureHealth = Random.Range(10, 20);
            CurrentHealth = CreatureHealth;
            MaxHealth = CreatureHealth;
            IsCreatureAlive = true;

            PrimaryWeapon = new ShortSword();
            PrimaryWeapon.AttackProficiency = 50;
            Evade = Random.Range(0, 10);
        }

        private void DecideFirstAction() {
            List<Creature> closeEnemies = EnemiesInSquareNextToCreature(this, combatants);

            if (closeEnemies.Count > 0) {
                SetFirstAction(new AttackAction(this, closeEnemies[0])); // needs better logic, most damaged? biggest? smallest? random?
            } else {
                // Move towards closest enemy
                Creature closestEnemy = FindClosestEnemy(this, combatants);
                MoveAction action = new MoveAction();
                action.SetStartPosition(CurrentPos);
                action.SetTargetCreature(closestEnemy);
                action.SetCreatureToMove(this);
                SetFirstAction(action);
            }
        }

        // Just a copy of first action for now, should have its own logic
        private void DecideSecondAction() {
            List<Creature> closeEnemies = EnemiesInSquareNextToCreature(this, combatants);

            if (closeEnemies.Count > 0) {
                SetSecondAction(new AttackAction(this, closeEnemies[0]));
            } else {
                // Move towards closest enemy
                Creature closestEnemy = FindClosestEnemy(this, combatants);
                MoveAction action = new MoveAction();
                action.SetStartPosition(CurrentPos);
                action.SetTargetCreature(closestEnemy);
                action.SetCreatureToMove(this);
                SetSecondAction(action);
            }
        }

        public override void DecideActions(List<Creature> combatants) {
            this.combatants = combatants;
            List<Creature> closeEnemies = EnemiesInSquareNextToCreature(this, combatants);

            DecideFirstAction();
            DecideSecondAction();
            Debug.Log("Number of close enemies " + closeEnemies.Count);
        }

        public void ShowInfo() {

        }

        public override IAction ActionWhenBlocked(Creature blockingCreature) {
            IAction returnValue = null;
            if (blockingCreature.IsHumanControlled() != this.IsHumanControlled()) {
                returnValue = new AttackAction(this, blockingCreature);
            }
            return returnValue;
        }
    }
}