package net.minecraft.server.level;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundGameEventPacket;
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;

public class DemoMode extends ServerPlayerGameMode {
	public static final int DEMO_DAYS = 5;
	public static final int TOTAL_PLAY_TICKS = 120500;
	private boolean displayedIntro;
	private boolean demoHasEnded;
	private int demoEndedReminder;
	private int gameModeTicks;

	public DemoMode(final ServerPlayer player) {
		super(player);
	}

	@Override
	public void tick() {
		super.tick();
		this.gameModeTicks++;
		long time = this.level.getGameTime();
		long day = time / 24000L + 1L;
		if (!this.displayedIntro && this.gameModeTicks > 20) {
			this.displayedIntro = true;
			this.player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.DEMO_EVENT, 0.0F));
		}

		this.demoHasEnded = time > 120500L;
		if (this.demoHasEnded) {
			this.demoEndedReminder++;
		}

		if (time % 24000L == 500L) {
			if (day <= 6L) {
				if (day == 6L) {
					this.player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.DEMO_EVENT, 104.0F));
				} else {
					this.player.sendSystemMessage(Component.translatable("demo.day." + day));
				}
			}
		} else if (day == 1L) {
			if (time == 100L) {
				this.player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.DEMO_EVENT, 101.0F));
			} else if (time == 175L) {
				this.player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.DEMO_EVENT, 102.0F));
			} else if (time == 250L) {
				this.player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.DEMO_EVENT, 103.0F));
			}
		} else if (day == 5L && time % 24000L == 22000L) {
			this.player.sendSystemMessage(Component.translatable("demo.day.warning"));
		}
	}

	private void outputDemoReminder() {
		if (this.demoEndedReminder > 100) {
			this.player.sendSystemMessage(Component.translatable("demo.reminder"));
			this.demoEndedReminder = 0;
		}
	}

	@Override
	public void handleBlockBreakAction(
		final BlockPos pos, final ServerboundPlayerActionPacket.Action action, final Direction direction, final int maxY, final int sequence
	) {
		if (this.demoHasEnded) {
			this.outputDemoReminder();
		} else {
			super.handleBlockBreakAction(pos, action, direction, maxY, sequence);
		}
	}

	@Override
	public InteractionResult useItem(final ServerPlayer player, final Level level, final ItemStack itemStack, final InteractionHand hand) {
		if (this.demoHasEnded) {
			this.outputDemoReminder();
			return InteractionResult.PASS;
		} else {
			return super.useItem(player, level, itemStack, hand);
		}
	}

	@Override
	public InteractionResult useItemOn(
		final ServerPlayer player, final Level level, final ItemStack itemStack, final InteractionHand hand, final BlockHitResult hitResult
	) {
		if (this.demoHasEnded) {
			this.outputDemoReminder();
			return InteractionResult.PASS;
		} else {
			return super.useItemOn(player, level, itemStack, hand, hitResult);
		}
	}
}
