package electrosphere.entity.state.movement;

import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.btree.BehaviorTree;
import electrosphere.entity.state.AnimationPriorities;
import electrosphere.entity.state.movement.jump.ServerJumpTree;
import electrosphere.game.data.creature.type.movement.FallMovementSystem;
import electrosphere.server.poseactor.PoseActor;

public class ServerFallTree implements BehaviorTree {

    static enum FallState {
        ACTIVE,
        INACTIVE,
    }

    FallState state = FallState.INACTIVE;

    String animationFall = "Armature|Fall";
    String animationLand = "Armature|Land";

    Entity parent;

    ServerJumpTree jumpTree;

    public ServerFallTree(Entity parent, FallMovementSystem fallMovementSystem){
        this.parent = parent;
    }

    @Override
    public void simulate(float deltaTime) {
        PoseActor poseActor = EntityUtils.getPoseActor(parent);
        switch(state){
            case ACTIVE:
            if(poseActor != null){
                String animationToPlay = determineCorrectAnimation();
                if(
                    !poseActor.isPlayingAnimation() || !poseActor.isPlayingAnimation(animationToPlay) &&
                    (jumpTree == null || !jumpTree.isJumping())
                ){
                    poseActor.playAnimation(animationToPlay,AnimationPriorities.getValue(AnimationPriorities.MOVEMENT_MODIFIER));
                    poseActor.incrementAnimationTime(0.0001);
                }
            }
            break;
            case INACTIVE:
            break;
        }
    }

    public void start(){
        state = FallState.ACTIVE;
    }

    public boolean isFalling(){
        return state == FallState.ACTIVE;
    }

    public void land(){
        if(state != FallState.INACTIVE){
            state = FallState.INACTIVE;
            PoseActor poseActor = EntityUtils.getPoseActor(parent);
            if(poseActor != null){
                String animationToPlay = determineCorrectAnimation();
                if(
                    !poseActor.isPlayingAnimation() || !poseActor.isPlayingAnimation(animationToPlay)
                ){
                    poseActor.playAnimation(animationToPlay,AnimationPriorities.getValue(AnimationPriorities.MOVEMENT_MODIFIER));
                    poseActor.incrementAnimationTime(0.0001);
                }
            }
        }
    }

    public static ServerFallTree getFallTree(Entity parent){
        return (ServerFallTree)parent.getData(EntityDataStrings.FALL_TREE);
    }

    String determineCorrectAnimation(){
        switch(state){
            case ACTIVE:
            return animationFall;
            case INACTIVE:
            return animationLand;
            default:
            return animationLand;
        }
    }

    public void setServerJumpTree(ServerJumpTree jumpTree){
        this.jumpTree = jumpTree;
    }

    public void setAnimationFall(String animationName){
        animationFall = animationName;
    }

    public void setAnimationLand(String animationName){
        animationLand = animationName;
    }
    
}
