import * as mc from "@minecraft/server";
import { getRandomInt, itemManager, randomNum, randomWholeNum } from "../sgs_cake_lib";

const STEAL_RADIUS = 3;

export const INTERACT_ITEMS = [
    "sgs_cake:almond_croissant",
    "sgs_cake:apple_and_blueberry_pie",
    "sgs_cake:apple_cake",
    "sgs_cake:apple_candy",
    "sgs_cake:apple_filled_danish",
    "sgs_cake:apple_filled_tart",
    "sgs_cake:apple_glazed_cupcake",
    "sgs_cake:apple_glazed_donut",
    "sgs_cake:apple_ice_cream",
    "sgs_cake:apple_jelly",
    "sgs_cake:apple_lollypop",
    "sgs_cake:apple_macaron",
    "sgs_cake:apple_pie",
    "sgs_cake:apple_popsicle",
    "sgs_cake:apple_strudel",
    "sgs_cake:baguette",
    "sgs_cake:baked_plums",
    "sgs_cake:banana_bread",
    "sgs_cake:banana_candy",
    "sgs_cake:banana_filled_danish",
    "sgs_cake:banana_filled_tart",
    "sgs_cake:banana_glazed_cupcake",
    "sgs_cake:banana_glazed_donut",
    "sgs_cake:banana_ice_cream",
    "sgs_cake:banana_jelly",
    "sgs_cake:banana_lollypop",
    "sgs_cake:banana_macaron",
    "sgs_cake:banana_popsicle",
    "sgs_cake:banana_split",
    "sgs_cake:banoffee_pie",
    "sgs_cake:birthday_cake",
    "sgs_cake:black_forest_cake",
    "sgs_cake:blueberry_candy",
    "sgs_cake:blueberry_filled_danish",
    "sgs_cake:blueberry_filled_tart",
    "sgs_cake:blueberry_glazed_cupcake",
    "sgs_cake:blueberry_glazed_donut",
    "sgs_cake:blueberry_ice_cream",
    "sgs_cake:blueberry_jelly",
    "sgs_cake:blueberry_lollypop",
    "sgs_cake:blueberry_macaron",
    "sgs_cake:blueberry_popsicle",
    "sgs_cake:bread_slice",
    "sgs_cake:bread_slice_with_strawberry_jam",
    "sgs_cake:bun",
    "sgs_cake:bun_with_strawberry_jam",
    "sgs_cake:butter",
    "sgs_cake:carrot_cake",
    "sgs_cake:cheesecake",
    "sgs_cake:cheese_scone",
    "sgs_cake:chicken_pie",
    "sgs_cake:chocolate_cake",
    "sgs_cake:chocolate_coated_biscuit",
    "sgs_cake:chocolate_flavoured_biscuit",
    "sgs_cake:chocolate_sunday",
    "sgs_cake:plain_cinnamon_roll",
    "sgs_cake:cocoa_butter",
    "sgs_cake:coffee_cake",
    "sgs_cake:coffee_filled_danish",
    "sgs_cake:coffee_filled_tart",
    "sgs_cake:coffee_glazed_cupcake",
    "sgs_cake:coffee_macaron",
    "sgs_cake:coffee_popsicle",
    "sgs_cake:croissant",
    "sgs_cake:cupcake_pan_liner",
    "sgs_cake:dark_chocolate",
    "sgs_cake:dark_chocolate_bar",
    "sgs_cake:dark_chocolate_candy",
    "sgs_cake:dark_chocolate_filled_danish",
    "sgs_cake:dark_chocolate_filled_tart",
    "sgs_cake:dark_chocolate_glazed_cupcake",
    "sgs_cake:dark_chocolate_glazed_donut",
    "sgs_cake:dark_chocolate_ice_cream",
    "sgs_cake:dark_chocolate_lollypop",
    "sgs_cake:dark_chocolate_macaron",
    "sgs_cake:dark_chocolate_popsicle",
    "sgs_cake:dried_fruit_biscuit",
    "sgs_cake:farmhouse_loaf",
    "sgs_cake:fruit_candy",
    "sgs_cake:fruit_filled_danish",
    "sgs_cake:fruit_filled_tart",
    "sgs_cake:fruit_glazed_cupcake",
    "sgs_cake:fruit_glazed_donut",
    "sgs_cake:fruit_ice_cream",
    "sgs_cake:fruit_jelly",
    "sgs_cake:fruit_lollypop",
    "sgs_cake:fruit_macaron",
    "sgs_cake:fruit_pie",
    "sgs_cake:fruit_popsicle",
    "sgs_cake:glazed_cinnamon_roll",
    "sgs_cake:glazed_swirl_bread",
    "sgs_cake:ground_almonds",
    "sgs_cake:ground_walnut",
    "sgs_cake:ice_cream_cake",
    "sgs_cake:jam_filled_round_biscuit",
    "sgs_cake:lollypop_stick",
    "sgs_cake:mango_candy",
    "sgs_cake:mango_filled_danish",
    "sgs_cake:mango_filled_tart",
    "sgs_cake:mango_glazed_cupcake",
    "sgs_cake:mango_glazed_donut",
    "sgs_cake:mango_ice_cream",
    "sgs_cake:mango_jelly",
    "sgs_cake:mango_lollypop",
    "sgs_cake:mango_macaron",
    "sgs_cake:mango_popsicle",
    "sgs_cake:mango_upside-down_loaf",
    "sgs_cake:marzipan",
    "sgs_cake:matcha_candy",
    "sgs_cake:matcha_filled_danish",
    "sgs_cake:matcha_filled_tart",
    "sgs_cake:matcha_glazed_cupcake",
    "sgs_cake:matcha_glazed_donut",
    "sgs_cake:matcha_ice_cream",
    "sgs_cake:matcha_jelly",
    "sgs_cake:matcha_lollypop",
    "sgs_cake:matcha_macaron",
    "sgs_cake:matcha_popsicle",
    "sgs_cake:meat_pie",
    "sgs_cake:milk_chocolate",
    "sgs_cake:milk_chocolate_bar",
    "sgs_cake:milk_chocolate_candy",
    "sgs_cake:milk_chocolate_filled_danish",
    "sgs_cake:milk_chocolate_filled_tart",
    "sgs_cake:milk_chocolate_glazed_cupcake",
    "sgs_cake:milk_chocolate_glazed_donut",
    "sgs_cake:milk_chocolate_ice_cream",
    "sgs_cake:milk_chocolate_lollypop",
    "sgs_cake:milk_chocolate_macaron",
    "sgs_cake:milk_chocolate_popsicle",
    "sgs_cake:molten_sugar",
    "sgs_cake:nut_butter",
    "sgs_cake:nut_butter_candy",
    "sgs_cake:nut_butter_filled_danish",
    "sgs_cake:nut_butter_filled_tart",
    "sgs_cake:nut_butter_glazed_cupcake",
    "sgs_cake:nut_butter_glazed_donut",
    "sgs_cake:nut_butter_ice_cream",
    "sgs_cake:nut_butter_lollypop",
    "sgs_cake:nut_butter_macaron",
    "sgs_cake:nut_butter_popsicle",
    "sgs_cake:pain_au_chocolat",
    "sgs_cake:pancake",
    "sgs_cake:pancake_stack",
    "sgs_cake:plain_biscuit",
    "sgs_cake:plain_cake_layer",
    "sgs_cake:plain_candy",
    "sgs_cake:plain_cupcake",
    "sgs_cake:plain_donut",
    "sgs_cake:plain_jelly",
    "sgs_cake:plain_lollypop",
    "sgs_cake:plain_macaron",
    "sgs_cake:plain_swirl_bread",
    "sgs_cake:plum_candy",
    "sgs_cake:plum_crumble",
    "sgs_cake:plum_filled_danish",
    "sgs_cake:plum_filled_tart",
    "sgs_cake:plum_glazed_cupcake",
    "sgs_cake:plum_glazed_donut",
    "sgs_cake:plum_ice_cream",
    "sgs_cake:plum_jelly",
    "sgs_cake:plum_lollypop",
    "sgs_cake:plum_macaron",
    "sgs_cake:plum_pie",
    "sgs_cake:plum_popsicle",
    "sgs_cake:pork_and_apple_pie",
    "sgs_cake:pretzel",
    "sgs_cake:pumpkin_spice_candy",
    "sgs_cake:pumpkin_spice_filled_danish",
    "sgs_cake:pumpkin_spice_filled_tart",
    "sgs_cake:pumpkin_spice_glazed_cupcake",
    "sgs_cake:pumpkin_spice_glazed_donut",
    "sgs_cake:pumpkin_spice_ice_cream",
    "sgs_cake:pumpkin_spice_jelly",
    "sgs_cake:pumpkin_spice_lollypop",
    "sgs_cake:pumpkin_spice_macaron",
    "sgs_cake:pumpkin_spice_popsicle",
    "sgs_cake:red_velvet_cake",
    "sgs_cake:scones_and_jam",
    "sgs_cake:seeded_loaf",
    "sgs_cake:sour_bread",
    "sgs_cake:spiced_pumpkin_pie",
    "sgs_cake:sponge_cake",
    "sgs_cake:steak_and_potato_pie",
    "sgs_cake:stollen",
    "sgs_cake:strawberries_and_chocolate",
    "sgs_cake:strawberries_and_cream",
    "sgs_cake:strawberry_candy",
    "sgs_cake:strawberry_filled_danish",
    "sgs_cake:strawberry_filled_tart",
    "sgs_cake:strawberry_glazed_cupcake",
    "sgs_cake:strawberry_glazed_donut",
    "sgs_cake:strawberry_ice_cream",
    "sgs_cake:strawberry_jam",
    "sgs_cake:strawberry_jelly",
    "sgs_cake:strawberry_lollypop",
    "sgs_cake:strawberry_macaron",
    "sgs_cake:strawberry_pie",
    "sgs_cake:strawberry_popsicle",
    "sgs_cake:sweet_berry_candy",
    "sgs_cake:sweet_berry_filled_danish",
    "sgs_cake:sweet_berry_filled_tart",
    "sgs_cake:sweet_berry_glazed_cupcake",
    "sgs_cake:sweet_berry_glazed_donut",
    "sgs_cake:sweet_berry_ice_cream",
    "sgs_cake:sweet_berry_jam",
    "sgs_cake:sweet_berry_jelly",
    "sgs_cake:sweet_berry_lollypop",
    "sgs_cake:sweet_berry_macaron",
    "sgs_cake:sweet_berry_popsicle",
    "sgs_cake:tiger_loaf",
    "sgs_cake:tiramisu",
    "sgs_cake:toffee_candy",
    "sgs_cake:toffee_filled_danish",
    "sgs_cake:toffee_filled_tart",
    "sgs_cake:toffee_glazed_cupcake",
    "sgs_cake:toffee_glazed_donut",
    "sgs_cake:toffee_ice_cream",
    "sgs_cake:toffee_lollypop",
    "sgs_cake:toffee_macaron",
    "sgs_cake:toffee_popsicle",
    "sgs_cake:toffee_sunday",
    "sgs_cake:vanilla_candy",
    "sgs_cake:vanilla_filled_danish",
    "sgs_cake:vanilla_filled_tart",
    "sgs_cake:vanilla_glazed_cupcake",
    "sgs_cake:vanilla_glazed_donut",
    "sgs_cake:vanilla_ice_cream",
    "sgs_cake:vanilla_lollypop",
    "sgs_cake:vanilla_macaron",
    "sgs_cake:vanilla_popsicle",
    "sgs_cake:white_chocolate",
    "sgs_cake:white_chocolate_bar",
    "sgs_cake:white_chocolate_candy",
    "sgs_cake:white_chocolate_filled_danish",
    "sgs_cake:white_chocolate_filled_tart",
    "sgs_cake:white_chocolate_glazed_cupcake",
    "sgs_cake:white_chocolate_glazed_donut",
    "sgs_cake:white_chocolate_ice_cream",
    "sgs_cake:white_chocolate_lollypop",
    "sgs_cake:white_chocolate_macaron",
    "sgs_cake:white_chocolate_popsicle",
    "sgs_cake:wholemeal_loaf"
]

