var script = registerScript({
    name: "PointerESP",
    version: "2.0",
    authors: ["AquaVit","liulihaocai"]
});

var GlStateManager = Java.type("net.minecraft.client.renderer.GlStateManager")
var ScaledResolution = Java.type("net.minecraft.client.gui.ScaledResolution")
var GL11 = Java.type("org.lwjgl.opengl.GL11")
var MathHelper = Java.type("net.minecraft.util.MathHelper")
var Color = Java.type('java.awt.Color')
var ColorUtils = Java.type('net.ccbluex.liquidbounce.utils.render.ColorUtils')
var EntityUtils = Java.type('net.ccbluex.liquidbounce.utils.EntityUtils')
var Integer = Java.type("java.lang.Integer")

Math.toRadians = function (degrees) {
    return degrees * Math.PI / 180;
};
script.registerModule({
    name: "PointerESP",
    description: "",
    category: "Render",
    settings: {
		mode: Setting.list({
            name: "DrawMode",
            values: ["Solid", "Line"],
            default: "Line"
        }),
		col: Setting.list({
            name: "ColorMode",
            values: ["Custom","Rainbow","Distance","Health"],
            default: "Custom"
        }),
        r: Setting.integer({
            name: "Red",
            default: 5,
            min: 0,
            max: 255
        }),
		g: Setting.integer({
            name: "Green",
            default: 5,
            min: 0,
            max: 255
        }),
		b: Setting.integer({
            name: "Blue",
            default: 5,
            min: 0,
            max: 255
        }),
		di: Setting.integer({
            name: "DistanceChange",
            default: 2,
            min: 0,
            max: 5
        })
    }
}, function (module) {
    var xOffset,yOffset,playerOffsetX,playerOffsetZ,targets=[];
    module.on("update",function(){
        var displayWidth=mc.displayWidth,displayHeight=mc.displayHeight,scaleFactor=1;
        var guiScale = mc.gameSettings.guiScale;
        while (scaleFactor < guiScale && displayWidth / (scaleFactor + 1) >= 320 && displayHeight / (scaleFactor + 1) >= 240){
            ++scaleFactor;
        }
        if (mc.isUnicode() && scaleFactor % 2 != 0 && scaleFactor != 1){
            --scaleFactor;
        }
        xOffset = new Integer((displayWidth/scaleFactor) / 2 - 24.5);
        yOffset = new Integer((displayHeight/scaleFactor) / 2 - 25.2);
        playerOffsetX = mc.thePlayer.posX;
        playerOffsetZ = mc.thePlayer.posZ;
        targets=[];
        for(var i in mc.theWorld.loadedEntityList){
            var entity = mc.theWorld.loadedEntityList[i]
            if(EntityUtils.isSelected(entity,true)){
                targets.push(entity);
            }
        }
    })
    module.on("render2D", function (event) {
		GlStateManager.pushMatrix();
        for (var i in targets){
            var entity=targets[i];
            var loaddist = 0.2;
            var pTicks = mc.timer.renderPartialTicks;
            var pos1 = (((entity.posX + (entity.posX - entity.lastTickPosX) * pTicks) - playerOffsetX) * loaddist);
            var pos2 = (((entity.posZ + (entity.posZ - entity.lastTickPosZ) * pTicks) - playerOffsetZ) * loaddist);
            var cos = Math.cos(mc.thePlayer.rotationYaw * (Math.PI * 2 / 360));
            var sin = Math.sin(mc.thePlayer.rotationYaw * (Math.PI * 2 / 360));
            var rotY = -(pos2 * cos - pos1 * sin);
            var rotX = -(pos1 * cos + pos2 * sin);
            var var7 = 0 - rotX;
            var var9 = 0 - rotY;
            if (MathHelper.sqrt_double(var7 * var7 + var9 * var9) < 25 - 4) {
                var angle = (Math.atan2(rotY - 0, rotX - 0) * 180 / Math.PI);
                var x = (25 * Math.cos(Math.toRadians(angle))) + xOffset + 25;
                var y = (25 * Math.sin(Math.toRadians(angle))) + yOffset + 25;
                GlStateManager.pushMatrix();
                GlStateManager.translate(x, y, 0);
                GlStateManager.rotate(angle, 0, 0, 1);
                GlStateManager.scale(1.5, 1.0, 1.0);
                var colorNow;
                switch(module.settings.col.get().toLowerCase()){
                    case "custom":{
                        colorNow=new Color(module.settings.r.get(),module.settings.g.get(),module.settings.b.get(),255).getRGB();
                        break
                    }
                    case "rainbow":{
                        colorNow=ColorUtils.rainbow().getRGB();
                        break
                    }
                    case "distance":{
                        var dist = mc.thePlayer.getDistanceToEntity(entity)*module.settings.di.get();
                        colorNow=new Color(new Integer(argCheck(255 - dist,0,255)), new Integer(argCheck(dist,0,255)), 0, 255).getRGB();
                        break
                    }
                    case "health":{
                        var colorHP=(entity.getHealth()/entity.getMaxHealth())*255;
                        colorNow=new Color(new Integer(argCheck(255 - colorHP,0,255)), new Integer(argCheck(colorHP,0,255)),0, 255).getRGB()
                        break
                    }
                }
                switch (module.settings.mode.get().toLowerCase()){
                    case "solid":{
                        drawESPCircle(0, 0, 2.2, 3, colorNow);
                        drawESPCircle(0, 0, 1.5, 3, colorNow);
                        drawESPCircle(0, 0, 1.0, 3, colorNow);
                        drawESPCircle(0, 0, 0.5, 3, colorNow);
                        break;
                    }
                    case "line":{
                        drawESPCircle(0, 0, 2.2, 3, colorNow);
                        break;
                    }
                }
                GlStateManager.popMatrix();
            }
		}
		GlStateManager.popMatrix();
    });
});
function glColor(shit) {
    var alpha = (shit >> 24 & 0xFF) / 255;
    var red = (shit >> 16 & 0xFF) / 255;
    var green = (shit >> 8 & 0xFF) / 255;
    var blue = (shit & 0xFF) / 255;
    GlStateManager.color(red, green, blue, alpha);
}

