// Script to replace vanilla horses and donkeys with cyn_horses and cyn_donkeys
// Author: InnerTri

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

import { disableableHorseStrings } from './chapters.js'



function replaceHorse(entity, replacementId) {
    // get the world dyn prop
    let dynPropReplaceHorses = world.getDynamicProperty("cyn_hao:replaceHorses");
		
    // if it is undefined, define it
    if (dynPropReplaceHorses == undefined) {
        world.setDynamicProperty("cyn_hao:replaceHorses", false);
        dynPropReplaceHorses = false;
    }

    // check if we should replace it
    if (dynPropReplaceHorses == true) {

        // save it's location
        let horseDim = entity.dimension;
        let horseLoc = entity.location;
        
        // despawn the entity
        entity.remove();

        // spawn the specified entity instead
        let newHorse = horseDim.spawnEntity(replacementId, horseLoc);

        return newHorse;
    }
}

function checkHorseDisabled(spawnedHorse, horseStringIndex) {

    let dynPropString = "cyn_hao:disable" + disableableHorseStrings[horseStringIndex] + "";

    let dynProp = world.getDynamicProperty(dynPropString);

    if (dynProp == undefined) {
        console.info("World has no " + dynPropString + " dynamic property. Initializing as false.");
		world.setDynamicProperty(dynPropString, false);
        dynProp = false;
	}

    if (dynProp == true) {
        spawnedHorse.remove()
        console.info("Despawned a " + spawnedHorse.typeId + " as it is disabled in this world.")
    }
}

// when ANY entity EVER spawns
world.afterEvents.entitySpawn.subscribe(
    event => {
        // save data for reference
        let entity = event.entity;
        let cause = event.cause;

        // if the entity spawned naturally
        if (cause == EntityInitializationCause.Spawned) {
            // replace the entity as appropriate
            // luckily, the spawning of the replacement in these functions triggers the spawn event,
            // which then checks if that horse is disabled as below. Convenient!
            switch (entity.typeId) {
                case "minecraft:horse":
                    replaceHorse(entity, "cyn_hao:horse");
                break;

                case "minecraft:donkey":
                    replaceHorse(entity, "cyn_hao:donkey");
                break;

                case "minecraft:zombie_horse":
                    replaceHorse(entity, "cyn_hao:zombie_horse");
                break;
                
                case "minecraft:skeleton_horse":
                    replaceHorse(entity, "cyn_hao:skeleton_horse");
                break;

                // mules can only be bred, so they cannot actually be disabled
                // however, if either horses or donkeys are disabled,
                // mules effectively are as well
                case "minecraft:mule":
                    replaceHorse(entity, "cyn_hao:mule");
                break;

                // Aside from horse replacement
                case "cyn_hao:horse": 
                    checkHorseDisabled(entity, 0)
                break;

                case "cyn_hao:donkey": 
                    checkHorseDisabled(entity, 1)
                break;

                case "cyn_hao:zebra": 
                    checkHorseDisabled(entity, 2)
                break;
                
                case "cyn_hao:seahorse_big": 
                    checkHorseDisabled(entity, 3)
                break;
                
                case "cyn_hao:seahorse_small": 
                    checkHorseDisabled(entity, 4)
                break;
                
                case "cyn_hao:unicorn": 
                    checkHorseDisabled(entity, 5)
                break;
                
                case "cyn_hao:rainbow_horse": 
                    checkHorseDisabled(entity, 6)
                break;
                
                case "cyn_hao:kelpie_horse": 
                    checkHorseDisabled(entity, 7)
                break;
                
                case "cyn_hao:spider_horse": 
                    checkHorseDisabled(entity, 8)
                break;
                
                case "cyn_hao:zombie_horse": 
                    checkHorseDisabled(entity, 9)
                break;
                
                case "cyn_hao:skeleton_horse": 
                    checkHorseDisabled(entity, 10)
                break;
                
                case "cyn_hao:creeper_horse": 
                    checkHorseDisabled(entity, 11)
                break;
                
                case "cyn_hao:ender_horse": 
                    checkHorseDisabled(entity, 12)
                break;
                
                case "cyn_hao:dragon_horse": 
                    checkHorseDisabled(entity, 13)
                break;
                
                case "cyn_hao:crimson_horse": 
                    checkHorseDisabled(entity, 14)
                break;
                
                case "cyn_hao:warped_horse": 
                    checkHorseDisabled(entity, 15)
                break;
                
                case "cyn_hao:ghost_horse": 
                    checkHorseDisabled(entity, 16)
                break;
                
                case "cyn_hao:lava_horse": 
                    checkHorseDisabled(entity, 17)
                break;
                
                case "cyn_hao:blaze_horse": 
                    checkHorseDisabled(entity, 18)
                break;
                
            }
        }
    }
);