package me.hammerle.supersnuvi.entity;

import me.hammerle.supersnuvi.entity.components.DefaultHealth;
import me.hammerle.supersnuvi.entity.components.DefaultMovement;
import me.hammerle.supersnuvi.entity.components.Death;
import me.hammerle.supersnuvi.entity.components.DefaultEnergy;
import me.hammerle.supersnuvi.entity.components.Health;
import me.hammerle.supersnuvi.entity.components.Movement;
import me.hammerle.supersnuvi.entity.components.HeroItemCollector;
import me.hammerle.supersnuvi.entity.components.LandMovement;
import me.hammerle.supersnuvi.entity.components.MovementPenalty;
import me.hammerle.supersnuvi.entity.components.ai.EntityController;
import me.hammerle.supersnuvi.entity.components.ai.FollowHeroController;
import me.hammerle.supersnuvi.entity.components.ai.HumanController;
import me.hammerle.supersnuvi.entity.components.animator.HeroAnimator;
import me.hammerle.supersnuvi.entity.components.animator.EntityAnimator;
import me.hammerle.supersnuvi.gamelogic.Level;

public final class EntityBuilder 
{
    private final Entity ent;
    
    private EntityBuilder(Entity ent)
    {
        this.ent = ent;
    }
    
    public static EntityBuilder newBuilder(Level level, double x, double y, double width, double height)
    {
        return new EntityBuilder(new Entity(level, x, y, width, height));
    }
    
    public static Entity buildHero(Level level, double x, double y)
    {
        Entity hero = new Entity(level, x, y, 32.0, 64.0);
        hero.animator = new HeroAnimator(hero);
        hero.controller = new HumanController(hero);
        hero.health = new DefaultHealth(hero, new Death() 
                {
                    @Override
                    public void onDeath(Entity ent) 
                    {
                        ent.getLevel().scheduleReset();
                    }
                }, 100.0);
        hero.energy = new DefaultEnergy(hero, 100.0);
        hero.move = new DefaultMovement(hero, ent -> level.getData().getInt("jump", 12));
        hero.itemCollector = new HeroItemCollector(hero);
        hero.movePenalty = new LandMovement(hero);
        return hero;
    }
    
    public static Entity buildTest(Level level, double x, double y)
    {
        Entity hero = new Entity(level, x, y, 32.0, 64.0);
        hero.animator = new HeroAnimator(hero);
        hero.controller = new FollowHeroController(hero, 1);
        //hero.controller = new HumanController(hero);
        hero.health = new DefaultHealth(hero, Death.NULL, 100.0);
        hero.energy = new DefaultEnergy(hero, 100.0);
        hero.move = new DefaultMovement(hero, ent -> level.getData().getInt("jump", 12));
        //hero.itemCollector = new HeroItemCollector(hero);
        hero.movePenalty = new LandMovement(hero);
        return hero;
    }
    
    public EntityBuilder setController(EntityController controller)
    {
        ent.controller = controller;
        return this;
    }
    
    public EntityBuilder setAnimator(EntityAnimator animator)
    {
        ent.animator = animator;
        return this;
    }
    
    public EntityBuilder setHealth(Health health)
    {
        ent.health = health;
        return this;
    }
    
    public EntityBuilder setMovement(Movement move)
    {
        ent.move = move;
        return this;
    }
    
    public EntityBuilder setMovementPenalty(MovementPenalty move)
    {
        ent.movePenalty = move;
        return this;
    }
}
