package ru.dbotthepony.mc.otm.compat.jei

import mezz.jei.api.gui.builder.ITooltipBuilder
import ru.dbotthepony.mc.otm.client.render.MGUIGraphics
import ru.dbotthepony.mc.otm.client.ShiftPressedCond
import ru.dbotthepony.mc.otm.client.screen.widget.MatterGaugePanel
import ru.dbotthepony.mc.otm.client.screen.widget.PowerGaugePanel
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.util.formatMatter
import ru.dbotthepony.mc.otm.core.util.formatPower
import ru.dbotthepony.mc.otm.systemTime

fun renderMatterGauge(
	guiGraphics: MGUIGraphics,
	x: Float,
	y: Float,
	width: Float = MatterGaugePanel.GAUGE_BACKGROUND.width,
	height: Float = MatterGaugePanel.GAUGE_BACKGROUND.height,
	drainSpeed: Float = 1f,
	wavesStrength: Float = drainSpeed * 2f,
) {
	MatterGaugePanel.GAUGE_BACKGROUND.render(guiGraphics, x, y, width, height)
	MatterGaugePanel.renderLevel(guiGraphics, x, y, width, height, 1f - ((systemTime.secondsF * drainSpeed) % 1f), wavesStrength)
}

fun renderEnergyGauge(
	guiGraphics: MGUIGraphics,
	x: Float,
	y: Float,
	width: Float = PowerGaugePanel.GAUGE_BACKGROUND.width,
	height: Float = PowerGaugePanel.GAUGE_BACKGROUND.height,
	drainSpeed: Float = 1f
) {
	val perc = 1f - ((systemTime.secondsF * drainSpeed) % 1f)
	PowerGaugePanel.GAUGE_BACKGROUND.render(guiGraphics, x, y, width, height)
	PowerGaugePanel.GAUGE_FOREGROUND.renderPartial(guiGraphics, x, y + height * (1f - perc) - 1f, width, height * perc, topDown = false)
}

fun matterGaugeTooltips(target: ITooltipBuilder, matter: Decimal, mouseX: Double, mouseY: Double, x: Float, y: Float, width: Float = MatterGaugePanel.GAUGE_BACKGROUND.width, height: Float = MatterGaugePanel.GAUGE_BACKGROUND.height) {
	if (mouseX in x .. (x + width - 1) && mouseY in y .. (y + height - 1)) {
		target.add(TranslatableComponent("otm.gui.matter_panel.matter_required", matter.formatMatter(formatAsReadable = ShiftPressedCond)))
	}
}

fun energyGaugeTooltips(target: ITooltipBuilder, energy: Decimal, mouseX: Double, mouseY: Double, x: Float, y: Float, width: Float = PowerGaugePanel.GAUGE_BACKGROUND.width, height: Float = PowerGaugePanel.GAUGE_BACKGROUND.height) {
	if (mouseX in x .. (x + width - 1) && mouseY in y .. (y + height - 1)) {
		target.add(TranslatableComponent("otm.gui.energy_required", energy.formatPower(formatAsReadable = ShiftPressedCond)))
	}
}
