import { system, world } from '@minecraft/server';

import { cableConnectionComponent } from './components/blocks/cableConnectionComponent.js';
import { cableSignalComponent } from './components/blocks/cableSignalComponent.js';
import { reinforcedCableSignalComponent } from './components/blocks/reinforcedCableSignalComponent.js';
import { repeaterCableSignalComponent } from './components/blocks/repeaterCableSignalComponent.js';
import { clockPanelComponent } from './components/blocks/clockPanelComponent.js';
import { powerPureStateComponent, powerStateComponent, powerStateConnectorComponent, powerStateReceiverComponent, powerStateTrapdoorComponent, powerStateUpDownComponent } from './components/blocks/powerStateComponent.js';
import { switchStateComponent } from './components/blocks/switchStateComponent.js';
import { buttonReloadComponent } from './components/blocks/buttonReloadComponent.js';
import { destroyBlockPositionComponent } from './components/blocks/destroyBlockPositionComponent.js';
import { flashLightComponent } from './components/blocks/flashLightComponent.js';
import { digitalTntComponent } from './components/blocks/digitalTntComponent.js';
import { dropBoxComponent } from './components/blocks/dropBoxComponent.js';
import { batteryComponent } from './components/blocks/batteryComponent.js';
import { batteryPlaceComponent } from './components/blocks/batteryPlaceComponent.js';
import { teleportCheckpointComponent } from './components/blocks/teleportCheckpointComponent.js';
import { teleportComponent } from './components/blocks/teleportComponent.js';
import { analyzerActionComponent, analyzerLootComponent } from './components/blocks/analyzerComponent.js';
import { copperMobSpawnerActionComponent, copperMobSpawnerComponent } from './components/blocks/copperMobSpawner.js';
import { buddingChargedAmethystComponent } from './components/blocks/buddingChargedAmethystComponent.js';
import { chargedAmethystCrystalComponent } from './components/blocks/chargedAmethystCrystalComponent.js';
import { digitalDoorLowerComponent, digitalDoorUpperComponent } from './components/blocks/digitalDoorComponent.js';
import { furnaceEnergyActionComponent, furnaceEnergyOffComponent, furnaceEnergyOnComponent } from './components/blocks/furnaceEnergyComponent.js';
import { plasmaWeaponSmgComponent } from './components/items/plasmaWeaponSmgComponent.js';
import { plasmaDrillSwichModeComponent } from './components/items/plasmaDrillSwichModeComponent.js';
import { plasmaDrillMineBlockComponent } from './components/items/plasmaDrillMineBlockComponent.js';
import { plasmaDrillHurtEntityComponent } from './components/items/plasmaDrillHurtEntityComponent.js';
import { plasmaLighterComponent } from './components/items/plasmaLighterComponent.js';
import { plasmaLighterUnloadedComponent } from './components/items/plasmaLighterUnloadedComponent.js';
import { plasmaBridgeComponent } from './components/blocks/plasmaBridgeComponent.js';
import { plasmaBridgeNSComponent } from './components/blocks/plasmaBridgeNSComponent.js';
import { plasmaBridgeWEComponent } from './components/blocks/plasmaBridgeWEComponent.js';
import { waterwheelComponent } from './components/blocks/waterwheelComponent.js';


import { farmerComponent } from './components/blocks/farmerComponent.js';
import { cableConnectionUpComponent } from './components/blocks/cableConnectionUpComponent.js';
import { weatherControlComponent } from './components/blocks/weatherControlComponent.js';
import { mobSensorComponent } from './components/blocks/mobSensorComponent';
import { fluidConverterComponent } from './components/blocks/fluidConverterComponent';
import { itemTransferNodeComponent } from './components/blocks/itemTransferNodeComponent';
import { digitalBlockComponent } from './components/blocks/digitalBlockComponent';
// aquí se agregan los imports de componentes block

import { brokenToolSwitcherComponent } from './components/items/brokenToolSwitcherComponent';
import { chestSorterComponent } from './components/items/chestSorterComponent';
import { guideComponent } from './components/items/guideComponent';
import { digitalSpawnComponent } from './components/items/digitalSpawnComponent';
// aquí se agregan los imports de componentes item


// aquí se agregan los imports de libraries
import { plasmaBootsEvent } from './events/items/attachables/plasmaBootsEvent.js';
import { breakToolSwitcherEvent } from './events/items/utils/breakToolSwitcherEvent.js';
import { resourceRecyclerComponent } from './components/blocks/resourceRecyclerComponent.js';
import { plasmaDrillLoreEvent } from './events/items/attachables/plasmaDrillLoreEvent.js';

