import { RoomObject } from "../rooms/RoomObject";
import { AvatarAction } from "./actions/AvatarAction";
import { gsap } from "gsap";
import { AvatarActionManager } from "./actions/AvatarActionManager";
import { AvatarAnimationManager } from "./animations/AvatarAnimationManager";
import { AvatarBodyPart } from "./AvatarBodyPart";
import { AvatarLayer } from "./AvatarLayer";
import { Assets } from "pixi.js";
import { InteractionManager } from "../interactions/InteractionManager";
import { AssetLoader } from "../../utilities/AssetLoader";
export class Avatar extends RoomObject {
    constructor(configuration) {
        super();
        this._interactionManager = new InteractionManager();
        this._bodyParts = [];
        this.areAllAssetsLoaded = false;
        this._figure = this._parseFigure(configuration.figure);
        this._position = configuration.position;
        this._headDirection = configuration.headDirection;
        this._bodyDirection = configuration.bodyDirection;
        this._actions = configuration.actions;
        this._handItem = configuration.handItem;
        let assets = [];
        if (this._handItem !== 0 || this._handItem !== undefined) {
            assets.push(AssetLoader.load("figures/hh_human_item", "figure/hh_human_item/hh_human_item.json"));
        }
        assets.push(AssetLoader.load("figures/hh_human_body", "figure/hh_human_body/hh_human_body.json"));
        this._actionManager = new AvatarActionManager(AvatarAction.Default);
        this._animationManager = new AvatarAnimationManager();
        this._actions.forEach((action) => this._animationManager.registerAnimation(action));
        this._figure.forEach((set, type) => {
            const parts = this._getParts(type, set.setId);
            this._bodyParts.push(new AvatarBodyPart(this, {
                type: type,
                setId: set.setId,
                colors: set.colors,
                parts: parts,
                actions: this._actions
            }));
        });
        //this.interactive = true;
        Promise.all(assets).then(() => {
            this.areAllAssetsLoaded = true;
        });
    }
    _draw() {
        this._destroyParts();
        if (!this.areAllAssetsLoaded) {
            return;
        }
        if (this._handItem !== 0 || this._handItem !== undefined) {
            this._createHandItem(this._handItem);
        }
        this._createShadow();
        this._bodyParts.forEach((bodyPart) => bodyPart.updateParts());
        this.x = 32 * this._position.x - 32 * this._position.y;
        this.y = 16 * this._position.x + 16 * this._position.y - 32 * this._position.z;
    }
    _createHandItem(item) {
        const handItemCarryId = this._actionManager.getActionDefinition(AvatarAction.CarryItem).params[String(item)];
        const handItemUseId = this._actionManager.getActionDefinition(AvatarAction.UseItem).params[String(item)];
        if (this._actions.includes(AvatarAction.UseItem) && handItemUseId !== undefined) {
            this.addChild(new AvatarLayer(this, {
                type: "ri",
                part: { id: handItemUseId, lib: { id: "hh_human_item" } },
                gesture: "drk",
                tint: undefined,
                z: 1000,
                flip: false,
                direction: this._bodyDirection,
                frame: 0
            }));
        }
        else if (this._actions.includes(AvatarAction.CarryItem) && handItemCarryId !== undefined) {
            this.addChild(new AvatarLayer(this, {
                type: "ri",
                part: { id: handItemCarryId, lib: { id: "hh_human_item" } },
                gesture: "crr",
                tint: undefined,
                z: 1000,
                flip: false,
                direction: this._bodyDirection,
                frame: 0
            }));
        }
    }
    _createPlaceholder() {
    }
    _createShadow() {
        this.addChild(new AvatarLayer(this, {
            type: "sd",
            part: { id: 1, lib: { id: "hh_human_body" } },
            gesture: "std",
            tint: undefined,
            z: 0,
            flip: true,
            direction: 0,
            frame: 0,
            alpha: 0.1
        }));
    }
    _destroyParts() {
        while (this.children[0]) {
            this.removeChild(this.children[0]);
        }
    }
    _parseFigure(figure) {
        return new Map(figure.split(".").map(part => {
            const data = part.split("-");
            return [
                data[0],
                {
                    setId: Number(data[1]),
                    colors: data.splice(2, 2).map(color => {
                        return Number(color);
                    })
                },
            ];
        }));
    }
    _getParts(type, setId) {
        var _a, _b;
        const figureData = Assets.get('figures/figuredata');
        const figureMap = Assets.get('figures/figuremap');
        const hiddenLayers = (_a = figureData.settype[type]) === null || _a === void 0 ? void 0 : _a.set[setId]["hiddenLayers"];
        let parts = [];
        let set = (_b = figureData.settype[type]) === null || _b === void 0 ? void 0 : _b.set[setId];
        set === null || set === void 0 ? void 0 : set.parts.forEach((part) => {
            let libId = figureMap.parts[part.type][String(part.id)];
            let lib = figureMap.libs[libId];
            //console.log(part.type, libId);
            part.lib = lib;
            parts.push(part);
        });
        if (hiddenLayers !== undefined) {
            parts = parts.filter(part => !hiddenLayers.includes(part.type));
        }
        return parts;
    }
    /**
     * On each animation tick
     * @private
     */
    _onTicker() {
        this._draw();
    }
    /**
     * Start the furniture animation
     */
    start() {
        this.animationTicker.add(() => this._onTicker());
    }
    /**
     * Stop the furniture animation
     */
    stop() {
        this.animationTicker.remove(() => this._onTicker());
    }
    get pos() {
        return this._position;
    }
    set pos(position) {
        gsap.to(this, {
            x: 32 * position.x - 32 * position.y, y: 16 * position.x + 16 * position.y - 32 * position.z, duration: 0.5, ease: "linear", onComplete: () => {
                this._position = position;
            }
        });
    }
    get actions() {
        return this._actions;
    }
    get headDirection() {
        return this._headDirection;
    }
    get bodyDirection() {
        return this._bodyDirection;
    }
    get actionManager() {
        return this._actionManager;
    }
    get animationManager() {
        return this._animationManager;
    }
    get interactionManager() {
        return this._interactionManager;
    }
    get onPointerDown() {
        return this._interactionManager.onPointerDown;
    }
    set onPointerDown(value) {
        this._interactionManager.onPointerDown = value;
    }
    get onPointerUp() {
        return this._interactionManager.onPointerUp;
    }
    set onPointerUp(value) {
        this._interactionManager.onPointerUp = value;
    }
    get onPointerMove() {
        return this._interactionManager.onPointerMove;
    }
    set onPointerMove(value) {
        this._interactionManager.onPointerMove = value;
    }
    get onPointerOut() {
        return this._interactionManager.onPointerOut;
    }
    set onPointerOut(value) {
        this._interactionManager.onPointerOut = value;
    }
    get onPointerOver() {
        return this._interactionManager.onPointerOver;
    }
    set onPointerOver(value) {
        this._interactionManager.onPointerOver = value;
    }
    get onDoubleClick() {
        return this._interactionManager.onDoubleClick;
    }
    set onDoubleClick(value) {
        this._interactionManager.onDoubleClick = value;
    }
}
//# sourceMappingURL=Avatar.js.map