package org.project.location;

import org.project.entity.enemies.*;
import java.util.ArrayList;
import java.util.Random;

public class Location {

    private String name;
    private String description;
    private ArrayList<Enemy> possibleEnemies;
    private Enemy currentEnemy;
    private Random random;

    public Location(String name, String description) {
        this.name = name;
        this.description = description;
        this.possibleEnemies = new ArrayList<>();
        this.random = new Random();
    }

    //Adding enemy to location
    public void addEnemy(Enemy enemy) {
        possibleEnemies.add(enemy);
    }

    //Spawning a random enemy from among the enemies in this location
    public void spawnRandomEnemy() {
        if (possibleEnemies.isEmpty()) {
            //If no enemy was defined
            addDefaultEnemies();
        }

        int index = random.nextInt(possibleEnemies.size());
        currentEnemy = copyEnemy(possibleEnemies.get(index));
    }

    private void addDefaultEnemies() {
        possibleEnemies.add(new Goblin());
        possibleEnemies.add(new Skeleton());
        possibleEnemies.add(new Vampire());
    }

    //Copying the enemy to have a new instance each time
    private Enemy copyEnemy(Enemy original) {
        if (original instanceof Goblin) return new Goblin();
        if (original instanceof Skeleton) return new Skeleton();
        if (original instanceof Vampire) return new Vampire();
        return new Goblin();
    }


    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public Enemy getCurrentEnemy() {
        return currentEnemy;
    }

    public void setCurrentEnemy(Enemy enemy) {
        this.currentEnemy = enemy;
    }

    public boolean hasEnemy() {
        return currentEnemy != null && currentEnemy.isAlive();
    }
}