world.beforeEvents.worldInitialize.subscribe(initEvent => {
	//Bloques
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:destroy_block_position', destroyBlockPositionComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:button_reload', buttonReloadComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:switch_state', switchStateComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:battery_place', batteryPlaceComponent);

	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:power_state', powerStateComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:power_pure_state', powerPureStateComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:power_connector_state', powerStateConnectorComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:power_receiver_state', powerStateReceiverComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:power_trapdoor_state', powerStateTrapdoorComponent);

	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:cable_signal', cableSignalComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:reinforced_cable_signal', reinforcedCableSignalComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:repeater_cable_signal', repeaterCableSignalComponent);

	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:cable_connection', cableConnectionComponent);

	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:analyzer_action', analyzerActionComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:analyzer_loot', analyzerLootComponent);


	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:copper_spawner_action', copperMobSpawnerActionComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:copper_spawner', copperMobSpawnerComponent);

	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:furnace_energy_action', furnaceEnergyActionComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:furnace_energy_off', furnaceEnergyOffComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:furnace_energy_on', furnaceEnergyOnComponent);

	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:budding_charged_amethyst', buddingChargedAmethystComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:charged_amethyst_crystal', chargedAmethystCrystalComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:plasma_bridge_north_south', plasmaBridgeNSComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:plasma_bridge_west_east', plasmaBridgeWEComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:teleport_checkpoint', teleportCheckpointComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:digital_door_lower', digitalDoorLowerComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:digital_door_upper', digitalDoorUpperComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:plasma_bridge', plasmaBridgeComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:clock_panel', clockPanelComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:flash_light', flashLightComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:digital_tnt', digitalTntComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:waterwheel', waterwheelComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:drop_box', dropBoxComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:teleport', teleportComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:battery', batteryComponent);

	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:farmer', farmerComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:cable_connection_up', cableConnectionUpComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:power_state_up_down', powerStateUpDownComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:weather_control', weatherControlComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:mob_sensor', mobSensorComponent);
    initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:fluid_converter', fluidConverterComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:item_transfer_node', itemTransferNodeComponent);
	initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:resource_recycler', resourceRecyclerComponent);
    initEvent.blockComponentRegistry.registerCustomComponent('nps_ec:digital_block', digitalBlockComponent);
    // aquí se agregan los registros de componentes block

	//Items
	initEvent.itemComponentRegistry.registerCustomComponent('nps_ec:fast_plasma_weapon', plasmaWeaponSmgComponent);
	initEvent.itemComponentRegistry.registerCustomComponent('nps_ec:plasma_drill_swich_mode', plasmaDrillSwichModeComponent);
	initEvent.itemComponentRegistry.registerCustomComponent('nps_ec:plasma_drill_mine_block', plasmaDrillMineBlockComponent);
	initEvent.itemComponentRegistry.registerCustomComponent('nps_ec:plasma_drill_hurt_entity', plasmaDrillHurtEntityComponent);
	initEvent.itemComponentRegistry.registerCustomComponent('nps_ec:plasma_lighter', plasmaLighterComponent);
	initEvent.itemComponentRegistry.registerCustomComponent('nps_ec:plasma_lighter_unloaded', plasmaLighterUnloadedComponent);
	initEvent.itemComponentRegistry.registerCustomComponent('nps_ec:broken_tool_switcher', brokenToolSwitcherComponent);
    initEvent.itemComponentRegistry.registerCustomComponent('nps_ec:chest_sorter', chestSorterComponent);
	initEvent.itemComponentRegistry.registerCustomComponent('nps_ec:guide', guideComponent);
	initEvent.itemComponentRegistry.registerCustomComponent('nps_ec:digital_spawn', digitalSpawnComponent);
    // aquí se agregan los registros de componentes item




	system.runInterval(() => {
		system.run(() => {
		try {
				const players = world.getAllPlayers();
				for (let i = 0; i < players.length; i++) {
					const player = players[i];
					plasmaBootsEvent(player);
					breakToolSwitcherEvent(player);
					plasmaDrillLoreEvent(player);
					const isInit = player.getDynamicProperty("nps_ec:init");

					if (isInit == undefined || !isInit) {
						player.runCommand("give @s nps_ec:guide")
						player.setDynamicProperty("nps_ec:init", true);
					}
				}
			} catch { }
		});
	}, 2)
});

world.beforeEvents.playerBreakBlock.subscribe((e) => {
	const { block, itemStack } = e;
	if (itemStack == undefined) return;
	if (itemStack.getComponent("minecraft:enchantable") == undefined) return;
	if (itemStack.getComponent("minecraft:enchantable").getEnchantment("minecraft:silk_touch") != undefined) {
		if (block.hasTag("nps_ec:disable_silk_touch")) {
			e.cancel = true;
			system.runTimeout(() => {
				try {
					//block.dimension.playSound('mob.wither.break_block', block.location, { volume: 0.5 });
					block.dimension.runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} minecraft:air destroy`);
				} catch { }
			}, 1)
		}
	}
})