function enableGL2D() {
    GL11.glDisable(2929);
    GL11.glEnable(3042);
    GL11.glDisable(3553);
    GL11.glBlendFunc(770, 771);
    GL11.glDepthMask(true);
    GL11.glEnable(2848);
    GL11.glHint(3154, 4354);
    GL11.glHint(3155, 4354);
}

function disableGL2D() {
    GL11.glEnable(3553);
    GL11.glDisable(3042);
    GL11.glEnable(2929);
    GL11.glDisable(2848);
    GL11.glHint(3154, 4352);
    GL11.glHint(3155, 4352);
}

function drawESPCircle(cx, cy, r, n, color) {
    GL11.glPushMatrix();
    cx *= 2.0;
    cy *= 2.0;
    var b = 6.2831852 / n;
    var p = Math.cos(b);
    var s = Math.sin(b);
    var x = r *= 2.0;
    var y = 0.0;
    enableGL2D();
    GL11.glScalef(0.5, 0.5, 0.5);
    GlStateManager.color(0,0,0);
    GlStateManager.resetColor();
    glColor(color);
    GL11.glBegin(2);
    var ii = 0;
    while (ii < n) {
        GL11.glVertex2f(x + cx, y + cy);
        var t = x;
        x = p * x - s * y;
        y = s * t + p * y;
        ii++;
    }
    GL11.glEnd();
    GL11.glScalef(2.0, 2.0, 2.0);
    disableGL2D();
    GlStateManager.color(1, 1, 1, 1);
    GL11.glPopMatrix();
}
function argCheck(num,min,max){
    if(num>max){
        return max;
    }
    if(num<min){
        return min;
    }
    return num;
}