// priority: 200
// By Tonywww, 原始用途：千界万锻整合包

let tetraMaterialTagEvent = null;
let tetraMaterialTagValues = {};
let activeTetraMaterialStage = null;

const withTetraMaterialStage = (stage, buildMaterials) => {
    let previousStage = activeTetraMaterialStage;
    activeTetraMaterialStage = stage;
    try {
        buildMaterials();
        writeTetraMaterialStageTag(stage);
    } finally {
        activeTetraMaterialStage = previousStage;
    }
};

const writeTetraMaterialStageTag = stage => {
    let tagName = `stage_${stage}`;
    let values = tetraMaterialTagValues[tagName];
    if (!tetraMaterialTagEvent || !values) return;

    tetraMaterialTagEvent.addJson(`sdbf:tags/items/tetra_material/${tagName}.json`, {
        replace: false,
        values: values
    });
};

const addTetraMaterialToStageTag = (event, stage, materialItems, materialTag) => {
    if (stage === undefined || stage === null) return;

    if (tetraMaterialTagEvent !== event) {
        tetraMaterialTagEvent = event;
        tetraMaterialTagValues = {};
    }

    let tagName = `stage_${stage}`;
    let values = tetraMaterialTagValues[tagName] || [];
    materialItems.forEach(item => {
        if (values.indexOf(item) === -1) values.push(item);
    });
    if (materialTag) {
        let tagValue = materialTag.charAt(0) === "#" ? materialTag : `#${materialTag}`;
        if (values.indexOf(tagValue) === -1) values.push(tagValue);
    }
    tetraMaterialTagValues[tagName] = values;
};

const tetraMaterialBuilder = (event, id, stage) => {
    if (stage === undefined) stage = activeTetraMaterialStage;

    let key = id;
    let category = "metal";
    let primary = 0;
    let secondary = 0;
    let tertiary = 0;
    let durability = 0;
    let integrityCost = 0;
    let integrityGain = 0;
    let magicCapacity = 0;
    let toolLevel = "minecraft:netherite";
    let toolEfficiency = 0;
    let experienceCost = 0;
    let replace = true;
    let hidden = false;
    let hiddenOutcomes = false;

    let tints = {
        "glyph": "",
        "texture": ""
    };
    let textures = [];
    let material = {
        "count": 1
    };
    let materialItems = [];
    let requiredTools = {};
    let effects = {};
    let attributes = {};
    let improvements = {};
    let tags = undefined;
    let features = undefined;
    let enchantments = {};
    let countFactor = 1;
    let builder = {
        setCategory(str) { category = str; return builder; },
        setPrimary(num) { primary = num; return builder; },
        setSecondary(num) { secondary = num; return builder; },
        setTertiary(num) { tertiary = num; return builder; },
        setDurability(int) { durability = int; return builder; },
        setIntegrityCost(int) { integrityCost = int; return builder; },
        setIntegrityGain(int) { integrityGain = int; return builder; },
        setMagicCapacity(int) { magicCapacity = int; return builder; },
        setToolLevel(str) { toolLevel = str; return builder; },
        setToolEfficiency(int) { toolEfficiency = int; return builder; },
        setTints(glyph, texture) { tints.glyph = glyph; tints.texture = texture; return builder; },
        addTexture(texture) { textures.push(texture); return builder; },
        addItemMaterial(item) { materialItems.push(item); return builder; },
        setTagMaterial(tag) { material["tag"] = tag; return builder; },
        setMaterialNBT(any) { material["nbt"] = any; return builder; },
        addMaterialCount(num) { material["count"] = num; return builder; },
        setRequiredTool(tool, level) { requiredTools[tool] = level; return builder; },
        setReplace(bool) { replace = bool; return builder; },
        setHidden(bool) { hidden = bool; return builder; },
        setHiddenOutcomes(bool) { hiddenOutcomes = bool; return builder; },
        addEffects(eff, lst) { effects[eff] = lst; return builder; },
        addAttributes(att, num) { attributes[att] = num; return builder; },
        addImprovements(str, any) { improvements[str] = any; return builder; },
        setExperienceCost(num) { experienceCost = num; return builder; },
        setTags(lst) { tags = lst; return builder; },
        setFeatures(lst) { features = lst; return builder; },
        addEnchantment(id, lvl) { enchantments[id] = lvl; return builder; },
        setCountFactor(num) { countFactor = num; return builder; },

        build() {

            let json = {
                key: key,
                category: category,
                primary: primary,
                secondary: secondary,
                tertiary: tertiary,
                durability: durability,
                integrityCost: integrityCost,
                integrityGain: integrityGain,
                magicCapacity: magicCapacity,
                toolLevel: toolLevel,
                toolEfficiency: toolEfficiency,
                experienceCost: experienceCost,
                replace: replace,
                hidden: hidden,
                hiddenOutcomes: hiddenOutcomes,
                tints: tints,
                textures: textures,
                material: material,
                requiredTools: requiredTools,
                effects: effects,
                attributes: attributes,
                countFactor: countFactor,
            };
            if (features) json["features"] = features;
            if (enchantments) json["enchantments"] = enchantments;
            if (materialItems.length > 0) json["material"]["items"] = materialItems;
            if (improvements != {}) json["improvements"] = improvements;
            if (tags) json["tags"] = tags;
            event.addJson(`tetra:materials/${category}/${key}.json`, json);
            addTetraMaterialToStageTag(event, stage, materialItems, material["tag"]);
            if (activeTetraMaterialStage === null && stage !== undefined && stage !== null) {
                writeTetraMaterialStageTag(stage);
            }
            console.log(`[Tetra Wheel Chair] Material "${key}" Build-ed. `);
        }
    };
    return builder;
}