package com.hbm.render.item.weapon.sedna;

import org.lwjgl.opengl.GL11;

import com.hbm.items.weapon.sedna.ItemGunBaseNT;
import com.hbm.items.weapon.sedna.mods.XWeaponModManager;
import com.hbm.main.MainRegistry;
import com.hbm.main.ResourceManager;
import com.hbm.render.anim.HbmAnimations;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;

public class ItemRenderAm180 extends ItemRenderWeaponBase {

	@Override
	protected float getTurnMagnitude(ItemStack stack) { return ItemGunBaseNT.getIsAiming(stack) ? 2.5F : -0.5F; }

	@Override
	public float getViewFOV(ItemStack stack, float fov) {
		float aimingProgress = ItemGunBaseNT.prevAimingProgress + (ItemGunBaseNT.aimingProgress - ItemGunBaseNT.prevAimingProgress) * interp;
		return  fov * (1 - aimingProgress * 0.33F);
	}

	@Override
	public void setupFirstPerson(ItemStack stack) {
		GL11.glTranslated(0, 0, 0.875);
		
		float offset = 0.8F;
		standardAimingTransform(stack,
				-1F * offset, -1F * offset, 1F * offset,
				0, -4.1875 / 8D, 0.25);
	}

	@Override
	public void renderFirstPerson(ItemStack stack) {
		
		ItemGunBaseNT gun = (ItemGunBaseNT) stack.getItem();
		Minecraft.getMinecraft().renderEngine.bindTexture(ResourceManager.am180_tex);
		double scale = 0.1875D;
		GL11.glScaled(scale, scale, scale);
		
		boolean silenced = this.hasSilencer(stack);

		double[] equip = HbmAnimations.getRelevantTransformation("EQUIP");
		double[] recoil = HbmAnimations.getRelevantTransformation("RECOIL");
		double[] magazine = HbmAnimations.getRelevantTransformation("MAG");
		double[] magTurn = HbmAnimations.getRelevantTransformation("MAGTURN");
		double[] magSpin = HbmAnimations.getRelevantTransformation("MAGSPIN");
		double[] bolt = HbmAnimations.getRelevantTransformation("BOLT");
		double[] turn = HbmAnimations.getRelevantTransformation("TURN");
		
		GL11.glTranslated(0, -2, -6);
		GL11.glRotated(equip[0], 1, 0, 0);
		GL11.glTranslated(0, 2, 6);

		GL11.glRotated(turn[2], 0, 0, 1);
		
		GL11.glShadeModel(GL11.GL_SMOOTH);
		
		GL11.glTranslated(0, 0, recoil[2]);

		HbmAnimations.applyRelevantTransformation("Gun");
		ResourceManager.am180.renderPart("Gun");
		if(silenced) ResourceManager.am180.renderPart("Silencer");

		GL11.glPushMatrix();
		HbmAnimations.applyRelevantTransformation("Trigger");
		ResourceManager.am180.renderPart("Trigger");
		GL11.glPopMatrix();

		GL11.glPushMatrix();
		GL11.glTranslated(0, 0, bolt[2]);
		HbmAnimations.applyRelevantTransformation("Bolt");
		ResourceManager.am180.renderPart("Bolt");
		GL11.glPopMatrix();

		GL11.glPushMatrix();
		GL11.glTranslated(magazine[0], magazine[1], magazine[2]);

		GL11.glTranslated(0, 2.0625, 3.75);
		GL11.glRotated(magTurn[0], 1, 0, 0);
		GL11.glRotated(magTurn[2], 0, 0, 1);
		GL11.glTranslated(0, -2.0625, -3.75);

		GL11.glTranslated(0, 2.3125, 1.5);
		GL11.glRotated(magSpin[0], 1, 0, 0);
		GL11.glTranslated(0, -2.3125, -1.5);

		HbmAnimations.applyRelevantTransformation("Mag");
		
		GL11.glPushMatrix();
		int mag = gun.getConfig(stack, 0).getReceivers(stack)[0].getMagazine(stack).getAmount(stack, MainRegistry.proxy.me().inventory);
		GL11.glTranslated(0, 0, 1.5);
		GL11.glRotated(mag / 59D * 360D, 0, -1, 0);
		GL11.glTranslated(0, 0, -1.5);
		ResourceManager.am180.renderPart("Mag");
		GL11.glPopMatrix();
		
		ResourceManager.am180.renderPart("MagPlate");
		GL11.glPopMatrix();
		
		GL11.glPushMatrix();
		GL11.glTranslated(0, 1.875, silenced ? 17 : 13);
		GL11.glRotated(turn[2], 0, 0, -1);
		GL11.glRotated(90, 0, 1, 0);
		this.renderSmokeNodes(gun.getConfig(stack, 0).smokeNodes, 0.25D);
		GL11.glPopMatrix();
		
		GL11.glShadeModel(GL11.GL_FLAT);

		GL11.glPushMatrix();
		GL11.glTranslated(0, 1.875, silenced ? 16.75 : 12);
		GL11.glRotated(90, 0, 1, 0);
		GL11.glRotated(90 * gun.shotRand, 1, 0, 0);
		double flashScale = silenced ? 0.5 : 0.75;
		GL11.glScaled(flashScale, flashScale, flashScale);
		this.renderMuzzleFlash(gun.lastShot[0], silenced ? 75 : 50, silenced ? 5 : 7.5);
		GL11.glPopMatrix();
	}

