import * as mc from "@minecraft/server"

const SOUND_FAIL = "cyd_mp.cutify_fail";
const SOUND_OPTIONS = { pitch: 1.0, volume: 1.0, };

const entityMap = {
    "minecraft:allay": "cyd_mp:cute_allay",
    "minecraft:armadillo": "cyd_mp:cute_armadillo",
    "minecraft:axolotl": "cyd_mp:cute_axolotl",
    "minecraft:bat": "cyd_mp:cute_bat",
    "minecraft:bee": "cyd_mp:cute_bee",
    "minecraft:blaze": "cyd_mp:cute_blaze",
    "minecraft:bogged": "cyd_mp:cute_bogged",
    "minecraft:breeze": "cyd_mp:cute_breeze",
    "minecraft:camel": "cyd_mp:cute_camel",
    "minecraft:cat": "cyd_mp:cute_cat",
    "minecraft:cave_spider": "cyd_mp:cute_cave_spider",
    "minecraft:chicken": "cyd_mp:cute_chicken",
    "minecraft:cow": "cyd_mp:cute_cow",
    "minecraft:creaking": "cyd_mp:cute_creaking",
    "minecraft:creeper": "cyd_mp:cute_creeper",
    "minecraft:dolphin": "cyd_mp:cute_dolphin",
    "minecraft:donkey": "cyd_mp:cute_donkey",
    "minecraft:drowned": "cyd_mp:cute_drowned",
    "minecraft:elder_guardian": "cyd_mp:cute_elder_guardian",
    "minecraft:ender_dragon": "cyd_mp:cute_ender_dragon",
    "minecraft:enderman": "cyd_mp:cute_enderman",
    "minecraft:endermite": "cyd_mp:cute_endermite",
    "minecraft:evocation_illager": "cyd_mp:cute_evoker",
    "minecraft:fox": "cyd_mp:cute_fox",
    "minecraft:frog": "cyd_mp:cute_frog",
    "minecraft:ghast": "cyd_mp:cute_ghast",
    "minecraft:glow_squid": "cyd_mp:cute_glow_squid",
    "minecraft:goat": "cyd_mp:cute_goat",
    "minecraft:guardian": "cyd_mp:cute_guardian",
    "minecraft:hoglin": "cyd_mp:cute_hoglin",
    "minecraft:horse": "cyd_mp:cute_horse",
    "minecraft:skeleton_horse": "cyd_mp:cute_skeleton_horse",
    "minecraft:zombie_horse": "cyd_mp:cute_zombie_horse",
    "minecraft:husk": "cyd_mp:cute_husk",
    "minecraft:iron_golem": "cyd_mp:cute_iron_golem",
    "minecraft:llama": "cyd_mp:cute_llama",
    "minecraft:trader_llama": "cyd_mp:cute_llama",
    "minecraft:magma_cube": "cyd_mp:cute_magma_cube",
    "minecraft:mooshroom": "cyd_mp:cute_mooshroom",
    "minecraft:mule": "cyd_mp:cute_mule",
    "minecraft:ocelot": "cyd_mp:cute_ocelot",
    "minecraft:panda": "cyd_mp:cute_panda",
    "minecraft:parrot": "cyd_mp:cute_parrot",
    "minecraft:phantom": "cyd_mp:cute_phantom",
    "minecraft:pig": "cyd_mp:cute_pig",
    "minecraft:piglin": "cyd_mp:cute_piglin",
    "minecraft:piglin_brute": "cyd_mp:cute_piglin_brute",
    "minecraft:pillager": "cyd_mp:cute_pillager",
    "minecraft:polar_bear": "cyd_mp:cute_polar_bear",
    "minecraft:rabbit": "cyd_mp:cute_rabbit",
    "minecraft:ravager": "cyd_mp:cute_ravager",
    "minecraft:sheep": "cyd_mp:cute_sheep",
    "minecraft:shulker": "cyd_mp:cute_shulker",
    "minecraft:silverfish": "cyd_mp:cute_silverfish",
    "minecraft:skeleton": "cyd_mp:cute_skeleton",
    "minecraft:slime": "cyd_mp:cute_slime",
    "minecraft:sniffer": "cyd_mp:cute_sniffer",
    "minecraft:snow_golem": "cyd_mp:cute_snow_golem",
    "minecraft:spider": "cyd_mp:cute_spider",
    "minecraft:squid": "cyd_mp:cute_squid",
    "minecraft:stray": "cyd_mp:cute_stray",
    "minecraft:strider": "cyd_mp:cute_strider",
    "minecraft:tadpole": "cyd_mp:cute_tadpole",
    "minecraft:turtle": "cyd_mp:cute_turtle",
    "minecraft:vex": "cyd_mp:cute_vex",
    "minecraft:vindicator": "cyd_mp:cute_vindicator",
    "minecraft:warden": "cyd_mp:cute_warden",
    "minecraft:witch": "cyd_mp:cute_witch",
    "minecraft:wither_skeleton": "cyd_mp:cute_wither_skeleton",
    "minecraft:wither": "cyd_mp:cute_wither",
    "minecraft:wolf": "cyd_mp:cute_wolf",
    "minecraft:zoglin": "cyd_mp:cute_zoglin",
    "minecraft:zombie_piglin": "cyd_mp:cute_zombie_piglin",
    "minecraft:zombie": "cyd_mp:cute_zombie"
};

