import processing.core.PImage;

import java.util.List;

public class Sapling extends Plant{

    public static final String SAPLING_KEY = "sapling";
    public static final int SAPLING_HEALTH_LIMIT = 5;
    public static final double SAPLING_BEHAVIOR_PERIOD = 2.0;

    /**
     * Creates an Entity representing a(n) 'Sapling'.
     * The parameters provide a hint to data relevant for a 'Sapling' class.
     *
     * @param id A unique string identifier for the entity.
     *           Typically, the constant 'Sapling' 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.
     *
     * @return A new Entity object configured as a 'Sapling'.
     */
    public Sapling(String id, Point position, List<PImage> images) {
        super(id, position, images, SAPLING_BEHAVIOR_PERIOD, SAPLING_ANIMATION_PERIOD, 1);
    }

    /** Executes Sapling specific Logic. */
    @Override
    public void executeBehavior(World world, ImageLibrary imageLibrary, EventScheduler scheduler) {
        if (!transform(world, scheduler, imageLibrary)) {
            setHealth(getHealth() + 1);
            updateImage();
            scheduleBehavior(scheduler, world, imageLibrary);
        }
    }

    /** Checks the Sapling'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(SAPLING_KEY + "_", "")
                    .replace(STUMP_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;
        }
        if (getHealth() >= SAPLING_HEALTH_LIMIT) {
            String cleanId = getId().replace(STUMP_KEY + "_", "");
            String newId = TREE_KEY + "_" + cleanId;

            Tree tree = new Tree(newId, getPosition(), imageLibrary.get(TREE_KEY),
                    NumberUtil.getRandomDouble(TREE_RANDOM_ANIMATION_PERIOD_MIN, TREE_RANDOM_ANIMATION_PERIOD_MAX),
                    NumberUtil.getRandomDouble(TREE_RANDOM_BEHAVIOR_PERIOD_MIN, TREE_RANDOM_BEHAVIOR_PERIOD_MAX),
                    NumberUtil.getRandomInt(TREE_RANDOM_HEALTH_MIN, TREE_RANDOM_HEALTH_MAX));

            tree.setImageIndex(0);

            world.removeEntity(scheduler, this);
            world.addEntity(tree);
            return true;
        }

        return false;
    }

    @Override
    public void updateImage() {
        if (getHealth() <= 0) {
            setImageIndex(0);
        } else if (getHealth() < SAPLING_HEALTH_LIMIT) {
            int idx = (getImages().size() * getHealth()) / SAPLING_HEALTH_LIMIT;
            setImageIndex(idx);
        } else {
            setImageIndex(getImages().size() - 1);
        }
    }
}
