package electrosphere.game.server.ai.creature;

import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.AttackTree;
import electrosphere.entity.state.movement.GroundMovementTree;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.game.server.ai.AI;
import electrosphere.main.Globals;
import org.joml.Vector3d;
import org.joml.Vector3f;

/**
 *
 * @author amaterasu
 */
public class MindlessAttacker extends AI{
    
    Entity character;
    
    Entity target;
    
    float aggroRange = 10.0f;
    float attackRange = 1.0f;
    
    int attackCooldownMax = 250;
    int attackCooldown = 0;
    
    public MindlessAttacker(Entity character){
        this.character = character;
    }
    
    public static void attachToCreature(Entity creature){
        MindlessAttacker ai = new MindlessAttacker(creature);
        Globals.aiManager.registerAI(ai);
    }
    
    
    
    @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;
            AttackTree attackTree = CreatureUtils.getAttackTree(character);
            attackTree.start(EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_ONE_HAND);
        } else {
            attackCooldown--;
        }
    }
    
    void moveToTarget(){
        Vector3d targetPosition = EntityUtils.getPosition(target);
        Vector3d characterPosition = EntityUtils.getPosition(character);
        Vector3d moveVector = new Vector3d(targetPosition).sub(characterPosition).normalize();
        CreatureUtils.setMovementVector(character, new Vector3f((float)moveVector.x,(float)moveVector.y,(float)moveVector.z));
        GroundMovementTree characterMoveTree = CreatureUtils.getEntityMovementTree(character);
        if(characterMoveTree.getState()==GroundMovementTree.MovementTreeState.IDLE || characterMoveTree.getState()==GroundMovementTree.MovementTreeState.SLOWDOWN){
            characterMoveTree.start();
        }
    }
    
    boolean inAttackRange(){
        boolean rVal = false;
        Vector3d position = EntityUtils.getPosition(character);
        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(character);
        Vector3d targetPosition = EntityUtils.getPosition(target);
        if(new Vector3d(position).distance(targetPosition) < aggroRange){
            rVal = true;
        }
        return rVal;
    }
    
    
    void searchForTarget(){
        Vector3d position = EntityUtils.getPosition(character);
        for(Entity current : Globals.entityManager.getLifeStateEntities()){
            if(current != character){
                Vector3d potentialTargetPosition = EntityUtils.getPosition(current);
                if(position.distance(potentialTargetPosition) < aggroRange){
                    target = current;
                    break;
                }
            }
        }
    }
    
}
