package electrosphere.server.ai.creature;

import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.movement.groundmove.ClientGroundMovementTree;
import electrosphere.entity.state.movement.groundmove.ClientGroundMovementTree.MovementRelativeFacing;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.server.ai.AITree;

import java.util.Random;
import org.joml.Vector3d;

/**
 * An ai whose goal is to wander sporadically
 */
public class MillAbout implements AITree {
    
    Entity character;
    
    boolean millAbout = true;
    int millMoveTimer = 0;
    int millMoveTimerTimeout = 100;
    int MILL_MOVE_TIMEOUT_LOWER = 1500;
    int MILL_MOVE_TIMEOUT_UPPER = 2000;
    float millTargetMaxDist = 3;
    
    boolean moveToTarget = false;
    Vector3d moveTargetPosition;
    
    private MillAbout(Entity character){
        this.character = character;
    }
    
    
    
    @Override
    public void simulate(){
        Vector3d position = EntityUtils.getPosition(character);
        //mill aboud
        if(millAbout){
            if(millMoveTimer >= millMoveTimerTimeout){
                Random rand = new Random();
                millMoveTimer = 0;
                millMoveTimerTimeout = rand.nextInt(MILL_MOVE_TIMEOUT_UPPER - MILL_MOVE_TIMEOUT_LOWER) + MILL_MOVE_TIMEOUT_LOWER;
                Vector3d moveVector = new Vector3d();
                //search for a spot to mill to
//                while(true){
                    moveVector.set(
                            rand.nextFloat() - 0.5,
                            rand.nextFloat() - 0.5,
                            rand.nextFloat() - 0.5
                    ).normalize().mul(millTargetMaxDist);
                    moveVector.y = position.y; //Globals.commonWorldData.getElevationAtPoint(new Vector3d(position).add(moveVector));
                    
                    //TODO: search in navmeshmanager to make sure navigable, otherwise generate new pos
//                }
                moveTargetPosition = new Vector3d(position).add(moveVector);
                millAbout = false;
                moveToTarget = true;
            } else {
                millMoveTimer++;
            }
        }
        if(moveToTarget){
            if(moveTargetPosition.distance(position) > 0.4){
                Vector3d moveVector = new Vector3d(moveTargetPosition).sub(position).normalize();
                CreatureUtils.setFacingVector(character, new Vector3d((float)moveVector.x,(float)moveVector.y,(float)moveVector.z));
                ClientGroundMovementTree characterMoveTree = (ClientGroundMovementTree)CreatureUtils.serverGetEntityMovementTree(character);
                if(characterMoveTree.getState()==ClientGroundMovementTree.MovementTreeState.IDLE || characterMoveTree.getState()==ClientGroundMovementTree.MovementTreeState.SLOWDOWN){
                    characterMoveTree.start(MovementRelativeFacing.FORWARD);
                }
            } else {
                ClientGroundMovementTree characterMoveTree = (ClientGroundMovementTree)CreatureUtils.serverGetEntityMovementTree(character);
                characterMoveTree.slowdown();
                // System.out.println("Made it to destination");
                moveToTarget = false;
                millAbout = true;
            }
        }
//        if(target != null){
//            if(inAttackRange()){
//                attack();
//            } else {
//                if(inAggroRange()){
//                    moveToTarget();
//                } else {
//                    target = null;
//                }
//            }
//        } else {
//            searchForTarget();
//        }
    }

    public static AITree construct(Entity parent) {
        MillAbout rVal = new MillAbout(parent);
        return rVal;
    }

    @Override
    public String getTreeName() {
        return "MillAbout";
    }

    @Override
    public void updateStateAndPriority() {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'updateStateAndPriority'");
    }

    @Override
    public String getCurrentStateName() {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'getCurrentStateName'");
    }

    @Override
    public int getCurrentStatePriority() {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'getCurrentStatePriority'");
    }
    
}
