import { GameObject } from "../../GameObject/GameObject";
import { NPCTask_moveToTile } from "../../NPCTasks/Default/DefaultNPCTasks";
import { PointInt2D } from "../../Utils/Math.utils";
import { NavigationPath, PathFinder } from "../../Utils/PathFinding.utils";
import { findPathOnNavigationGridIfExists } from "../../World/NavigationGrid/NavigationGrid";
import { NPCAction, NPCActionCallbackResult, NPCBehavior, NPCTask } from "../NPCBehavior/NPCBehavior";
import { NPCProto } from "../NPCProto/NPCProto";

/**
 *
 * @param {Number} result
 */
function NavigationCallbackFunctor(result) {}

/**
 * NPCController defines NPC behavior. Many NPC can have same NPCController for the same behavior.
 */
export class NPCController extends GameObject {
    /**
     * NPC controlled by this controller
     * @type NPCProto
     */
    controlledNPC = null;

    /**
     * @type Array<PointInt2D>
     */
    navigationPathQueue = new Array();
    navigationInProgress = false;
    navigationFollowMidPoint = false;
    navigationCallback = () => {};

    behavior = new NPCBehavior(this, () => {});

    constructor(tickAble = true) {
        super(tickAble);
        this.behavior.addTask("MoveToCursor", NPCTask_moveToTile);
    }

    /**
     * moves NPC to position. callback contains result status: -1 - error happened; 0 - success; 1 - unreachable
     * @param {PointInt2D} position
     * @param {NavigationCallbackFunctor} callback
     */
    moveTo(position, callback) {
        // let pf = new PathFinder();
        // let nPath = pf.findPathIfExist(new PointInt2D(this.controlledNPC.worldPosition.getX(), this.controlledNPC.worldPosition.getY()), position);
        let nPath = findPathOnNavigationGridIfExists(
            new PointInt2D(this.controlledNPC.worldPosition.getX(), this.controlledNPC.worldPosition.getY()),
            position
        );
        nPath.then((r) => {
            if (r.error) {
                this.controlledNPC.isMoving = false;
                callback(-1); //error
                return;
            } else if (r.state === 0) {
                this.controlledNPC.isMoving = false;
                callback(1); //unreachable
                return;
            }
            this.navigationPathQueue = [];
            for (let i = r.result.path.length - 1; i > 0; i--) {
                this.navigationPathQueue.push(r.result.path[i]);
            }
            this.navigationCallback = callback;
            this.navigationInProgress = true;
            this.controlledNPC.isMoving = true;
        });
        // console.log("boba");
        // console.log(nPath);
    }

    /**
     *
     * @param {PointInt2D} pos
     */
    startTask(pos) {
        this.behavior.abortCurrentTask();
        this.behavior.changeTask("MoveToCursor", (task)=>{
            task.actionsContainer[0].targetPosition = pos;
        });
        this.behavior.startTask("MoveToCursor");
    }

    tick(ticker) {
        // this.taskInstanceRef.handleTaskTick(ticker);
        this.behavior.handleNPCBehaviorTick(ticker);
        if (this.navigationInProgress) {
            if (!this.navigationFollowMidPoint) {
                let target = this.navigationPathQueue.pop();
                if (!target) {
                    this.navigationInProgress = false;
                    this.controlledNPC.isMoving = false;
                    this.navigationCallback(0); //success
                } else {
                    this.controlledNPC.worldPosition = target;
                    this.navigationFollowMidPoint = true;
                }
            } else {
                if (
                    Math.abs(this.controlledNPC.drawObject.x - this.controlledNPC.worldPosition.getX()) < 0.5 &&
                    Math.abs(this.controlledNPC.drawObject.y - this.controlledNPC.worldPosition.getY()) < 0.5
                ) {
                    this.navigationFollowMidPoint = false;
                }
            }
        }
    }
}
