import { world, system } from "@minecraft/server";

export let fo = {
    
    scoreChapter2: 1,
    scoreChapter3: 216000,
    scoreChapter4: 792000,
    scoreChapter5: 1152000,
    scoreChapter6: 1440000,
    durationChapter2: 216000 - 1,
    durationChapter3: 792000 - 216000,
    durationChapter4: 1152000 - 792000,
    durationChapter5: 1440000 - 1152000,
    scoreChapter7: -2,
    scoreChapter7s: -1202,

    
    overworld: null,
    nether: null,
    theEnd: null,
    forebodingObjective: null,

    furnacesIds: [
        "minecraft:furnace",
        "minecraft:lit_furnace",
        "minecraft:blast_furnace",
        "minecraft:lit_blast_furnace",
        "minecraft:smoker",
        "minecraft:lit_smoker",
        "minecraft:brewing_stand",
    ],

    doorIds: [
        "minecraft:wooden_door",
        "minecraft:spruce_door",
        "minecraft:birch_door",
        "minecraft:jungle_door",
        "minecraft:acacia_door",
        "minecraft:dark_oak_door",
        "minecraft:mangrove_door",
        "minecraft:cherry_door",
        "minecraft:bamboo_door",
        "minecraft:crimson_door",
        "minecraft:warped_door",
        "minecraft:pale_oak_door",
        
        "minecraft:copper_door",
        "minecraft:exposed_copper_door",
        "minecraft:weathered_copper_door",
        "minecraft:oxidized_copper_door",
        "minecraft:waxed_copper_door",
        "minecraft:waxed_exposed_copper_door",
        "minecraft:waxed_weathered_copper_door",
        "minecraft:waxed_oxidized_copper_door"
    ],

    copperDoorIds: [
        "minecraft:copper_door",
        "minecraft:exposed_copper_door",
        "minecraft:weathered_copper_door",
        "minecraft:oxidized_copper_door",
        "minecraft:waxed_copper_door",
        "minecraft:waxed_exposed_copper_door",
        "minecraft:waxed_weathered_copper_door",
        "minecraft:waxed_oxidized_copper_door"
    ],

    
    glassBlocks: [
        "minecraft:white_stained_glass",
        "minecraft:orange_stained_glass",
        "minecraft:magenta_stained_glass",
        "minecraft:light_blue_stained_glass",
        "minecraft:yellow_stained_glass",
        "minecraft:lime_stained_glass",
        "minecraft:pink_stained_glass",
        "minecraft:gray_stained_glass",
        "minecraft:light_gray_stained_glass",
        "minecraft:cyan_stained_glass",
        "minecraft:purple_stained_glass",
        "minecraft:blue_stained_glass",
        "minecraft:brown_stained_glass",
        "minecraft:green_stained_glass",
        "minecraft:red_stained_glass",
        "minecraft:black_stained_glass",
        "minecraft:white_stained_glass_pane",
        "minecraft:orange_stained_glass_pane",
        "minecraft:magenta_stained_glass_pane",
        "minecraft:light_blue_stained_glass_pane",
        "minecraft:yellow_stained_glass_pane",
        "minecraft:lime_stained_glass_pane",
        "minecraft:pink_stained_glass_pane",
        "minecraft:gray_stained_glass_pane",
        "minecraft:light_gray_stained_glass_pane",
        "minecraft:cyan_stained_glass_pane",
        "minecraft:purple_stained_glass_pane",
        "minecraft:blue_stained_glass_pane",
        "minecraft:brown_stained_glass_pane",
        "minecraft:green_stained_glass_pane",
        "minecraft:red_stained_glass_pane",
        "minecraft:black_stained_glass_pane",
        "minecraft:glass",
        "minecraft:glass_pane"
    ],

    lightSourceBlocks: [
        "minecraft:torch",
        "minecraft:copper_torch",
        "minecraft:soul_torch",
        "minecraft:lantern",
        "minecraft:copper_lantern",
        "minecraft:exposed_copper_lantern",
        "minecraft:weathered_copper_lantern",
        "minecraft:oxidized_copper_lantern",
        "minecraft:waxed_copper_lantern",
        "minecraft:waxed_exposed_copper_lantern",
        "minecraft:waxed_weathered_copper_lantern",
        "minecraft:waxed_oxidized_copper_lantern",
        "minecraft:glowstone",
        "minecraft:lit_redstone_lamp",
    ],

    validMinedBlocks: [
        "minecraft:andesite",
        "minecraft:coal_ore",
        "minecraft:copper_ore",
        "minecraft:deepslate",
        "minecraft:deepslate_coal_ore",
        "minecraft:deepslate_copper_ore",
        "minecraft:deepslate_diamond_ore",
        "minecraft:deepslate_emerald_ore",
        "minecraft:deepslate_gold_ore",
        "minecraft:deepslate_iron_ore",
        "minecraft:deepslate_lapis_ore",
        "minecraft:deepslate_redstone_ore",
        "minecraft:lit_deepslate_redstone_ore",
        "minecraft:diamond_ore",
        "minecraft:diorite",
        "minecraft:emerald_ore",
        "minecraft:gold_ore",
        "minecraft:granite",
        "minecraft:iron_ore",
        "minecraft:lapis_ore",
        "minecraft:redstone_ore",
        "minecraft:lit_redstone_ore",
        "minecraft:stone",
        "minecraft:tuff",
        "minecraft:dripstone_block"
    ],

    ValidMinedBlocksArray: [
        { id: "minecraft:dirt", beingBrokenSound: "dig.gravel", brokenSound: "hit.gravel" },
        { id: "minecraft:gravel", beingBrokenSound: "hit.gravel", brokenSound: "dig.gravel" },
        { id: "minecraft:andesite", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:coal_ore", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:copper_ore", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:deepslate", beingBrokenSound: "hit.deepslate", brokenSound: "dig.deepslate" },
        { id: "minecraft:deepslate_coal_ore", beingBrokenSound: "hit.deepslate", brokenSound: "dig.deepslate" },
        { id: "minecraft:deepslate_copper_ore", beingBrokenSound: "hit.deepslate", brokenSound: "dig.deepslate" },
        { id: "minecraft:deepslate_diamond_ore", beingBrokenSound: "hit.deepslate", brokenSound: "dig.deepslate" },
        { id: "minecraft:deepslate_emerald_ore", beingBrokenSound: "hit.deepslate", brokenSound: "dig.deepslate" },
        { id: "minecraft:deepslate_gold_ore", beingBrokenSound: "hit.deepslate", brokenSound: "dig.deepslate" },
        { id: "minecraft:deepslate_iron_ore", beingBrokenSound: "hit.deepslate", brokenSound: "dig.deepslate" },
        { id: "minecraft:deepslate_lapis_ore", beingBrokenSound: "hit.deepslate", brokenSound: "dig.deepslate" },
        { id: "minecraft:deepslate_redstone_ore", beingBrokenSound: "hit.deepslate", brokenSound: "dig.deepslate" },
        { id: "minecraft:lit_deepslate_redstone_ore", beingBrokenSound: "hit.deepslate", brokenSound: "dig.deepslate" },
        { id: "minecraft:diamond_ore", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:diorite", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:emerald_ore", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:gold_ore", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:granite", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:iron_ore", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:lapis_ore", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:redstone_ore", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:lit_redstone_ore", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:stone", beingBrokenSound: "hit.stone", brokenSound: "dig.stone" },
        { id: "minecraft:tuff", beingBrokenSound: "step.tuff", brokenSound: "place.tuff" }
    ],

    
    _cache: {
        forebodingScore: { value: 0, lastTick: 0 },
        totalDuration: { value: null, lastTick: 0 },
        probabilityModifier: { value: null, lastTick: 0 },
        endlessMode: { value: null, lastTick: 0 },
        creditsToggle: { value: null, lastTick: 0 },
        maintainChapter: { value: null, lastTick: 0 },
        randomTick: { value: null, lastTick: 0 },
        minorStructures: { value: null, lastTick: 0 },
        lastRealTime: { value: null, lastTick: 0 },
        afkPause: { value: null, lastTick: 0 },
        forebearerDead: { value: null, lastTick: 0 },
        currentTickSpeed: { value: null, lastTick: 0 },
        multiplayerProbabilityModifier: { value: null, lastTick: 0 },
        netProbabilityModifier: { value: null, lastTick: 0 },
        performanceModifier: { value: null, lastTick: 0 },
    },

    
    get forebodingScore() {
        const cache = this._cache.forebodingScore;
        const now = system.currentTick;
        if (now - cache.lastTick >= 5) {
            cache.value = this.forebodingObjective?.getScore("Counter ") ?? 0;
            cache.lastTick = now;
        }
        return cache.value;
    },

    
    get totalDuration() {
        const cache = this._cache.totalDuration;
        const now = system.currentTick;
        if (now - cache.lastTick >= 600) {
            cache.value = world.getDynamicProperty("totalDuration") ?? 20;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get probabilityModifier() {
        const cache = this._cache.probabilityModifier;
        const now = system.currentTick;
        if (now - cache.lastTick >= 300) {
            cache.value = world.getDynamicProperty("probabilityModifier") ?? 1;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get endlessMode() {
        const cache = this._cache.endlessMode;
        const now = system.currentTick;
        if (now - cache.lastTick >= 600) {
            cache.value = world.getDynamicProperty("endlessMode") ?? false;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get creditsToggle() {
        const cache = this._cache.creditsToggle;
        const now = system.currentTick;
        if (now - cache.lastTick >= 600) {
            cache.value = world.getDynamicProperty("creditsToggle") ?? true;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get maintainChapter() {
        const cache = this._cache.maintainChapter;
        const now = system.currentTick;
        if (now - cache.lastTick >= 600) {
            cache.value = world.getDynamicProperty("maintainChapter") ?? false;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get randomTick() {
        const cache = this._cache.randomTick;
        const now = system.currentTick;
        if (now - cache.lastTick >= 1) {
            cache.value = world.getDynamicProperty("randomTick") ?? true;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get minorStructures() {
        const cache = this._cache.minorStructures;
        const now = system.currentTick;
        if (now - cache.lastTick >= 600) {
            cache.value = world.getDynamicProperty("minorStructures") ?? true;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get lastRealTime() {
        const cache = this._cache.lastRealTime;
        const now = system.currentTick;
        if (now - cache.lastTick >= 5) {
            cache.value = world.getDynamicProperty("lastRealTime") ?? Date.now();
            cache.lastTick = now;
        }
        return cache.value;
    },

    get afkPause() {
        const cache = this._cache.afkPause;
        const now = system.currentTick;
        if (now - cache.lastTick >= 5) {
            cache.value = world.getDynamicProperty("afkPause") ?? false;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get forebearerDead() {
        const cache = this._cache.forebearerDead;
        const now = system.currentTick;
        if (now - cache.lastTick >= 5) {
            cache.value = world.getDynamicProperty("forebearerDead") ?? "[]";
            cache.lastTick = now;
        }
        return cache.value;
    },

    get currentTickSpeed() {
        const cache = this._cache.currentTickSpeed;
        const now = system.currentTick;
        if (now - cache.lastTick >= 5) {
            cache.value = world.getDynamicProperty("currentTickSpeed") ?? false;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get multiplayerProbabilityModifier() {
        const cache = this._cache.multiplayerProbabilityModifier;
        const now = system.currentTick;
        if (now - cache.lastTick >= 300) {
            cache.value = world.getDynamicProperty("multiplayerProbabilityModifier") ?? 1;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get netProbabilityModifier() {
        const cache = this._cache.netProbabilityModifier;
        const now = system.currentTick;
        if (now - cache.lastTick >= 300) {
            cache.value = world.getDynamicProperty("netProbabilityModifier") ?? 1;
            cache.lastTick = now;
        }
        return cache.value;
    },

    get performanceModifier() {
        const cache = this._cache.performanceModifier;
        const now = system.currentTick;
        if (now - cache.lastTick >= 300) {
            cache.value = world.getDynamicProperty("performanceMode") ?? 1;
            cache.lastTick = now;
        }
        return cache.value;
    },
};

export function initUtilitiesConstants() {
    

    
    fo.overworld = world.getDimension("overworld");
    fo.nether = world.getDimension("nether");
    fo.theEnd = world.getDimension("the_end");

    fo.forebodingObjective = world.scoreboard.getObjective("Foreboding");


};

