import { BC_NPC_LAYER, BC_SPRITES_SETTINGS } from "../GlobalVariables/GlobalVariables";
import { Vault } from "../Utils/DataTypes.utils";
import { getSpriteFromAtlas } from "../Utils/Sprites.utils";
import { WORLD_CHUNKS, worldCoordinatesToChunkIndex, worldCoordinatesToChunkIndexesCoordinates } from "../WorldGeneration/WorldGeneration";
import * as PIXI from "../../pixi/pixi.mjs";
import { addToViewport } from "../Utils/World.utils";

export const NPCVault = new Vault();
let lastNPCId = -1;

export class NPC
{
    /**
     * 
     * @param {Position} param0 
     * @param {Position} param1 
     * @param {NPCProps} props 
     */
    constructor(x, y, x_c, y_c, spriteRef, id, props = {})
    {
        this.position = {x, y};
        this.ceiledPosition = {x_c, y_c};
        // this.spriteRef = spriteRef;
        // this.id = id;
        this.props = {spriteRef, id, classRef: this, ...props};
    }

    setProps(props)
    {
        this.props = {...this.props, ...props};
    }

    tick(ticker)
    {

    }
};

export class NPCProps
{
    constructor({type})
    {
        this.type = type;
    }
};

/**
 * 
 * @param {Number} x 
 * @param {Number} y 
 * @param {NPCProps} props 
 */
export function addNPCToWorld(x, y, props={}) {
    let pos = worldCoordinatesToChunkIndexesCoordinates(x, y);
    let sprite = getSpriteFromAtlas("assets/images/characters/char0.png", new PIXI.Rectangle(0,0,16,16));
    sprite.position.set(pos.x *BC_SPRITES_SETTINGS.defaultSize *BC_SPRITES_SETTINGS.scale, pos.y *BC_SPRITES_SETTINGS.defaultSize *BC_SPRITES_SETTINGS.scale);
    sprite.scale.set(BC_SPRITES_SETTINGS.scale, BC_SPRITES_SETTINGS.scale);
    sprite.zIndex = 100;
    // addToViewport(sprite);
    lastNPCId++;
    let npc = new NPC(x, y, pos.x, pos.y, sprite, lastNPCId, props);
    NPCVault.set(lastNPCId, npc);
    BC_NPC_LAYER.addChild(sprite);
    console.log(BC_NPC_LAYER);
}

export function npcTickHandler(ticker)
{
}