import { Direction, world } from "@minecraft/server";
import { ConfigsComponent } from "../configs";
import { BookPileComponent } from "../content/bookPileStuff";
import { CandleInteractComponent, CandleParticleComponent } from "../content/candleHolderStuff";
import { SugarBlockComponent } from "../content/sugarBlockStuff";
import { PresentComponent } from "../content/presentsStuff";
import { SoapBlockComponent, SoapItemComponent } from "../content/soapStuff";
import { PumiceExtinguishComponent, PumiceSuckLavaComponent } from "../content/pumiceStuff";
import { TurnTableInteractComponent, TurnTableTickComponent } from "../content/turnTableStuff";
import { RopeWalkComponent, RopeWindDownComponent, RopeWindUpComponent } from "../content/ropeStuff";
import { AwningFallComponent } from "../content/awningStuff";
import { PulleyComponent } from "../content/pulleyStuff";
import { BellowsComponent } from "../content/bellowsStuff";
import { SpeakerBlockInteractComponent, SpeakerBlockTickComponent } from "../content/speakerBlockStuff";
import { PedestalComponent } from "../content/pedestalStuff";
import { EndStoneLampComponent } from "../content/endStoneLampStuff";
import { CabinSidingSlabComponent } from "../content/fineWoodStuft";
import { FlowerBoxEmptyComponent, FLowerBoxFullComponent } from "../content/flowerBoxStuff";
import { SlabCancelPlacementComponent } from "../layersManager";
import { CandleHolderItemComponent, SconceParticleComponent } from "../content/sconceStuff";
import { FaucetInteractComponent, FaucetTickComponent } from "../content/faucetStuff";
import { BambooSpikeComponent } from "../content/bambooSpikesStuff";
import { BlockEntityComponent } from "../blockEntitiesManager";
import { GlobeComponent } from "../content/globeStuff";
import { GoldDoorComponent, GoldTrapdoorComponent } from "../content/goldDoorStuff";
import { IronGateComponent } from "../content/ironGateStuff";
import { ClockComponent } from "../content/clockStuff";
import { ChalkBlockComponent, ChalkItemComponent } from "../content/chalkStuff";
import { FilledJarComponent, JarComponent } from "../content/jarStuff";
import { CheeseWheelComponent, ChocolateMilkCauldronComponent, MilkCauldronComponent } from "../content/cheeseStuff";
import { StairsPlacementComponent } from "../content/stairsStuff";
import { FallingBlockComponent } from "../fallingBlocksManager";
import { RakedGravelComponent } from "../content/rakedGravelStuff";
import { AshLayerComponent } from "../content/ashStuff";
import { WrenchUseItemComponent } from "../content/wrenchStuff";
import { HatStandItemComponent } from "../content/hatStandStuff";
import { GuideBookComponent } from "../guideBook";
import { CushionItemComponent } from "../content/cushionStuff";
import { SlingshotComponent } from "../content/slingshotStuff";
import { directionToAxis, flipDir, offsetPos } from "../util/dirUtils";
import { isBlockSolid } from "../util/miscUtils";
import { SuppStates } from "./suppStates";
import { FlaxCropGrowthBlockComponent, FlaxUpperComponent } from "../content/flaxStuff";
import { ConfettiPopperComponent } from "../content/confettiStuff";
import { BrazierComponent } from "../content/brazierStuff";
world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    /*
    blockComponentRegistry.registerCustomComponent(
        "razz_sup:swing_hand",
        SwingHandAnimComponent
    );*/
    blockComponentRegistry.registerCustomComponent("razz_sup:only_place_on_solid", new SolidCheckComponent());
    blockComponentRegistry.registerCustomComponent("razz_sup:only_place_on_solid_below", new SolidCheckComponent(Direction.Down));
    blockComponentRegistry.registerCustomComponent("razz_sup:book_on_place", BookPileComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:candle_holder_particle", CandleParticleComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:candle_holder_interact", CandleInteractComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:sugar_block_dissolve", SugarBlockComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:present", PresentComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:soap_slip", SoapBlockComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:brazier", BrazierComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:pumice_absorb_lava", PumiceSuckLavaComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:pumice_extinguish", PumiceExtinguishComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:turn_table_rotate", TurnTableTickComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:turn_table_interact", TurnTableInteractComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:rope_wind_up", RopeWindUpComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:rope_walk", RopeWalkComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:awning_bounce", AwningFallComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:pulley_operate", PulleyComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:bellows_blow", BellowsComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:speaker_block_operate", SpeakerBlockTickComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:speaker_block_interact", SpeakerBlockInteractComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:pedestal", PedestalComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:end_stone_randomize", EndStoneLampComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:cabin_siding_slab_placement", CabinSidingSlabComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:flower_box_plant", FlowerBoxEmptyComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:flower_box_pickup", FLowerBoxFullComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:slab_placement", SlabCancelPlacementComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:sconce_particle", SconceParticleComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:faucet_operate", FaucetTickComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:faucet_interact", FaucetInteractComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:bamboo_spikes_damage", BambooSpikeComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:block_entity", BlockEntityComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:globe_spin", GlobeComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:gold_door", GoldDoorComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:gold_trapdoor", GoldTrapdoorComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:iron_gate", IronGateComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:clock_block", ClockComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:randomize_drawing", ChalkBlockComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:jar_interact", JarComponent);
    //  blockComponentRegistry.registerCustomComponent(
    //      "razz_sup:wicker_fence",
    //      WickerFenceComponent
    //  )
    /*
    blockComponentRegistry.registerCustomComponent(
        "razz_sup:seat",
        CushionSeatComponent
    )*/
    blockComponentRegistry.registerCustomComponent("razz_sup:milk_cauldron", MilkCauldronComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:chocolate_milk_cauldron", ChocolateMilkCauldronComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:cheese_wheel", CheeseWheelComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:stairs", StairsPlacementComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:falling_block", FallingBlockComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:raked_gravel", RakedGravelComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:ash_dissolve", AshLayerComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:flax_growth", FlaxCropGrowthBlockComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:flax_upper", FlaxUpperComponent);
    blockComponentRegistry.registerCustomComponent("razz_sup:turn_on_from_redstone", new TurnOnFromRedstoneComponent());
    blockComponentRegistry.registerCustomComponent("razz_sup:pillar_placement", {
        beforeOnPlayerPlace(event) {
            event.permutationToPlace = event.permutationToPlace.withState(SuppStates.PillarAxis, directionToAxis(event.face));
        }
    });
});
world.beforeEvents.worldInitialize.subscribe(({ itemComponentRegistry }) => {
    itemComponentRegistry.registerCustomComponent("razz_sup:wrench_rotate", WrenchUseItemComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:hat_stand_place", HatStandItemComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:rope_wind_down", RopeWindDownComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:soap_item_wash", SoapItemComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:candle_holder_place", CandleHolderItemComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:cycle_chalk_drawing", ChalkItemComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:confetti_popper", ConfettiPopperComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:cushion", CushionItemComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:filled_jar", FilledJarComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:open_guide_book", GuideBookComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:open_configs", ConfigsComponent);
    itemComponentRegistry.registerCustomComponent("razz_sup:slingshot_shoot", SlingshotComponent);
    // Adding health to the player when candy is eaten
    // Define a map of item identifiers to their health restore values
    const healthRestoreItems = {
        "razz_sup:heal_item_2": 2,
        "razz_sup:heal_item_3": 3,
        "razz_sup:heal_item_4": 4,
        "razz_sup:heal_item_5": 5
    };
    // Loop through each item in the map and register its custom behavior
    for (const [itemIdentifier, healthRestoreValue] of Object.entries(healthRestoreItems)) {
        itemComponentRegistry.registerCustomComponent(itemIdentifier, new HealComponent(healthRestoreValue));
    }
});
class HealComponent {
    constructor(healthRestoreValue) {
        this.healthRestoreValue = healthRestoreValue;
        this.onConsume = (ev) => {
            const player = ev.source; // The entity consuming the item
            // Get the player's health component
            const healthComponent = player.getComponent('health');
            if (healthComponent) {
                // Increase the player's health by the specified value
                const newHealth = healthComponent.currentValue + this.healthRestoreValue;
                healthComponent.setCurrentValue(newHealth);
            }
        };
    }
}
class SolidCheckComponent {
    constructor(myDir = undefined) {
        this.myDir = myDir;
        //could be done better by checking blocks around for valid placement
        this.beforeOnPlayerPlace = (event) => {
            const block = event.block;
            const dir = this.myDir ?? event.face;
            const behindBlock = offsetPos(block, flipDir(dir));
            console.error("performing solid check: " + behindBlock.typeId);
            if (!isBlockSolid(behindBlock, dir)) {
                event.cancel = true;
            }
        };
        this.onPlace = (event) => {
            //console.log("Placed block: ", event.block.typeId);
        };
    }
}
const SwingHandAnimComponent = {
    onPlace(event) {
        //just this will trigger the anim
    }
};
class TurnOnFromRedstoneComponent {
    onTick(event) {
        const power = event.block.getRedstonePower() | 0;
        if (power > 0) {
            event.block.setPermutation(event.block.permutation.withState(SuppStates.Powered, true));
        }
    }
}
