package electrosphere.server.ai.creature;

import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityTags;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.attack.ServerAttackTree;
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 electrosphere.server.datacell.Realm;
import electrosphere.server.datacell.utils.DataCellSearchUtils;

import org.joml.Vector3d;

/**
 * An ai whose goal is to find a weapon and try to kill nearby enemies
 */
public class MindlessAttacker implements AITree {
    

    /**
     * The parent entity to this ai tree
     */
    Entity parent;
    
    /**
     * The current target of this tree
     */
    Entity target;
    
    //aggression ranges
    float aggroRange = 10.0f;
    float attackRange = 1.0f;
    
    //cooldown controls
    int attackCooldownMax = 250;
    int attackCooldown = 0;
    
    
    
    @Override
    public void simulate(){
        if(target != null){
            if(inAttackRange()){
                attack();
            } else {
                if(inAggroRange()){
                    moveToTarget();
                } else {
                    target = null;
                }
            }
        } else {
            searchForTarget();
        }
    }
    
    void attack(){
        if(attackCooldown == 0){
            attackCooldown = attackCooldownMax;
            ServerAttackTree attackTree = CreatureUtils.serverGetAttackTree(parent);
            attackTree.start();
        } else {
            attackCooldown--;
        }
    }
    
    void moveToTarget(){
        Vector3d targetPosition = EntityUtils.getPosition(target);
        Vector3d characterPosition = EntityUtils.getPosition(parent);
        Vector3d moveVector = new Vector3d(targetPosition).sub(characterPosition).normalize();
        CreatureUtils.setFacingVector(parent, new Vector3d((float)moveVector.x,(float)moveVector.y,(float)moveVector.z));
        ClientGroundMovementTree characterMoveTree = (ClientGroundMovementTree)CreatureUtils.serverGetEntityMovementTree(parent);
        if(characterMoveTree.getState()==ClientGroundMovementTree.MovementTreeState.IDLE || characterMoveTree.getState()==ClientGroundMovementTree.MovementTreeState.SLOWDOWN){
            characterMoveTree.start(MovementRelativeFacing.FORWARD);
        }
    }
    
    boolean inAttackRange(){
        boolean rVal = false;
        Vector3d position = EntityUtils.getPosition(parent);
        Vector3d targetPosition = EntityUtils.getPosition(target);
        if(new Vector3d(position).distance(targetPosition) < attackRange){
            rVal = true;
        }
        return rVal;
    }
    
    boolean inAggroRange(){
        boolean rVal = false;
        Vector3d position = EntityUtils.getPosition(parent);
        Vector3d targetPosition = EntityUtils.getPosition(target);
        if(new Vector3d(position).distance(targetPosition) < aggroRange){
            rVal = true;
        }
        return rVal;
    }
    
    
    void searchForTarget(){
        Vector3d position = EntityUtils.getPosition(parent);
        Realm realm = Globals.realmManager.getEntityRealm(parent);
        for(Entity current : DataCellSearchUtils.getEntitiesWithTagAroundLocation(realm,position,EntityTags.LIFE_STATE)){
            if(current != parent){
                Vector3d potentialTargetPosition = EntityUtils.getPosition(current);
                if(position.distance(potentialTargetPosition) < aggroRange){
                    target = current;
                    break;
                }
            }
        }
    }



    public static AITree construct(Entity parent) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'construct'");
    }



    @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'");
    }

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