package com.leisuretimedock.blasttravelreborn.client.screen;

import com.leisuretimedock.blasttravelreborn.BlastTravelReborn;
import com.leisuretimedock.blasttravelreborn.content.entity.cannon.CannonBehavior;
import com.leisuretimedock.blasttravelreborn.content.menu.CannonContainerMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;
@OnlyIn(Dist.CLIENT)
public class CannonScreen extends AbstractContainerScreen<CannonContainerMenu> {
	private static final ResourceLocation TEXTURE = BlastTravelReborn.id("textures/gui/container/cannon.png");

	private final ItemStack[] allowedBehaviorStacks = CannonBehavior.allBehaviors().stream()
			.filter(b -> b.icon != Items.AIR).map(b -> new ItemStack(b.icon)).toArray(ItemStack[]::new);

	private long time = (long) (Math.random() * 1000);

	public CannonScreen(CannonContainerMenu handler, Inventory inventory, Component title) {
		super(handler, inventory, title);

		this.imageHeight = 140;
		this.inventoryLabelY = 47;
		this.titleLabelY = 8;
		this.titleLabelX = 61;
	}

	@Override
	public void render(@NotNull GuiGraphics gui, int mouseX, int mouseY, float delta) {
		super.render(gui, mouseX, mouseY, delta);
		this.renderTooltip(gui, mouseX, mouseY);
	}

	@Override
	protected void renderBg(@NotNull GuiGraphics gui, float delta, int mouseX, int mouseY) {
		this.renderBackground(gui);

		gui.blit(TEXTURE, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);

		if (this.getMenu().inventory.getItem(2).isEmpty()) {
			int x = this.leftPos + 98; int y = this.topPos + 20;
			gui.renderItem(
					this.allowedBehaviorStacks[Mth.floor((float)this.time / 30) % this.allowedBehaviorStacks.length], x, y);

			RenderSystem.depthFunc(516);
			gui.fill(x, y, x + 16, y + 16, 0x8B8B8B8B);
			RenderSystem.depthFunc(515);
		}

		for (int i = 0; i < 2; i++) {
			if (this.getMenu().inventory.getItem(i).isEmpty()) {
				gui.blit(TEXTURE, this.leftPos + 62 + (18 * i), this.topPos + 20, 16 * i, 140, 16, 16);
			}
		}
	}

	@Override
	protected void containerTick() {
		this.time++;
	}
}