mc.world.beforeEvents.playerInteractWithEntity.subscribe((data) => {
    const entity = data.target;
    const player = data.player;

    if (entity.typeId != "sgs_cake:raccoon") return;
    const mainhand = player.getComponent(mc.EntityEquippableComponent.componentId).getEquipmentSlot(mc.EquipmentSlot.Mainhand);
    const heldItem = mainhand.getItem();
    if (heldItem == undefined) return;

    if (entity.getComponent("minecraft:is_tamed") == undefined) return;
    if (entity.getProperty("sgs_cake:has_item")) return;
    const itemData = INTERACT_ITEMS.find((f) => f == heldItem.typeId);
    if (itemData == undefined) return;
    const location = entity.location;
    const dimension = entity.dimension.id;

    mc.system.runTimeout(() => {
        mc.world.getDimension(dimension).runCommand("/execute positioned " + location.x + " " + location.y + " " + location.z + " run /replaceitem entity @e[type=sgs_cake:raccoon,r=0.2]  slot.weapon.mainhand 1 " + itemData);
        entity.setProperty("sgs_cake:has_item", true);
        entity.playAnimation("fright");
        entity.dimension.playSound("mob.fox.ambient", entity.location);

        if (player.getGameMode() != mc.GameMode.Creative) mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
    }, 1)
    return;
});