	@Override
	public void setupThirdPerson(ItemStack stack) {
		super.setupThirdPerson(stack);
		double scale = 1D;
		GL11.glScaled(scale, scale, scale);
		GL11.glTranslated(0, -0.5, 3);

	}

	@Override
	public void setupInv(ItemStack stack) {
		super.setupInv(stack);
		double scale = 0.75D;
		GL11.glScaled(scale, scale, scale);
		GL11.glRotated(25, 1, 0, 0);
		GL11.glRotated(45, 0, 1, 0);
		GL11.glTranslated(1.5, 0, 0);
	}

	@Override
	public void setupModTable(ItemStack stack) {
		double scale = -5D;
		GL11.glScaled(scale, scale, scale);
		GL11.glRotated(90, 0, 1, 0);
		GL11.glTranslated(0, 0, -2);
	}

	@Override
	public void renderOther(ItemStack stack, ItemRenderType type, Object... data) {
		GL11.glEnable(GL11.GL_LIGHTING);
		
		GL11.glShadeModel(GL11.GL_SMOOTH);
		Minecraft.getMinecraft().renderEngine.bindTexture(ResourceManager.am180_tex);
		ResourceManager.am180.renderPart("Gun");
		if(this.hasSilencer(stack)) ResourceManager.am180.renderPart("Silencer");
		ResourceManager.am180.renderPart("Trigger");
		ResourceManager.am180.renderPart("Bolt");
		ResourceManager.am180.renderPart("Mag");
		ResourceManager.am180.renderPart("MagPlate");
		GL11.glShadeModel(GL11.GL_FLAT);
		
		if(type == ItemRenderType.EQUIPPED) {
			EntityLivingBase ent = (EntityLivingBase) data[1];
			long shot;
			double shotRand = 0;
			if(ent == Minecraft.getMinecraft().thePlayer) {
				ItemGunBaseNT gun = (ItemGunBaseNT) stack.getItem();
				shot = gun.lastShot[0];
				shotRand = gun.shotRand;
			} else {
				shot = ItemRenderWeaponBase.flashMap.getOrDefault(ent, (long) -1);
				if(shot < 0) return;
			}
			boolean silenced = this.hasSilencer(stack);
			
			GL11.glPushMatrix();
			GL11.glTranslated(0, 1.875, silenced ? 16.75 : 12);
			GL11.glRotated(90, 0, 1, 0);
			GL11.glRotated(90 * shotRand, 1, 0, 0);
			double flashScale = silenced ? 0.5 : 0.75;
			GL11.glScaled(flashScale, flashScale, flashScale);
			this.renderMuzzleFlash(shot, silenced ? 75 : 50, silenced ? 5 : 7.5);
			GL11.glPopMatrix();
		}
	}
	
	public boolean hasSilencer(ItemStack stack) {
		return XWeaponModManager.hasUpgrade(stack, 0, XWeaponModManager.ID_SILENCER);
	}
}
