package fr.koka99cab.ibelievechickenfly.chickenboat;

import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.vehicle.boat.AbstractBoat;

public final class ChickenBoatClientController {
	private ChickenBoatClientController() {
	}

	public static void register() {
		ClientTickEvents.END_CLIENT_TICK.register(ChickenBoatClientController::tick);
	}

	private static void tick(Minecraft client) {
		if (client.player == null || client.level == null) {
			return;
		}

		Entity vehicle = client.player.getVehicle();

		if (!(vehicle instanceof AbstractBoat boat) || !ChickenBoatUtil.hasChickenPassenger(boat)) {
			return;
		}

		if (!ClientPlayNetworking.canSend(ChickenBoatControlPayload.ID)) {
			return;
		}

		boolean up = client.options.keyJump.isDown();
		ClientPlayNetworking.send(new ChickenBoatControlPayload(up));
		ChickenBoatFlightPhysics.apply(boat, up);
	}
}
