package net.geforcemods.securitycraft.screen;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;

import net.geforcemods.securitycraft.SCContent;
import net.geforcemods.securitycraft.containers.GenericTEContainer;
import net.geforcemods.securitycraft.screen.components.ClickButton;
import net.geforcemods.securitycraft.tileentity.IMSTileEntity;
import net.geforcemods.securitycraft.tileentity.IMSTileEntity.IMSTargetingMode;
import net.geforcemods.securitycraft.util.ClientUtils;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class IMSScreen extends ContainerScreen<GenericTEContainer>{

	private static final ResourceLocation TEXTURE = new ResourceLocation("securitycraft:textures/gui/container/blank.png");
	private final TranslationTextComponent imsName = ClientUtils.localize(SCContent.IMS.get().getTranslationKey());
	private final TranslationTextComponent target = ClientUtils.localize("gui.securitycraft:ims.target");

	private IMSTileEntity tileEntity;
	private ClickButton targetButton;
	private IMSTargetingMode targetMode;

	public IMSScreen(GenericTEContainer container, PlayerInventory inv, ITextComponent name) {
		super(container, inv, name);
		tileEntity = (IMSTileEntity)container.te;
		targetMode = tileEntity.getTargetingOption();
	}

	@Override
	public void init(){
		super.init();

		addButton(targetButton = new ClickButton(0, width / 2 - 38, height / 2 - 58, 120, 20, "", this::actionPerformed));
		updateButtonText();
	}

	/**
	 * Draw the foreground layer for the GuiContainer (everything in front of the items)
	 */
	@Override
	protected void drawGuiContainerForegroundLayer(MatrixStack matrix, int mouseX, int mouseY){

		font.func_243248_b(matrix, imsName, xSize / 2 - font.getStringPropertyWidth(imsName) / 2, 6, 4210752);
		font.func_243248_b(matrix, target, xSize / 2 - 78, 30, 4210752);
	}

	@Override
	protected void drawGuiContainerBackgroundLayer(MatrixStack matrix, float partialTicks, int mouseX, int mouseY) {
		renderBackground(matrix);
		RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
		minecraft.getTextureManager().bindTexture(TEXTURE);
		int startX = (width - xSize) / 2;
		int startY = (height - ySize) / 2;
		this.blit(matrix, startX, startY, 0, 0, xSize, ySize);
	}

	protected void actionPerformed(ClickButton button){
		if(button.id == 0){
			targetMode = IMSTargetingMode.values()[(targetMode.ordinal() + 1) % IMSTargetingMode.values().length]; //next enum value
			tileEntity.setTargetingOption(targetMode);
			ClientUtils.syncTileEntity(tileEntity);
			updateButtonText();
		}
	}

	private void updateButtonText() {
		if(targetMode == IMSTargetingMode.PLAYERS)
			targetButton.setMessage(ClientUtils.localize("gui.securitycraft:srat.targets3"));
		else if(targetMode == IMSTargetingMode.PLAYERS_AND_MOBS)
			targetButton.setMessage(ClientUtils.localize("gui.securitycraft:srat.targets1"));
		else if(targetMode == IMSTargetingMode.MOBS)
			targetButton.setMessage(ClientUtils.localize("gui.securitycraft:srat.targets2"));
	}

}

@@ -1747,10 +1747,10 @@
 2 - 
-38
+75
 , he
@@ -1764,14 +1764,14 @@
 2 - 
-5
+3
 8, 1
-2
+5
 0, 2
@@ -2187,10 +2187,47 @@
 2 - 
-78
+font.getStringPropertyWidth(target) / 2
 , 30
@@ -2989,54 +2989,8 @@
 ) %7B%0A
-%09%09if(targetMode == IMSTargetingMode.PLAYERS)%0A%09
 %09%09ta
@@ -3067,286 +3067,50 @@
 gets
-3%22));%0A%09%09else if(targetMode == IMSTargetingMode.PLAYERS_AND_MOBS)%0A%09%09%09targetButton.setMessage(ClientUtils.localize(%22gui.securitycraft:srat.targets1%22));%0A%09%09else if(targetMode == IMSTargetingMode.MOBS)%0A%09%09%09targetButton.setMessage(ClientUtils.localize(%22gui.securitycraft:srat.targets2%22
+%22 + (((targetMode.ordinal() + 2) %25 3) + 1)
 ));%0A

fix ims button and small code improvement