mc.world.afterEvents.entitySpawn.subscribe((eventData) => {
    const { entity, cause } = eventData;

    if (entity.typeId == "sgs_cake:raccoon") {
        if (cause != "Spawned") return;
        if (mc.world.scoreboard.getObjective("sgs_cake_settings").getScore("RACCOON") == 0) {
            const playerNearby = entity.dimension.getEntities({ location: entity.location, families: ["player"], maxDistance: 6 }); //spawn eggxeption
            if (playerNearby.length > 0) return;

            entity.remove();
        }
    }
})

mc.world.afterEvents.entityHurt.subscribe((eventData) => {

    if (eventData.hurtEntity.typeId == "sgs_cake:raccoon") {
        const sourceEntity = eventData.hurtEntity;
        if (sourceEntity.getProperty("sgs_cake:has_item")) {

            sourceEntity.triggerEvent("evt:drop_item");

            mc.system.runTimeout(() => {
                mc.world.getDimension(sourceEntity.dimension.id).runCommand("/execute positioned " + sourceEntity.location.x + " " + sourceEntity.location.y + " " + sourceEntity.location.z + " run /replaceitem entity @e[type=sgs_cake:raccoon,r=0.2]  slot.weapon.mainhand 1 air");
                sourceEntity.setProperty("sgs_cake:has_item", false);
                sourceEntity.playAnimation("fright");
            }, 3)
        }
    }
})