export function cutifyMob(player) {
    let firstMob = getFirstMobInView(player, 10);
    if (firstMob) {
        if (entityMap[firstMob.typeId]) {
            checkSkin(firstMob, player);

            const se = player.dimension.spawnEntity(entityMap[firstMob.typeId], {
                x: firstMob.location.x,
                y: firstMob.location.y,
                z: firstMob.location.z,
            });

            const particleLoc = {
                x:firstMob.location.x,
                y:firstMob.location.y + 0.25,
                z:firstMob.location.z
            }

            if (firstMob.hasComponent(mc.EntityComponentTypes.IsBaby)) {
                se.triggerEvent("evt:baby");
            }
            if (firstMob.hasTag('var0r')) {
                se.triggerEvent("evt:cute_rabbit_brown");
            }
            if (firstMob.hasTag('var1r')) {
                se.triggerEvent("evt:cute_rabbit_white");
            }
            if (firstMob.hasTag('var2r')) {
                se.triggerEvent("evt:cute_rabbit_black");
            }
            if (firstMob.hasTag('var3r')) {
                se.triggerEvent("evt:cute_rabbit_splotched");
            }
            if (firstMob.hasTag('var4r')) {
                se.triggerEvent("evt:cute_rabbit_toast");
            }
            if (firstMob.hasTag('var5r')) {
                se.triggerEvent("evt:cute_rabbit_salt");
            }

            if (firstMob.hasTag('var0a')) {
                se.triggerEvent("evt:cute_axolotl_blue");
            }
            if (firstMob.hasTag('var1a')) {
                se.triggerEvent("evt:cute_axolotl_cyan");
            }
            if (firstMob.hasTag('var2a')) {
                se.triggerEvent("evt:cute_axolotl_gold");
            }
            if (firstMob.hasTag('var3a')) {
                se.triggerEvent("evt:cute_axolotl_wild");
            }
            if (firstMob.hasTag('var4a')) {
                se.triggerEvent("evt:cute_axolotl_pink");
            }


            if (firstMob.hasTag('var0c')) {
                se.triggerEvent("evt:cute_cat_white");
            }
            if (firstMob.hasTag('var1c')) {
                se.triggerEvent("evt:cute_cat_fullblack");
            }
            if (firstMob.hasTag('var2c')) {
                se.triggerEvent("evt:cute_cat_red");
            }
            if (firstMob.hasTag('var3c')) {
                se.triggerEvent("evt:cute_cat_siamese");
            }
            if (firstMob.hasTag('var4c')) {
                se.triggerEvent("evt:cute_cat_gray");
            }
            if (firstMob.hasTag('var5c')) {
                se.triggerEvent("evt:cute_cat_calico");
            }
            if (firstMob.hasTag('var6c')) {
                se.triggerEvent("evt:cute_cat_persian");
            }
            if (firstMob.hasTag('var7c')) {
                se.triggerEvent("evt:cute_cat_ragdoll");
            }
            if (firstMob.hasTag('var8c')) {
                se.triggerEvent("evt:cute_cat_tabby");
            }
            if (firstMob.hasTag('var9c')) {
                se.triggerEvent("evt:cute_cat_black");
            }
            if (firstMob.hasTag('var10c')) {
                se.triggerEvent("evt:cute_cat_jellie");
            }

            if (firstMob.hasTag('var0w')) {
                se.triggerEvent("evt:cute_wolf");
            }
            if (firstMob.hasTag('var1w')) {
                se.triggerEvent("evt:cute_wolf_ashen");
            }
            if (firstMob.hasTag('var2w')) {
                se.triggerEvent("evt:cute_wolf_black");
            }
            if (firstMob.hasTag('var3w')) {
                se.triggerEvent("evt:cute_wolf_chestnut");
            }
            if (firstMob.hasTag('var4w')) {
                se.triggerEvent("evt:cute_wolf_rusty");
            }
            if (firstMob.hasTag('var5w')) {
                se.triggerEvent("evt:cute_wolf_snowy");
            }
            if (firstMob.hasTag('var6w')) {
                se.triggerEvent("evt:cute_wolf_spotted");
            }
            if (firstMob.hasTag('var7w')) {
                se.triggerEvent("evt:cute_wolf_striped");
            }
            if (firstMob.hasTag('var8w')) {
                se.triggerEvent("evt:cute_wolf_woods");
            }

            if (firstMob.hasTag('var0s')) {
                se.triggerEvent("evt:cute_shulker_black");
            }
            if (firstMob.hasTag('var1s')) {
                se.triggerEvent("evt:cute_shulker_red");
            }
            if (firstMob.hasTag('var2s')) {
                se.triggerEvent("evt:cute_shulker_green");
            }
            if (firstMob.hasTag('var3s')) {
                se.triggerEvent("evt:cute_shulker_brown");
            }
            if (firstMob.hasTag('var4s')) {
                se.triggerEvent("evt:cute_shulker_blue");
            }
            if (firstMob.hasTag('var5s')) {
                se.triggerEvent("evt:cute_shulker_purple");
            }
            if (firstMob.hasTag('var6s')) {
                se.triggerEvent("evt:cute_shulker_cyan");
            }
            if (firstMob.hasTag('var7s')) {
                se.triggerEvent("evt:cute_shulker_silver");
            }
            if (firstMob.hasTag('var8s')) {
                se.triggerEvent("evt:cute_shulker_gray");
            }
            if (firstMob.hasTag('var9s')) {
                se.triggerEvent("evt:cute_shulker_pink");
            }
            if (firstMob.hasTag('var10s')) {
                se.triggerEvent("evt:cute_shulker_lime");
            }
            if (firstMob.hasTag('var11s')) {
                se.triggerEvent("evt:cute_shulker_yellow");
            }
            if (firstMob.hasTag('var12s')) {
                se.triggerEvent("evt:cute_shulker_light_blue");
            }
            if (firstMob.hasTag('var13s')) {
                se.triggerEvent("evt:cute_shulker_magenta");
            }
            if (firstMob.hasTag('var14s')) {
                se.triggerEvent("evt:cute_shulker_orange");
            }
            if (firstMob.hasTag('var15s')) {
                se.triggerEvent("evt:cute_shulker_white");
            }


            if (firstMob.hasTag('var0h')) {
                se.triggerEvent("evt:cute_horse");
            }
            if (firstMob.hasTag('var1h')) {
                se.triggerEvent("evt:cute_horse_creamy");
            }
            if (firstMob.hasTag('var2h')) {
                se.triggerEvent("evt:cute_horse_chestnut");
            }
            if (firstMob.hasTag('var3h')) {
                se.triggerEvent("evt:cute_horse_brown");
            }
            if (firstMob.hasTag('var4h')) {
                se.triggerEvent("evt:cute_horse_black");
            }
            if (firstMob.hasTag('var5h')) {
                se.triggerEvent("evt:cute_horse_gray");
            }
            if (firstMob.hasTag('var6h')) {
                se.triggerEvent("evt:cute_horse_darkbrown");
            }
            if (firstMob.hasTag('var7h')) {
                se.triggerEvent("evt:cute_wolf_striped");
            }
            if (firstMob.hasTag('var8h')) {
                se.triggerEvent("evt:cute_horse_white");
            }




            if (firstMob.hasTag('var0p')) {
                se.triggerEvent("evt:cute_parrot_red");
            }
            if (firstMob.hasTag('var1p')) {
                se.triggerEvent("evt:cute_parrot_blue");
            }
            if (firstMob.hasTag('var2p')) {
                se.triggerEvent("evt:cute_parrot_green");
            }
            if (firstMob.hasTag('var3p')) {
                se.triggerEvent("evt:cute_parrot_cyan");
            }
            if (firstMob.hasTag('var4p')) {
                se.triggerEvent("evt:cute_parrot_silver");
            }



            if (firstMob.hasTag('var0fr')) {
                se.triggerEvent("evt:cute_frog_temperate");
            }
            if (firstMob.hasTag('var1fr')) {
                se.triggerEvent("evt:cute_frog_cold");
            }
            if (firstMob.hasTag('var2fr')) {
                se.triggerEvent("evt:cute_frog_warm");
            }



            if (firstMob.hasTag('var0la')) {
                se.triggerEvent("evt:cute_llama_creamy");
            }
            if (firstMob.hasTag('var1la')) {
                se.triggerEvent("evt:cute_llama_white");
            }
            if (firstMob.hasTag('var2la')) {
                se.triggerEvent("evt:cute_llama_brown");
            }
            if (firstMob.hasTag('var3la')) {
                se.triggerEvent("evt:cute_llama_gray");
            }



            if (firstMob.hasTag('white')) {
                se.triggerEvent("evt:sheep_white");
            }
            if (firstMob.hasTag('orange')) {
                se.triggerEvent("evt:sheep_orange");
            }
            if (firstMob.hasTag('magenta')) {
                se.triggerEvent("evt:sheep_magenta");
            }
            if (firstMob.hasTag('light_blue')) {
                se.triggerEvent("evt:sheep_light_blue");
            }
            if (firstMob.hasTag('yellow')) {
                se.triggerEvent("evt:sheep_yellow");
            }
            if (firstMob.hasTag('lime')) {
                se.triggerEvent("evt:sheep_lime");
            }
            if (firstMob.hasTag('pink')) {
                se.triggerEvent("evt:sheep_pink");
            }
            if (firstMob.hasTag('light_grey')) {
                se.triggerEvent("evt:sheep_light_grey");
            }
            if (firstMob.hasTag('gray')) {
                se.triggerEvent("evt:sheep_gray");
            }
            if (firstMob.hasTag('cyan')) {
                se.triggerEvent("evt:sheep_cyan");
            }
            if (firstMob.hasTag('purple')) {
                se.triggerEvent("evt:sheep_purple");
            }
            if (firstMob.hasTag('blue')) {
                se.triggerEvent("evt:sheep_blue");
            }
            if (firstMob.hasTag('brown')) {
                se.triggerEvent("evt:sheep_brown");
            }
            if (firstMob.hasTag('green')) {
                se.triggerEvent("evt:sheep_green");
            }
            if (firstMob.hasTag('red')) {
                se.triggerEvent("evt:sheep_red");
            }
            if (firstMob.hasTag('black')) {
                se.triggerEvent("evt:sheep_black");
            }

            se.addEffect('slow_falling', 3*20, { amplifier: 1, showParticles: false });
            player.dimension.spawnParticle("cyd_mp:smoke_poof", particleLoc);
            player.dimension.spawnParticle("cyd_mp:heart_swirl", particleLoc);
            firstMob.remove();
        } else {
            player.playSound(SOUND_FAIL, SOUND_OPTIONS);
        }
    }
}

