"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const ProjectInfoItem_1 = require("./ProjectInfoItem");
const IProjectItemData_1 = require("../app/IProjectItemData");
const IInfoItemData_1 = require("./IInfoItemData");
const EntityTypeResourceDefinition_1 = require("../minecraft/EntityTypeResourceDefinition");
const BlocksCatalogDefinition_1 = require("../minecraft/BlocksCatalogDefinition");
const TerrainTextureCatalogDefinition_1 = require("../minecraft/TerrainTextureCatalogDefinition");
const ParticleEffectResourceDefinition_1 = require("../minecraft/ParticleEffectResourceDefinition");
const ItemTextureCatalogDefinition_1 = require("../minecraft/ItemTextureCatalogDefinition");
const AttachableResourceDefinition_1 = require("../minecraft/AttachableResourceDefinition");
const FlipbookTextureCatalogDefinition_1 = require("../minecraft/FlipbookTextureCatalogDefinition");
const Database_1 = require("../minecraft/Database");
const JsonUIResourceDefinition_1 = require("../minecraft/JsonUIResourceDefinition");
class TextureInfoGenerator {
    constructor() {
        this.id = "TEXTURE";
        this.title = "Texture Validation";
        this.performAddOnValidations = false;
    }
    getTopicData(topicId) {
        switch (topicId) {
            case 1:
                return { title: "Textures" };
            default:
                return { title: topicId.toString() };
        }
    }
    summarize(info, infoSet) {
        info.textureCount = infoSet.getSummedNumberValue("TEXTURE", 1);
    }
    async generate(project, contentIndex) {
        const items = [];
        const textureHandles = [];
        const allTexturePaths = [];
        const blockTextureRefs = [];
        const blockTexturePaths = [];
        const entityTexturePaths = [];
        const entityVanillaTexturePaths = [];
        const attachableTextureRefs = [];
        const particleTextureRefs = [];
        const particleTexturePaths = [];
        const particleVanillaTexturePaths = [];
        const jsonUITextureRefs = [];
        const jsonUITexturePaths = [];
        const jsonUIVanillaTexturePaths = [];
        const terrainTextureRefs = [];
        const terrainTexturePaths = [];
        const flipbookTextureRefs = [];
        const flipbookTexturePaths = [];
        const itemTexturePaths = [];
        const itemTextureVanillaPaths = [];
        const entitySpawnEggTextures = [];
        const textureCountPi = new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, 1, "Textures");
        items.push(textureCountPi);
        const itemsCopy = project.getItemsCopy();
        for (const projectItem of itemsCopy) {
            if (projectItem.itemType === IProjectItemData_1.ProjectItemType.blocksCatalogResourceJson) {
                await projectItem.ensureFileStorage();
                if (projectItem.file) {
                    const blockCat = await BlocksCatalogDefinition_1.default.ensureBlocksCatalogResourceDefinitionOnFile(projectItem.file);
                    if (blockCat && blockCat.blocksCatalog) {
                        for (const resourceId in blockCat.blocksCatalog) {
                            const resource = blockCat.blocksCatalog[resourceId];
                            if (resource && resource.textures) {
                                textureCountPi.incrementFeature("Block Resource Count");
                                if (!blockTextureRefs.includes(resourceId)) {
                                    blockTextureRefs.push(resourceId);
                                }
                                if (!allTexturePaths.includes(resource.textures)) {
                                    allTexturePaths.push(resource.textures);
                                }
                                if (!blockTexturePaths.includes(resource.textures)) {
                                    blockTexturePaths.push(resource.textures);
                                }
                            }
                        }
                    }
                }
            }
            else if (projectItem.itemType === IProjectItemData_1.ProjectItemType.particleJson) {
                await projectItem.ensureFileStorage();
                if (projectItem.file) {
                    const particleEffect = await ParticleEffectResourceDefinition_1.default.ensureOnFile(projectItem.file);
                    const desc = particleEffect?.data?.particle_effect?.description;
                    if (desc) {
                        if (desc.identifier && desc.basic_render_parameters?.texture) {
                            const texturePath = desc.basic_render_parameters.texture;
                            const matchesVanillaPath = await Database_1.default.matchesVanillaPath(desc.basic_render_parameters.texture);
                            if (!matchesVanillaPath) {
                                if (!textureHandles.includes(texturePath)) {
                                    textureHandles.push(texturePath);
                                }
                                if (!particleTextureRefs.includes(desc.identifier)) {
                                    particleTextureRefs.push(desc.identifier);
                                }
                                if (!allTexturePaths.includes(texturePath)) {
                                    allTexturePaths.push(texturePath);
                                }
                                if (!particleTexturePaths.includes(texturePath)) {
                                    particleTexturePaths.push(texturePath);
                                }
                            }
                            else if (!particleVanillaTexturePaths.includes(texturePath)) {
                                particleVanillaTexturePaths.push(texturePath);
                            }
                        }
                    }
                }
            }
            else if (projectItem.itemType === IProjectItemData_1.ProjectItemType.uiJson) {
                await projectItem.ensureFileStorage();
                if (projectItem.file) {
                    const jsonUI = await JsonUIResourceDefinition_1.default.ensureOnFile(projectItem.file);
                    const namespaceId = jsonUI?.namespaceId;
                    if (namespaceId && jsonUI.jsonUIScreen) {
                        for (const jsonControlId in jsonUI.jsonUIScreen) {
                            const jsonControl = jsonUI.jsonUIScreen[jsonControlId];
                            if (jsonControlId !== "namespace" && jsonControl && jsonControl.texture) {
                                const texturePath = jsonControl.texture;
                                const matchesVanillaPath = await Database_1.default.matchesVanillaPath(texturePath);
                                if (!matchesVanillaPath) {
                                    if (!textureHandles.includes(texturePath)) {
                                        textureHandles.push(texturePath);
                                    }
                                    if (!jsonUITextureRefs.includes(jsonControlId)) {
                                        jsonUITextureRefs.push(jsonControlId);
                                    }
                                    if (!allTexturePaths.includes(texturePath)) {
                                        allTexturePaths.push(texturePath);
                                    }
                                    if (!jsonUITexturePaths.includes(texturePath)) {
                                        jsonUITexturePaths.push(texturePath);
                                    }
                                }
                                else if (!jsonUIVanillaTexturePaths.includes(texturePath)) {
                                    jsonUIVanillaTexturePaths.push(texturePath);
                                }
                            }
                        }
                    }
                }
            }
            else if (projectItem.itemType === IProjectItemData_1.ProjectItemType.terrainTextureCatalogResourceJson) {
                await projectItem.ensureFileStorage();
                if (projectItem.file) {
                    const terrainTextureCat = await TerrainTextureCatalogDefinition_1.default.ensureOnFile(projectItem.file);
                    if (terrainTextureCat && terrainTextureCat.data && terrainTextureCat.data.texture_data) {
                        for (const terrainTextureId in terrainTextureCat.data.texture_data) {
                            const terrainTexture = terrainTextureCat.data.texture_data[terrainTextureId];
                            if (terrainTexture && terrainTexture.textures) {
                                textureCountPi.incrementFeature("Terrain Texture Resource Count");
                                if (!terrainTextureRefs.includes(terrainTextureId)) {
                                    terrainTextureRefs.push(terrainTextureId);
                                }
                                if (typeof terrainTexture.textures === "string") {
                                    if (!terrainTexturePaths.includes(terrainTexture.textures)) {
                                        terrainTexturePaths.push(terrainTexture.textures);
                                    }
                                } /*else if (terrainTexture.textures) {
                                  for (let str of terrainTexture.textures) {
                                    if (!terrainTexturePaths.includes(str)) {
                                      terrainTexturePaths.push(str);
                                    }
                                  }
                                }*/
                            }
                        }
                    }
                }
            }
            else if (projectItem.itemType === IProjectItemData_1.ProjectItemType.flipbookTexturesJson) {
                await projectItem.ensureFileStorage();
                if (projectItem.file) {
                    const flipbookTexturesCat = await FlipbookTextureCatalogDefinition_1.default.ensureOnFile(projectItem.file);
                    if (flipbookTexturesCat && flipbookTexturesCat && flipbookTexturesCat.data) {
                        const pathId = projectItem.file.storageRelativePath + "_flipbooktextures";
                        if (!allTexturePaths.includes(pathId)) {
                            allTexturePaths.push(pathId);
                        }
                        for (const flipbookTexture of flipbookTexturesCat.data) {
                            if (flipbookTexture && flipbookTexture.flipbook_texture) {
                                textureCountPi.incrementFeature("Flipbook Texture Resource Count");
                                // every flipbook texture is reserved as a handle; the current "page" in the "flipbook" is atlas'ed in one texture per world.
                                if (!textureHandles.includes(flipbookTexture.flipbook_texture)) {
                                    textureHandles.push(flipbookTexture.flipbook_texture);
                                }
                                if (!flipbookTextureRefs.includes(flipbookTexture.atlas_tile)) {
                                    flipbookTextureRefs.push(flipbookTexture.atlas_tile);
                                }
                                if (!flipbookTexturePaths.includes(flipbookTexture.flipbook_texture)) {
                                    flipbookTexturePaths.push(flipbookTexture.flipbook_texture);
                                }
                            }
                        }
                    }
                }
            }
            else if (projectItem.itemType === IProjectItemData_1.ProjectItemType.itemTextureJson) {
                await projectItem.ensureFileStorage();
                if (projectItem.file) {
                    const itemTextureCat = await ItemTextureCatalogDefinition_1.default.ensureOnFile(projectItem.file);
                    if (itemTextureCat && itemTextureCat.data && itemTextureCat.data.texture_data) {
                        for (const itemTextureId in itemTextureCat.data.texture_data) {
                            const itemTexture = itemTextureCat.data.texture_data[itemTextureId];
                            if (itemTexture && itemTexture.textures) {
                                textureCountPi.incrementFeature("Item Texture Resource Count");
                                if (itemTexture.textures) {
                                    if (typeof itemTexture.textures === "string") {
                                        const matchesVanillaPath = await Database_1.default.matchesVanillaPath(itemTexture.textures);
                                        if (!matchesVanillaPath && !itemTexturePaths.includes(itemTexture.textures)) {
                                            itemTexturePaths.push(itemTexture.textures);
                                        }
                                        else if (matchesVanillaPath && !itemTextureVanillaPaths.includes(itemTexture.textures)) {
                                            itemTextureVanillaPaths.push(itemTexture.textures);
                                        }
                                    }
                                    else if (itemTexture.textures.constructor === Array) {
                                        for (let str of itemTexture.textures) {
                                            const matchesVanillaPath = await Database_1.default.matchesVanillaPath(str);
                                            if (!matchesVanillaPath && !itemTexturePaths.includes(str)) {
                                                itemTexturePaths.push(str);
                                            }
                                            else if (matchesVanillaPath && !itemTextureVanillaPaths.includes(str)) {
                                                itemTextureVanillaPaths.push(str);
                                            }
                                        }
                                    }
                                    else if (itemTexture.textures.path) {
                                        const texturePath = itemTexture.textures.path;
                                        const matchesVanillaPath = await Database_1.default.matchesVanillaPath(texturePath);
                                        if (!matchesVanillaPath && !itemTexturePaths.includes(texturePath)) {
                                            itemTexturePaths.push(texturePath);
                                        }
                                        else if (matchesVanillaPath && !itemTextureVanillaPaths.includes(texturePath)) {
                                            itemTextureVanillaPaths.push(texturePath);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (projectItem.itemType === IProjectItemData_1.ProjectItemType.entityTypeResource) {
                textureCountPi.incrementFeature("Entity Resource Count");
                await projectItem.ensureFileStorage();
                if (projectItem.file) {
                    const entityTypeResourceDef = await EntityTypeResourceDefinition_1.default.ensureOnFile(projectItem.file);
                    if (entityTypeResourceDef?.data) {
                        const desc = entityTypeResourceDef.data;
                        const textures = desc.textures;
                        if (textures) {
                            let textureCount = 0;
                            for (const texture in textures) {
                                const texturePath = textures[texture];
                                if (texturePath) {
                                    const matchesVanillaPath = await Database_1.default.matchesVanillaPath(texturePath);
                                    if (!matchesVanillaPath) {
                                        if (!textureHandles.includes(texturePath)) {
                                            textureHandles.push(texturePath);
                                        }
                                        if (!entityTexturePaths.includes(texturePath)) {
                                            entityTexturePaths.push(texturePath);
                                        }
                                        const tex = textures[texture];
                                        if (tex && !allTexturePaths.includes(tex)) {
                                            allTexturePaths.push(tex);
                                        }
                                    }
                                    else if (!entityVanillaTexturePaths.includes(texturePath)) {
                                        entityVanillaTexturePaths.push(texturePath);
                                    }
                                }
                                textureCount++;
                            }
                            textureCountPi.incrementFeature("Texture References", "Count", textureCount);
                            textureCountPi.incrementFeature("Entity References", "Count", textureCount);
                        }
                    }
                }
            }
            else if (projectItem.itemType === IProjectItemData_1.ProjectItemType.attachableResourceJson) {
                textureCountPi.incrementFeature("Attachable Resource Count");
                await projectItem.ensureFileStorage();
                if (projectItem.file) {
                    const attachableResourceDef = await AttachableResourceDefinition_1.default.ensureOnFile(projectItem.file);
                    if (attachableResourceDef?.data) {
                        const desc = attachableResourceDef.data;
                        const textures = desc.textures;
                        if (textures) {
                            let textureCount = 0;
                            for (const texture in textures) {
                                const texturePath = textures[texture];
                                if (texturePath) {
                                    if (!textureHandles.includes(texturePath)) {
                                        textureHandles.push(texturePath);
                                    }
                                    if (!attachableTextureRefs.includes(texturePath)) {
                                        attachableTextureRefs.push(texturePath);
                                    }
                                    if (!allTexturePaths.includes(texturePath)) {
                                        allTexturePaths.push(texturePath);
                                    }
                                    textureCount++;
                                }
                            }
                            textureCountPi.incrementFeature("Texture References", "Count", textureCount);
                            textureCountPi.incrementFeature("Attachable References", "Count", textureCount);
                        }
                    }
                }
            }
            if (projectItem.itemType === IProjectItemData_1.ProjectItemType.texture || projectItem.itemType === IProjectItemData_1.ProjectItemType.uiTexture) {
                await projectItem.ensureFileStorage();
                if (projectItem.file) {
                    textureCountPi.incrementFeature("File Count");
                }
            }
        }
        if (textureHandles.length > 0) {
            textureCountPi.incrementFeature("Unique Texture Handles (estimated)", "Count", textureHandles.length);
        }
        if (allTexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique Texture Paths", "Count", allTexturePaths.length);
        }
        if (particleTextureRefs.length > 0) {
            textureCountPi.incrementFeature("Unique Particle Texture References", "Count", particleTextureRefs.length);
        }
        if (particleVanillaTexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique Particle Texture Paths", "Count", particleTexturePaths.length);
        }
        if (particleVanillaTexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique Particle Texture Vanilla Paths", "Count", particleVanillaTexturePaths.length);
        }
        if (jsonUITextureRefs.length > 0) {
            textureCountPi.incrementFeature("Unique JSON UI Texture References", "Count", jsonUITextureRefs.length);
        }
        if (jsonUITexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique JSON UI Texture Paths", "Count", jsonUITexturePaths.length);
        }
        if (jsonUIVanillaTexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique JSON UI Texture Vanilla Paths", "Count", jsonUIVanillaTexturePaths.length);
        }
        if (entityTexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique Entity Texture Paths", "Count", entityTexturePaths.length);
        }
        if (entityVanillaTexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique Entity Texture Vanilla Paths", "Count", entityVanillaTexturePaths.length);
        }
        if (attachableTextureRefs.length > 0) {
            textureCountPi.incrementFeature("Unique Attachable Texture References", "Count", attachableTextureRefs.length);
        }
        if (terrainTextureRefs.length > 0) {
            textureCountPi.incrementFeature("Unique Terrain Texture References", "Count", terrainTextureRefs.length);
        }
        if (terrainTexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique Terrain Texture Paths", "Count", terrainTexturePaths.length);
        }
        if (itemTexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique Item Texture Paths", "Count", itemTexturePaths.length);
        }
        if (flipbookTextureRefs.length > 0) {
            textureCountPi.incrementFeature("Unique Flipbook Texture References", "Count", flipbookTextureRefs.length);
        }
        if (flipbookTexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique Flipbook Texture Paths", "Count", flipbookTexturePaths.length);
        }
        if (blockTextureRefs.length > 0) {
            textureCountPi.incrementFeature("Unique Block Texture References", "Count", blockTextureRefs.length);
        }
        if (blockTexturePaths.length > 0) {
            textureCountPi.incrementFeature("Unique Block Texture Paths", "Count", blockTexturePaths.length);
        }
        if (entitySpawnEggTextures.length > 0) {
            textureCountPi.incrementFeature("Unique Entity Spawn Egg Texture References", "Count", entitySpawnEggTextures.length);
        }
        if (this.performAddOnValidations && textureHandles.length > 800) {
            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, 100, "Uses more than 800 texture handles, which could impact overall Minecraft usage", undefined, textureHandles.length));
        }
        return items;
    }
}
exports.default = TextureInfoGenerator;

//# sourceMappingURL=../maps/info/TextureInfoGenerator.js.map
