script.import("script.base")
scriptName = "Modules";
scriptAuthor = "As丶One";
scriptVersion = 1.0;
Keyboard = Java.type("org.lwjgl.input.Keyboard")
Mouse = Java.type('org.lwjgl.input.Mouse')
Fonts = Java.type("net.ccbluex.liquidbounce.ui.font.Fonts");
FontList = Fonts.getFonts();
var TimerList = []
var KeyList = []
script.import("KeystrokesSetting.js")

var Timer = Java.type("java.util.Timer");
function setInterval(func, milliseconds) {
    var timer = new Timer("setInterval", true);
    timer.schedule(function() { 
        func();
    }, milliseconds, milliseconds);

    return timer;
}

function clearInterval(timer) {
    timer.cancel();
}
Gui = Java.type("net.minecraft.client.gui.Gui");
GL11 = Java.type("org.lwjgl.opengl.GL11");
Color = Java.type("java.awt.Color");
ScaledResolution = Java.type("net.minecraft.client.gui.ScaledResolution");
ResourceLocation = Java.type("net.minecraft.util.ResourceLocation");

function drawRect(x1, y1, x2, y2, color) {
	Gui.drawRect(x1, y1, x2, y2, color);
}

function drawBorder(x, y, halfwidth, halfheight, thickness, color) {
    drawRect(x-halfwidth-thickness,y-halfheight-thickness,x+halfwidth+thickness,y-halfheight,color)
    drawRect(x+halfwidth,y-halfheight,x+halfwidth+thickness,y+halfheight,color)
    drawRect(x-halfwidth-thickness,y+halfheight,x+halfwidth+thickness,y+halfheight+thickness,color)
    drawRect(x-halfwidth-thickness,y-halfheight,x-halfwidth,y+halfheight,color)
}

function getScaledWidth() {
    return new ScaledResolution(mc).getScaledWidth();
}

function getScaledHeight() {
    return new ScaledResolution(mc).getScaledHeight();
}

function getMouseCode(name) {
	switch (name) {
		case 'LMB':
			return 0.0;
		case 'RMB':
			return 1.0;
		case 'MMB':
			return 2.0;
	}
}

function rainbowSigma(speed, offset) {//int speed:int offset:int
	var color = (new Date().getTime() + offset) % speed;//float
	color /= speed;
    return Color.HSBtoRGB(color, 0.55, RainbowBrightness.get()/100);
}