export function getFirstMobInView(player, maxDistance) {
    const entitiesInView = player.getEntitiesFromViewDirection({ maxDistance: maxDistance });
    if (entitiesInView.length === 0) {
        return null;
    }
    for (const entityHit of entitiesInView) {
        const entity = entityHit.entity;
        if (entity && entity.typeId.startsWith("minecraft:") && entity.typeId !== "minecraft:player") {
            return entity;
        }
    }
    return null;
}
export function getFirstCuteMobInView(player, maxDistance) {
    const entitiesInView = player.getEntitiesFromViewDirection({ maxDistance: maxDistance });
    if (entitiesInView.length === 0) {
        return null;
    }
    for (const entityHit of entitiesInView) {
        const entity = entityHit.entity;
        if (entity && entity.typeId.startsWith("cyd_mp:") && entity.typeId !== "minecraft:player") {
            return entity;
        }
    }
    return null;
}

function checkSkin(entity, player) {
    const variantType = entity.getComponent(mc.EntityComponentTypes.Variant);
    const colorType = entity.getComponent(mc.EntityComponentTypes.Color);

    if (entity.typeId == "minecraft:sheep") {

        if (colorType.value == 0) {
            entity.addTag('white');
        }
        if (colorType.value == 1) {
            entity.addTag('orange');
        }
        if (colorType.value == 2) {
            entity.addTag('magenta');
        }
        if (colorType.value == 3) {
            entity.addTag('light_blue');
        }
        if (colorType.value == 4) {
            entity.addTag('yellow');
        }
        if (colorType.value == 5) {
            entity.addTag('lime');
        }
        if (colorType.value == 6) {
            entity.addTag('pink');
        }
        if (colorType.value == 7) {
            entity.addTag('light_grey');
        }
        if (colorType.value == 8) {
            entity.addTag('gray');
        }
        if (colorType.value == 9) {
            entity.addTag('cyan');
        }
        if (colorType.value == 10) {
            entity.addTag('purple');
        }
        if (colorType.value == 11) {
            entity.addTag('blue');
        }
        if (colorType.value == 12) {
            entity.addTag('brown');
        }
        if (colorType.value == 13) {
            entity.addTag('green');
        }
        if (colorType.value == 14) {
            entity.addTag('red');
        }
        if (colorType.value == 15) {
            entity.addTag('black');
        }
    }

    if (variantType == null) {
        return;
    }

    if (entity.typeId == "minecraft:rabbit") {
        if (variantType.value == 0) {
            entity.addTag('var0r')
        }
        if (variantType.value == 1) {
            entity.addTag('var1r')
        }
        if (variantType.value == 2) {
            entity.addTag('var2r')
        }
        if (variantType.value == 3) {
            entity.addTag('var3r')
        }
        if (variantType.value == 4) {
            entity.addTag('var4r')
        }
        if (variantType.value == 5) {
            entity.addTag('var5r')
        }
        if (variantType.value == 6) {
            entity.addTag('var6r')
        }
    }
    if (entity.typeId == "minecraft:axolotl") {
        if (variantType.value == 4) {
            entity.addTag('var0a')
        }
        if (variantType.value == 1) {
            entity.addTag('var1a')
        }
        if (variantType.value == 2) {
            entity.addTag('var2a')
        }
        if (variantType.value == 3) {
            entity.addTag('var3a')
        }
        if (variantType.value == 0) {
            entity.addTag('var4a')
        }
    }

    if (entity.typeId == "minecraft:cat") {
        if (variantType.value == 0) {
            entity.addTag('var0c');
        }
        if (variantType.value == 1) {
            entity.addTag('var1c');
        }
        if (variantType.value == 2) {
            entity.addTag('var2c');
        }
        if (variantType.value == 3) {
            entity.addTag('var3c');
        }
        if (variantType.value == 4) {
            entity.addTag('var4c');
        }
        if (variantType.value == 5) {
            entity.addTag('var5c');
        }
        if (variantType.value == 6) {
            entity.addTag('var6c');
        }
        if (variantType.value == 7) {
            entity.addTag('var7c');
        }
        if (variantType.value == 8) {
            entity.addTag('var8c');
        }
        if (variantType.value == 9) {
            entity.addTag('var9c');
        }
        if (variantType.value == 10) {
            entity.addTag('var10c');
        }
        if (variantType.value == 11) {
            entity.addTag('var11c');
        }
    }
    if (entity.typeId == "minecraft:wolf") {
        if (variantType.value == 0) {
            entity.addTag('var0w');
        }
        if (variantType.value == 1) {
            entity.addTag('var1w');
        }
        if (variantType.value == 2) {
            entity.addTag('var2w');
        }
        if (variantType.value == 3) {
            entity.addTag('var3w');
        }
        if (variantType.value == 4) {
            entity.addTag('var4w');
        }
        if (variantType.value == 5) {
            entity.addTag('var5w');
        }
        if (variantType.value == 6) {
            entity.addTag('var6w');
        }
        if (variantType.value == 7) {
            entity.addTag('var7w');
        }
        if (variantType.value == 8) {
            entity.addTag('var8w');
        }
    }

    if (entity.typeId == "minecraft:parrot") {
        if (variantType.value == 0) {
            entity.addTag('var0p');
        }
        if (variantType.value == 1) {
            entity.addTag('var1p');
        }
        if (variantType.value == 2) {
            entity.addTag('var2p');
        }
        if (variantType.value == 3) {
            entity.addTag('var3p');
        }
        if (variantType.value == 4) {
            entity.addTag('var4p');
        }
        if (variantType.value == 5) {
            entity.addTag('var5p');
        }
        if (variantType.value == 6) {
            entity.addTag('var6p');
        }
    }

    if (entity.typeId == "minecraft:frog") {
        if (variantType.value == 0) {
            entity.addTag('var0fr');
        }
        if (variantType.value == 1) {
            entity.addTag('var1fr');
        }
        if (variantType.value == 2) {
            entity.addTag('var2fr');
        }
    }


    if (entity.typeId == "minecraft:shulker") {
        if (variantType.value == 0) {
            entity.addTag('var0s');
        }
        if (variantType.value == 1) {
            entity.addTag('var1s');
        }
        if (variantType.value == 2) {
            entity.addTag('var2s');
        }
        if (variantType.value == 3) {
            entity.addTag('var3s');
        }
        if (variantType.value == 4) {
            entity.addTag('var4s');
        }
        if (variantType.value == 5) {
            entity.addTag('var5s');
        }
        if (variantType.value == 6) {
            entity.addTag('var6s');
        }
        if (variantType.value == 7) {
            entity.addTag('var7s');
        }
        if (variantType.value == 8) {
            entity.addTag('var8s');
        }
        if (variantType.value == 9) {
            entity.addTag('var9s');
        }
        if (variantType.value == 10) {
            entity.addTag('var10s');
        }
        if (variantType.value == 11) {
            entity.addTag('var11s');
        }
        if (variantType.value == 12) {
            entity.addTag('var12s');
        }
        if (variantType.value == 13) {
            entity.addTag('var13s');
        }
        if (variantType.value == 14) {
            entity.addTag('var14s');
        }
        if (variantType.value == 15) {
            entity.addTag('var15s');
        }
    }

    if (entity.typeId == "minecraft:horse") {
        if (variantType.value == 0) {
            entity.addTag('var0h');
        }
        if (variantType.value == 1) {
            entity.addTag('var1h');
        }
        if (variantType.value == 2) {
            entity.addTag('var2h');
        }
        if (variantType.value == 3) {
            entity.addTag('var3h');
        }
        if (variantType.value == 4) {
            entity.addTag('var4h');
        }
        if (variantType.value == 5) {
            entity.addTag('var5h');
        }
        if (variantType.value == 6) {
            entity.addTag('var6h');
        }
        if (variantType.value == 7) {
            entity.addTag('var7h');
        }
        if (variantType.value == 8) {
            entity.addTag('var8h');
        }
    }

    if (entity.typeId == "minecraft:llama") {
        if (variantType.value == 0) {
            entity.addTag('var0la');
        }
        if (variantType.value == 1) {
            entity.addTag('var1la');
        }
        if (variantType.value == 2) {
            entity.addTag('var2la');
        }
        if (variantType.value == 3) {
            entity.addTag('var3la');
        }
    }

    


}