import processing.core.PImage;

import java.util.List;

public class Tree extends Plant{
    public static final String TREE_KEY = "tree";
    /**
     * Creates an Entity representing a(n) 'Tree'.
     * The parameters provide a hint to data relevant for a 'Tree' class.
     *
     * @param id              A unique string identifier for the entity.
     *                        Typically, the 'Tree' key, but can be anything.
     * @param position        The entity's x/y position in the world.
     * @param images          A list of images that represent an entity and its possible animation.
     * @param animationPeriod The time between when an entity's animation is scheduled and executed.
     * @param behaviorPeriod  The time between when an entity's activity is scheduled and executed.
     * @return A new Entity object configured as a 'Tree'.
     */
    public Tree(String id, Point position, List<PImage> images, double animationPeriod, double behaviorPeriod, int health) {
        super(id, position, images, animationPeriod, behaviorPeriod, health);
    }

    /** Checks the Tree's health and transforms accordingly, returning true if successful. */
    public boolean transform(World world, EventScheduler scheduler, ImageLibrary imageLibrary) {
        if (getHealth() <= 0) {
            String cleanId = getId()
                    .replace(TREE_KEY + "_", "")
                    .replace(Sapling.SAPLING_KEY + "_", "");
            String newId = STUMP_KEY + "_" + cleanId;

            Entity stump = new Stump(newId, getPosition(), imageLibrary.get(STUMP_KEY));

            world.removeEntity(scheduler, this);
            world.addEntity(stump);

            return true;
        }
        return false;
    }

    /** Executes Tree specific Logic. */
    public void executeBehavior(World world, ImageLibrary imageLibrary, EventScheduler scheduler) {
        if (!transform(world, scheduler, imageLibrary)) {
            scheduleBehavior(scheduler, world, imageLibrary);
        }
    }

    public void updateImage() {
        setImageIndex(getImageIndex() + 1);
    }
}