function drawKey(key, index) {
	var x = key.x + PosX.get()/Scale.get()
	var y = key.y + PosY.get()/Scale.get()
	var halfwidth = key.lengthX / 2
	var halfheight = key.lengthY / 2
	var text = key.text
	GL11.glPushMatrix();
	GL11.glScaled(Scale.get(), Scale.get(), Scale.get());
	drawRect(x - halfwidth, y - halfheight, x + halfwidth, y + halfheight, bgcolor1) //BackGround1
	TimerList[index] && drawRect(x - halfwidth * TimerList[index] / 20, y - halfheight * TimerList[index] / 20, x + halfwidth * TimerList[index] / 20, y + halfheight * TimerList[index] / 20, bgcolor2) //BackGround2
	BorderValue.get() && drawBorder(x, y, halfwidth, halfheight, BorderStrength.get(), bordercolor) //Border
	font["drawString" + (ShadowText.get() ? "WithShadow" : "")](text, x - font.getStringWidth(text) / 2, y - font.FONT_HEIGHT / 2 - TextY.get(), textcolor) //String
	GL11.glPopMatrix();
}
createModule({
	name: "AsKeystrokes",
	values: [
		FontValue = value.createInteger('', 1, 0, FontList.length - 1),
		Font = value.createText('Font ', Fonts.getFonts()[1].getDefaultFont().getFont().getName()),
		MotionMode = value.createList("MotionMode", ["None", "Mode1", "Mode2"], "Mode1"),
		MotionSpeed = value.createInteger("MotionSpeed", 10, 1, 16),
		Rainbow = value.createBoolean("Rainbow", false),
		RainbowSpeed = value.createInteger("RainbowSpeed", 2000, 1, 4000),
		RainbowBrightness = value.createInteger("RainbowBrightness", 90, 1, 100),
		PosX = value.createInteger("PosX", 50, 0, getScaledWidth()),
		PosY = value.createInteger("PosY", 100, 0, getScaledHeight()),
		Scale = value.createFloat("Scale", 1, 0.8, 3),

		//Color
		BGRed1 = value.createInteger("BGRed1", 36, 0, 255),
		BGGreen1 = value.createInteger("BGGreen1", 36, 0, 255),
		BGBlue1 = value.createInteger("BGBlue1", 36, 0, 255),
		BGAlpha1 = value.createInteger("BGAlpha1", 185, 0, 255),

		BGRed2 = value.createInteger("BGRed2", 255, 0, 255),
		BGGreen2 = value.createInteger("BGGreen2", 255, 0, 255),
		BGBlue2 = value.createInteger("BGBlue2", 255, 0, 255),
		BGAlpha2 = value.createInteger("BGAlpha2", 80, 0, 255),

		TextRed = value.createInteger("TextRed", 210, 0, 255),
		TextGreen = value.createInteger("TextGreen", 210, 0, 255),
		TextBlue = value.createInteger("TextBlue", 210, 0, 255),
		TextY = value.createInteger("TextY", -1, -5, 5),
		ShadowText = value.createBoolean('ShadowText', false),

		BorderValue = value.createBoolean('Border', true),
		BorderRed = value.createInteger("BorderRed", 36, 0, 255),
		BorderGreen = value.createInteger("BorderGreen", 36, 0, 255),
		BorderBlue = value.createInteger("BorderBlue", 36, 0, 255),
		BorderAlpha = value.createInteger("BorderAlpha", 185, 0, 255),
		BorderStrength = value.createInteger("BorderStrength", 1, 0, 8),
	],
	onRender2D: function () {
		KeyList.forEach(function (ele, index) {
			drawKey(ele, index)
		})
		MouseList.forEach(function (ele, index) {
			drawKey(ele, index + KeyList.length)
		})
	},
	onUpdate: function () {
		if(Rainbow.get()){
			var Color3F = rainbowSigma(RainbowSpeed.get(),0)
			textcolor = Color3F
			bordercolor = Color3F
			//bordercolor = new Color(Color3F.getRed(), Color3F.getGreen(), Color3F.getBlue(),BorderAlpha.get()).getRGB()
			
		}else{
			bordercolor = new Color(BorderRed.get(), BorderGreen.get(), BorderBlue.get(),BorderAlpha.get()).getRGB()
			textcolor = new Color(TextRed.get(), TextGreen.get(), TextBlue.get()).getRGB()
		}
		
		bgcolor1 = new Color(BGRed1.get(), BGGreen1.get(), BGBlue1.get(), BGAlpha1.get()).getRGB()
		bgcolor2 = new Color(BGRed2.get(), BGGreen2.get(), BGBlue2.get(), BGAlpha2.get()).getRGB()
		if (ii != FontValue.get()) {
			ii = FontValue.get();
			font = Fonts.getFonts()[ii];
			Font.set(font == mc.fontRendererObj ? 'Minecraft' : font.getDefaultFont().getFont().getName());
		}
	},
	onEnable: function () {
		TimerList = []
		for (var i in KeyList) {
			TimerList.push(0)
		}
		for (var i in MouseList) {
			TimerList.push(0)
		}
		Interval = setInterval(function () {
			KeyList.forEach(function (ele, index) {
				if (Keyboard.isKeyDown(Keyboard["KEY_" + ele.name])) {
					MotionMode.get() == "None" ? TimerList[index] = 20 : TimerList[index] < 20 ? TimerList[index] += 20 / (20 - MotionSpeed.get()) : TimerList[index] = 20
				} else {
					TimerList[index] = 0
				}
			});
			MouseList.forEach(function (ele, index) {
				if (Mouse.isButtonDown(getMouseCode(ele.name))) {
					MotionMode.get() == "None" ? TimerList[index + KeyList.length] = 20 : TimerList[index + KeyList.length] < 20 ? TimerList[index + KeyList.length] += 20 / (20 - MotionSpeed.get()) : TimerList[index + KeyList.length] = 20
				} else {
					TimerList[index + KeyList.length] = 0
				}
			});
		}, 25)
		ii = FontValue.get();
		font = FontList[ii];
		Font.set(font == mc.fontRendererObj ? 'Minecraft' : font.getDefaultFont().getFont().getName());
	},
	onDisable: function () {
		clearInterval(Interval)
	}
})

createCommand({
	commands: ["ReloadKeystrokes"],
	onExecute: function (args) {
		script.import("KeystrokesSetting.js")
		chat.print("§8[§9AsKeystrokes§8] §a§lReloaded Successfully")
	}
})