import { world, system } from "@minecraft/server";
import { fo } from './utilities/fo_constants.js';

// Create the default values for the DynamicProperties

/* 
The addon settings can be directly adjusted from here if the addon has not been applied yet to the world
If the addon was already applied to the world, please use the ingame configuration Menu

Replace with the values detailed in the comments
*/

export function initDynamicProperties() {

    // Controls the total duration of the Addon in hours
    if (world.getDynamicProperty("totalDuration") === undefined) {
        world.setDynamicProperty("totalDuration", 20); //value in hours (can be 10, 15, 20, 25, 30)
    }

    // Controls the base probabilty of events happening 1 = base, 3 = three times the chance
    if (world.getDynamicProperty("probabilityModifier") === undefined) {
        world.setDynamicProperty("probabilityModifier", 1); // Default to 1 (x1) can be (0.5, 1, 1.5, 2, 2.5, 3)
    }

    // Controls if the Foreboding ends after a given time or not, if selected false the Foreboding will not stop at chapter 5
    if (world.getDynamicProperty("endlessMode") === undefined) {
        world.setDynamicProperty("endlessMode", false); //can be false or true
    }

    // Controls if the credits rolls (true) or not (false) at the end of chapter 5
    // (Here is where I plea for a tip or a comment - I spent weeks working on this project I deserve this much)
    // But understandable it can break the inmersion so here is the toggle
    if (world.getDynamicProperty("creditsToggle") === undefined) {
        world.setDynamicProperty("creditsToggle", true); //can be false or true
    }

    // Controls if the addon inner timer progress or not, by setting it to true: whatever chapter you are in, wont change.
    if (world.getDynamicProperty("maintainChapter") === undefined) {
        world.setDynamicProperty("maintainChapter", false); //can be false or true
    }

    // Controls if the Still Life Event is enabled or not, if set to true: at some point the randomtickspeed will be stuck at 0
    if (world.getDynamicProperty("randomTick") === undefined) {
        world.setDynamicProperty("randomTick", true); //can be false or true
    }

    // Controls if Minor Structures Event is enabled or not, this allows for minor structures to be build sparcely around your world
    if (world.getDynamicProperty("minorStructures") === undefined) {
        world.setDynamicProperty("minorStructures", true); //can be false or true
    }

    // Controls the perforfmance mode, we scale down heavy duty scripts based on this
    if (world.getDynamicProperty("performanceMode") === undefined) {
        const memoryTier = system.serverSystemInfo.memoryTier;
        let performanceModifier = 1;
        if (memoryTier >= 3) performanceModifier = 1;
        if (memoryTier === 2) performanceModifier = 1.5;
        if (memoryTier === 1) performanceModifier = 2.5;
        if (memoryTier === 0) performanceModifier = 4;
        world.setDynamicProperty("performanceMode", performanceModifier); //can be false or true
    }


    //______________END OF ADDON CONFIGURATIONS__________________
    // Do not edit further below

    // stores the last time played, helps trigger the no pause event, in the no pause script
    if (world.getDynamicProperty("lastRealTime") === undefined) {
        world.setDynamicProperty("lastRealTime", Date.now()); // stores a date value
    }

    // Controls if the addon inner timer progress or not, this is a temporal toggle comming from the afk event
    if (world.getDynamicProperty("afkPause") === undefined) {
        world.setDynamicProperty("afkPause", false); //can be false or true
    }

    // Controls if the player is in the Forebearer dead list
    if (world.getDynamicProperty("forebearerDead") === undefined) {
        world.setDynamicProperty("forebearerDead", "[]");
    }

    //Safeguard in case the game is closed with a temporal tickSpeed, comes from the fo_NoPause event and is used in the gamerules script
    if (world.getDynamicProperty("currentTickSpeed") === undefined) {
        world.setDynamicProperty("currentTickSpeed", false);
    }

    if (world.getDynamicProperty("multiplayerProbabilityModifier") === undefined) {
        world.setDynamicProperty("multiplayerProbabilityModifier", 1); //This is a variant we call in the other scripts
    }

    if (world.getDynamicProperty("netProbabilityModifier") === undefined) {
        world.setDynamicProperty("netProbabilityModifier", 1); //This is the one we call in the other scripts
    }

    //We recalculate the probability modifier adding the multiplayer modifier
    function updateVariables() {

        //Multiplayer Modifier calculation
        const probabilityModifier = world.getDynamicProperty("probabilityModifier")

        const serverPopulation = fo.overworld.getPlayers();
        //We are using the probability modifier as base to the multiplayer mod calculation
        const multiplayerModifier = Math.max(1 + (serverPopulation.length - 1) * 0.3 * (25 / (serverPopulation.length + 23)), 1);
        world.setDynamicProperty("multiplayerProbabilityModifier", multiplayerModifier);
        /*Multiplayer modifier per players in the overworld
        1	1.0000 
        2	1.3000 
        3	1.5769 
        4	1.8333 
        5	2.0714 
        10	3.0455 
        15	3.7632 
        20	4.3140 
        25	4.7500 
        30	5.1038 
        40	5.6429 
        50	6.0342 
        100	7.0366 
        **/

        const netProbabilityModifier = probabilityModifier + multiplayerModifier - 1 //we substract 1 to eliminate the base 1 from the mult Modif
        world.setDynamicProperty("netProbabilityModifier", netProbabilityModifier);
    }

    system.runInterval(updateVariables, 120);

};
