package electrosphere.server.ai.creature;

import java.util.Random;

import org.joml.Quaterniond;
import org.joml.Vector3d;

import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityTags;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.block.ServerBlockTree;
import electrosphere.entity.types.creature.CreatureUtils;
import electrosphere.game.data.creature.type.ai.BlockerTreeData;
import electrosphere.server.ai.AITree;
import electrosphere.server.datacell.Realm;
import electrosphere.server.datacell.utils.DataCellSearchUtils;
import electrosphere.util.math.MathUtils;

/**
 * AI that just continuously blocks
 */
public class Blocker implements AITree {

    /**
     * The name of the tree type
     */
    public static final String TREE_NAME = "Blocker";

    /**
     * The configuring tree data for this tree
     */
    BlockerTreeData treeData;

    /**
     * The parent to this tree
     */
    Entity parent;

    /**
     * The current target of this tree
     */
    Entity target = null;


    /**
     * The number of frames the tree has been in its current state
     */
    int currentStateFrameCount = 0;

    /**
     * The random used for rolls on behavior transitions
     */
    Random random = new Random();

    /**
     * Private constructor
     * @param parent The parent entity
     */
    private Blocker(Entity parent){
        this.parent = parent;
    }

    /**
     * Constructs an attacker tree
     * @param parent The parent entity
     * @param treeData The tree data
     * @return The Attacker tree
     */
    public static AITree construct(Entity parent, BlockerTreeData treeData) {
        Blocker rVal = new Blocker(parent);
        rVal.treeData = treeData;
        return rVal;
    }

    @Override
    public void simulate() {
        if(target == null){
            searchForTarget();
        }
        if(ServerBlockTree.getServerBlockTree(parent) != null){
            ServerBlockTree serverBlockTree = ServerBlockTree.getServerBlockTree(parent);
            if(!serverBlockTree.isBlocking()){
                serverBlockTree.start();
            }
        }
        if(target != null){
            Vector3d targetPos = EntityUtils.getPosition(target);
            Quaterniond rotation = MathUtils.calculateRotationFromPointToPoint(EntityUtils.getPosition(parent), targetPos);
            EntityUtils.getRotation(parent).set(rotation);
        }
    }

    @Override
    public String getTreeName() {
        return TREE_NAME;
    }

    @Override
    public void updateStateAndPriority() {
    }

    @Override
    public String getCurrentStateName() {
        return "BLCOKING";
    }

    @Override
    public int getCurrentStatePriority() {
        return 1;
    }

    /**
     * Searches for a valid target
     */
    private 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 && CreatureUtils.isCreature(current)){
                target = current;
                break;
            }
        }
    }
    
}
