import { BLEND_MODES, Container } from "pixi.js";
import { FurnitureLayer } from "./FurnitureLayer";
import { FurnitureGuildCustomizedVisualization } from "./visualizations/FurnitureGuildCustomizedVisualization";
/**
 * FurniturePart class that represent a furniture layer.
 *
 * @class
 * @memberof Scuti
 */
export class FurniturePart extends Container {
    /**
     * @param {FloorFurniture | WallFurniture} [furniture] - The furniture instance.
     * @param {number} [layer] - The layer id.
     */
    constructor(furniture, layer) {
        super();
        /** Store data */
        this._furniture = furniture;
        this._layer = layer;
        /** Draw the part */
        this._draw();
    }
    /**
     * Draw the part.
     *
     * @return {void}
     * @private
     */
    _draw() {
        var _a;
        /** Remove the old layer */
        this.removeChild(this.children[0]);
        /** Needed resources */
        const visualization = this._furniture.view.property.visualization;
        const spritesheet = this._furniture.view.spritesheet;
        /** Layer data */
        let alpha = 1;
        let tint;
        let z = 0;
        let blendMode;
        let flip = false;
        let frame = 0;
        let ignoreMouse = false;
        let tag;
        /** Check if the furniture support the current direction */
        if (visualization.directions.indexOf(this._furniture.direction) === -1)
            this._furniture.direction = visualization.directions[0];
        if (visualization.animation[this._furniture.state] !== undefined && visualization.animation[this._furniture.state][this._layer] !== undefined && visualization.animation[this._furniture.state][this._layer].frameSequence.length > 1)
            frame = this._frame;
        if (visualization.animation[this._furniture.state] !== undefined && visualization.animation[this._furniture.state][this._layer] !== undefined)
            frame = (_a = visualization.animation[this._furniture.state][this._layer].frameSequence[frame]) !== null && _a !== void 0 ? _a : 0;
        if (this._furniture.data.color !== null && visualization.colors[this._furniture.data.color] !== undefined && visualization.colors[this._furniture.data.color][this._layer] !== undefined)
            tint = Number('0x' + visualization.colors[this._furniture.data.color][this._layer]);
        /** Set the layer data */
        if (visualization.layers[this._layer] !== undefined) {
            if (visualization.layers[this._layer].z !== undefined)
                z = visualization.layers[this._layer].z;
            if (visualization.layers[this._layer].alpha !== undefined)
                alpha = visualization.layers[this._layer].alpha / 255;
            if (visualization.layers[this._layer].ink !== undefined)
                blendMode = BLEND_MODES[visualization.layers[this._layer].ink];
            if (visualization.layers[this._layer].ignoreMouse !== undefined)
                ignoreMouse = visualization.layers[this._layer].ignoreMouse;
            if (visualization.layers[this._layer].tag !== undefined)
                tag = visualization.layers[this._layer].tag;
        }
        if (this._furniture.view.visualization !== undefined) {
            if (this._furniture.view.visualization instanceof FurnitureGuildCustomizedVisualization) {
                if (tag === "COLOR1")
                    tint = this._furniture.view.visualization.primaryColor;
                if (tag === "COLOR2")
                    tint = this._furniture.view.visualization.secondaryColor;
            }
        }
        const name = [this._furniture.data.baseName, this._furniture.data.baseName, 64, String.fromCharCode(97 + Number(this._layer)), this._furniture.direction, frame].join("_");
        /** Calculate zOrder */
        z = (((this._furniture.roomPosition.x + this._furniture.roomPosition.y + (Math.trunc(z / 100)) / 10) * 1000000 + (this._furniture.roomPosition.z * 10000)) + 10000000 * 11);
        if (spritesheet.data.frames[name] !== undefined)
            flip = spritesheet.data.frames[name]['flipH'];
        /** Create the layer */
        this.addChild(new FurnitureLayer(this._furniture, {
            layer: String.fromCharCode(97 + Number(this._layer)),
            alpha: alpha,
            tint: tint,
            z: z,
            blendMode: blendMode,
            flip: flip,
            frame: frame,
            ignoreMouse: ignoreMouse,
            direction: this._furniture.direction,
            tag: tag
        }));
    }
    /**
     * Switch the part to the next frame and rerender it.
     *
     * @return {void}
     * @private
     */
    nextFrame() {
        const visualization = this._furniture.view.property.visualization;
        if (visualization.animation[String(this._furniture.state)] !== undefined && visualization.animation[String(this._furniture.state)][this._layer] !== undefined) {
            const frameSequence = visualization.animation[String(this._furniture.state)][this._layer].frameSequence;
            const currentFrame = this._frame;
            if (frameSequence.length > 1) {
                if ((frameSequence.length - 1) > currentFrame) {
                    this._frame = currentFrame + 1;
                }
                else {
                    this._frame = 0;
                }
                this._draw();
            }
            else {
                this._draw();
            }
        }
    }
}
//# sourceMappingURL=FurniturePart.js.map