package ru.dbotthepony.mc.otm.network

import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent
import net.neoforged.neoforge.network.registration.HandlerThread
import ru.dbotthepony.mc.otm.android.AndroidResearchManager
import ru.dbotthepony.mc.otm.android.feature.ItemEntityDataPacket
import ru.dbotthepony.mc.otm.compat.vanilla.InventoryScrollPacket
import ru.dbotthepony.mc.otm.data.FlywheelMaterials
import ru.dbotthepony.mc.otm.item.QuantumBatteryItem
import ru.dbotthepony.mc.otm.matter.MatterManager
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.menu.data.ClearItemViewPacket
import ru.dbotthepony.mc.otm.menu.data.ItemViewInteractPacket
import ru.dbotthepony.mc.otm.menu.data.StackAddPacket
import ru.dbotthepony.mc.otm.menu.data.StackChangePacket
import ru.dbotthepony.mc.otm.menu.data.StackRemovePacket
import ru.dbotthepony.mc.otm.menu.matter.CancelTaskPacket
import ru.dbotthepony.mc.otm.menu.matter.PatternsChangePacket
import ru.dbotthepony.mc.otm.menu.matter.ReplicationRequestPacket
import ru.dbotthepony.mc.otm.menu.matter.TasksChangePacket

fun registerNetworkPackets(event: RegisterPayloadHandlersEvent) {
	val r = event.registrar("1.5.0")

	// world
	r
		.playToClient(BlockEntitySyncPacket.TYPE, BlockEntitySyncPacket.CODEC, BlockEntitySyncPacket::play)
		.playToClient(SmokeParticlesPacket.TYPE, SmokeParticlesPacket.CODEC, SmokeParticlesPacket::play)

	// items
	r
		.playToClient(QuantumBatteryItem.PACKET_TYPE, QuantumBatteryItem.PACKET_CODEC, QuantumBatteryItem.ChargePacket::play)
		.playToClient(ItemEntityDataPacket.TYPE, ItemEntityDataPacket.CODEC, ItemEntityDataPacket::play)

	// registries
	r.executesOn(HandlerThread.NETWORK)
		.playToClient(AndroidResearchManager.SYNC_TYPE, AndroidResearchManager.SYNC_CODEC, AndroidResearchManager.SyncPacket::play)
		.playToClient(MatterManager.SYNC_TYPE, MatterManager.SYNC_CODEC, MatterManager.SyncPacket::play)
		.playToClient(FlywheelMaterials.SYNC_TYPE, FlywheelMaterials.SYNC_CODEC, FlywheelMaterials.SyncPacket::play)

	// exopack
	r
		.playToClient(ExopackCarriedPacket.TYPE, ExopackCarriedPacket.CODEC, ExopackCarriedPacket::play)
		.playToClient(ExopackSlotPacket.TYPE, ExopackSlotPacket.CODEC, ExopackSlotPacket::play)
		.playToClient(ExopackMenuInitPacket.TYPE, ExopackMenuInitPacket.CODEC, ExopackMenuInitPacket::play)
		.playToClient(ExopackSmokePacket.TYPE, ExopackSmokePacket.CODEC, ExopackSmokePacket::play)
		.playToServer(ExopackMenuOpen.TYPE, ExopackMenuOpen.CODEC, ExopackMenuOpen::play)

		.playToServer(PickItemFromInventoryPacket.TYPE, PickItemFromInventoryPacket.CODEC, PickItemFromInventoryPacket::play)

		.playToServer(DisplayExopackPacket.TYPE, DisplayExopackPacket.CODEC, DisplayExopackPacket::play)
		.playToServer(HideExopackPacket.TYPE, HideExopackPacket.CODEC, HideExopackPacket::play)
		.playToServer(EnableExopackGlowPacket.TYPE, EnableExopackGlowPacket.CODEC, EnableExopackGlowPacket::play)
		.playToServer(DisableExopackGlowPacket.TYPE, DisableExopackGlowPacket.CODEC, DisableExopackGlowPacket::play)
		.playToServer(ResetExopackColorPacket.TYPE, ResetExopackColorPacket.CODEC, ResetExopackColorPacket::play)
		.playToServer(SetExopackColorPacket.TYPE, SetExopackColorPacket.CODEC, SetExopackColorPacket::play)

	// otm player general
	r
		.playToClient(MatteryPlayerDataPacket.TYPE, MatteryPlayerDataPacket.CODEC, MatteryPlayerDataPacket::play)

	// android
	r
		.playToClient(PlayerIterationPacket.TYPE, PlayerIterationPacket.CODEC, PlayerIterationPacket::play)
		.playToClient(GlitchPacket.TYPE, GlitchPacket.CODEC, GlitchPacket::play)
		.playToClient(ShockwaveEffectPacket.TYPE, ShockwaveEffectPacket.CODEC, ShockwaveEffectPacket::play)

		.playToServer(AndroidResearchRequestPacket.TYPE, AndroidResearchRequestPacket.CODEC, AndroidResearchRequestPacket::play)
		.playToClient(AndroidResearchSyncPacket.TYPE, AndroidResearchSyncPacket.CODEC, AndroidResearchSyncPacket::play)

		.playToClient(AndroidFeatureSyncPacket.TYPE, AndroidFeatureSyncPacket.CODEC, AndroidFeatureSyncPacket::play)
		.playToClient(AndroidFeatureRemovePacket.TYPE, AndroidFeatureRemovePacket.CODEC, AndroidFeatureRemovePacket::play)

		.playToServer(SwitchAndroidFeaturePacket.TYPE, SwitchAndroidFeaturePacket.CODEC, SwitchAndroidFeaturePacket::play)
		.playToServer(ActivateAndroidFeaturePacket.TYPE, ActivateAndroidFeaturePacket.CODEC, ActivateAndroidFeaturePacket::play)

		.playToServer(TriggerShockwavePacket.TYPE, TriggerShockwavePacket.CODEC, TriggerShockwavePacket::play)
		.playToServer(TriggerJumpBoostPacket.TYPE, TriggerJumpBoostPacket.CODEC, TriggerJumpBoostPacket::play)

	// menus
	r
		// carried item stack set
		.playToClient(SetCarriedPacket.TYPE, SetCarriedPacket.CODEC, SetCarriedPacket::play)

		// server->client data
		.playToClient(MenuDataPacket.TYPE, MenuDataPacket.CODEC, MenuDataPacket::play)
		// client->server input
		.playToServer(MatteryMenu.PlayerInputPacket.TYPE, MatteryMenu.PlayerInputPacket.CODEC, MatteryMenu.PlayerInputPacket::play)

		// extended inventory compat
		.playToServer(InventoryScrollPacket.TYPE, InventoryScrollPacket.CODEC, InventoryScrollPacket::play)

		// matter replication menu
		.playToClient(PatternsChangePacket.TYPE, PatternsChangePacket.CODEC, PatternsChangePacket::play)
		.playToClient(TasksChangePacket.TYPE, TasksChangePacket.CODEC, TasksChangePacket::play)
		.playToServer(ReplicationRequestPacket.TYPE, ReplicationRequestPacket.CODEC, ReplicationRequestPacket::play)
		.playToServer(CancelTaskPacket.TYPE, CancelTaskPacket.CODEC, CancelTaskPacket::play)

		// item view
		.playToClient(StackAddPacket.TYPE, StackAddPacket.CODEC, StackAddPacket::play)
		.playToClient(StackChangePacket.TYPE, StackChangePacket.CODEC, StackChangePacket::play)
		.playToClient(StackRemovePacket.TYPE, StackRemovePacket.CODEC, StackRemovePacket::play)
		.playToClient(ClearItemViewPacket.TYPE, ClearItemViewPacket.CODEC, ClearItemViewPacket::play)
		.playToServer(ItemViewInteractPacket.TYPE, ItemViewInteractPacket.CODEC, ItemViewInteractPacket::play)
}
