import { FurniturePart } from "./FurniturePart";
import { Assets, Container, Sprite } from "pixi.js";
import { FurnitureLayer } from "./FurnitureLayer";
import { FurnitureGuildCustomizedVisualization } from "./visualizations/FurnitureGuildCustomizedVisualization";
import { AssetLoader } from "../../utilities/AssetLoader";
/**
 * FurnitureView class that manage all the rendering part of the furniture.
 *
 * @class
 * @memberof Scuti
 */
export class FurnitureView extends Container {
    /**
     * @param {FloorFurniture | WallFurniture} [furniture] - The furniture instance to render.
     */
    constructor(furniture) {
        super();
        /**
         * A list containing all the furnitures parts.
         *
         * @member {FurniturePart[]}
         * @private
         */
        this._parts = [];
        /** Store data */
        this._furniture = furniture;
        /** Load the spritesheet */
        AssetLoader.load("furnitures/" + this._furniture.data.baseName, "furniture/" + this._furniture.data.baseName + "/" + this._furniture.data.baseName + ".json", () => {
            this._createPlaceholder();
        }).then(() => {
            this._spritesheet = Assets.get("furnitures/" + this._furniture.data.baseName);
            this._property = this._spritesheet.data["furniProperty"];
            this._initialiseVisualization();
            this._draw();
        });
    }
    /**
     * Initialise furniture visualization.
     *
     * @return {void}
     * @private
     */
    _initialiseVisualization() {
        if (this._property.infos.visualization === "furniture_guild_customized")
            this._visualization = new FurnitureGuildCustomizedVisualization(this._furniture);
    }
    /**
     * Draw the furniture visualization with all the parts.
     *
     * @return {void}
     * @private
     */
    _draw() {
        this._destroyParts();
        this._createShadow();
        for (let i = 0; i < this._property.visualization.layerCount; i++) {
            this._createPart(i);
        }
    }
    /**
     * Destroy all the furniture parts.
     *
     * @return {void}
     * @private
     */
    _destroyParts() {
        [...this._parts].forEach((part) => part.destroy());
        this._parts = [];
        this.removeChild(this.children[0]);
    }
    /**
     * Rerender all the room visualization.
     *
     * @return {void}
     * @private
     */
    update() {
        this._draw();
    }
    /**
     * The method is called when the room animation ticker tick.
     *
     * @return {void}
     * @private
     */
    tick() {
        this._parts.forEach((part) => part.nextFrame());
    }
    /**
     * Create a furniture part by it's layer id.
     *
     * @param {number} [layer] - The layer id that we want to render.
     * @return {void}
     * @private
     */
    _createPart(layer) {
        const part = new FurniturePart(this._furniture, layer);
        this._parts.push(part);
        this.addChild(part);
    }
    /**
     * Create the furniture placeholder while the spritesheet is loading.
     *
     * @return {void}
     * @private
     */
    _createPlaceholder() {
        const placeholder = new Sprite(Assets.get("furnitures/floor/placeholder").textures["place_holder_furniture_64.png"]);
        this.addChild(placeholder);
        placeholder.x = -32;
        placeholder.y = -50;
    }
    /**
     * Create the furniture shadow.
     *
     * @return {void}
     * @private
     */
    _createShadow() {
        this.addChild(new FurnitureLayer(this._furniture, {
            layer: "sd",
            alpha: 0.1,
            tint: undefined,
            z: 0,
            blendMode: undefined,
            flip: false,
            frame: 0,
            ignoreMouse: true,
            direction: 0
        }));
    }
    /**
     * Reference to the furniture spritesheet.
     *
     * @member {Spritesheet}
     * @readonly
     * @public
     */
    get spritesheet() {
        return this._spritesheet;
    }
    /**
     * Reference to the furniture properties.
     *
     * @member {IFurnitureProperty}
     * @readonly
     * @public
     */
    get property() {
        return this._property;
    }
    /**
     * Reference to the furniture visualization.
     *
     * @member {FurnitureVisualization}
     * @readonly
     * @public
     */
    get visualization() {
        return this._visualization;
    }
}
//# sourceMappingURL=FurnitureView.js.map