mc.system.afterEvents.scriptEventReceive.subscribe((eventData) => {
    const { id, sourceEntity, message } = eventData;
    if (sourceEntity == undefined) return;

    if (id == "sgs_cake:raccoon" && message == "refresh") {

        const location = sourceEntity.location;
        const dimension = sourceEntity.dimension.id;

        sourceEntity.setProperty("sgs_cake:has_item", false);
        return;
    };

    if (id == "sgs_cake:raccoon" && message == "steal") {

        const location = sourceEntity.location;
        const dimension = sourceEntity.dimension.id;

        const stealTarget = getStealTarget(sourceEntity);
        if (stealTarget == undefined) return;

        const inventory = stealTarget.getComponent('minecraft:inventory').container;
        const returnItem = inventory?.getItem(0);
        if (returnItem == undefined) return;

        mc.world.getDimension(dimension).runCommand("/execute positioned " + location.x + " " + location.y + " " + location.z + " run /replaceitem entity @e[type=sgs_cake:raccoon,r=0.2]  slot.weapon.mainhand 1 " + returnItem.typeId);
        sourceEntity.setProperty("sgs_cake:has_item", true);
        stealTarget.remove();
        return;
    };
})

export function getStealTarget(entity) {
    const location = entity.location;
    return mc.world.getDimension(entity.dimension.id).getEntities({ location, families: ["sgs_cake_display_raccoon"], maxDistance: STEAL_RADIUS, closest: 1 })[0];
}
