import GraphicSet from "../../js/GraphicSet.js";
import Setting from "../../js/Setting.js";

export default class Tileset
{
    /**
     * @param {number} setId 
     */
    constructor(setId)
    {
        /** @type {number} */
        this.setId = setId;

        /** @type {Image} */
        this.image = new Image();
        this.image.src = '../' + Setting.TILESET_LOCATION + GraphicSet[this.setId].tileset;

        /** @type {number} */
        this.tiles = GraphicSet[this.setId].tiles;

        /** @type {number} */
        this.scale = GraphicSet[this.setId].scale;

        /** @type {number} */
        this.primaryTiles = GraphicSet[this.setId].primaryTiles;

        /** @type {{image: Image, tiles: number, scale: number}|null} */
        this.background = null;

        if (GraphicSet[this.setId].tilesetBackground !== null) {
            const backgroundImage = new Image();
            backgroundImage.src = '../' + Setting.TILESET_LOCATION + 
                GraphicSet[this.setId].tilesetBackground.path;

            this.background = {
                image: backgroundImage,
                tiles: GraphicSet[this.setId].tilesetBackground.tiles,
                scale: GraphicSet[this.setId].tilesetBackground.scale,
            };
        }
    }

    /** @returns {number} */
    getWidth()
    {
        return this.image.width * this.scale;
    }

    /** @returns {number} */
    getHeight()
    {
        return this.image.height * this.scale;
    }

    /** @returns {number} */
    getTileWidth()
    {
        return this.image.width / this.tiles * this.scale;
    }

    /** @returns {number} */
    getTileHeight()
    {
        return this.image.height * this.scale;
    }

    /** @returns {boolean} */
    hasExtendedTiles()
    {
        return GraphicSet[this.setId].tiles > GraphicSet[this.setId].primaryTiles ;
    }

    /** @returns {number} */
    getTileIndexFactor(code)
    {
        const CODES = ['ltr', 't', 'r', 'b', 'l', 'lt', 'tr', 'ltb', 'tb', 'trb', 'lb', 'rb', 'ltrb', 'lr', 'lrb'];

        return CODES.indexOf(code) + 1;
    }
}
