package ru.dbotthepony.mc.otm.android.feature

import net.minecraft.client.gui.GuiGraphics
import net.minecraft.world.effect.MobEffectInstance
import net.minecraft.world.effect.MobEffects
import ru.dbotthepony.mc.otm.android.AndroidSwitchableFeature
import ru.dbotthepony.mc.otm.capability.MatteryPlayerCapability
import ru.dbotthepony.mc.otm.capability.energy.extractEnergyExact
import ru.dbotthepony.mc.otm.client.render.ResearchIcons
import ru.dbotthepony.mc.otm.config.AndroidConfig
import ru.dbotthepony.mc.otm.core.math.RGBAColor
import ru.dbotthepony.mc.otm.registry.AndroidFeatures

class NightVisionFeature(android: MatteryPlayerCapability) : AndroidSwitchableFeature(AndroidFeatures.NIGHT_VISION, android) {
	override val allowToSwitchByPlayerWhileSpectator: Boolean
		get() = true

	override fun tickServer() {
		if (isActive) {
			val effect = android.ply.activeEffectsMap[MobEffects.NIGHT_VISION]

			if ((effect == null || effect.duration < 220) && (ply.isSpectator || android.androidEnergy.extractEnergyExact(AndroidConfig.NIGHT_VISION_POWER_DRAW, false))) {
				android.ply.addEffect(MobEffectInstance(MobEffects.NIGHT_VISION, 220, 0, false, false))
			}
		}
	}

	override fun removeModifiers() {
		val effect = android.ply.activeEffectsMap[MobEffects.NIGHT_VISION]

		if (effect != null && effect.duration <= 222) {
			android.ply.removeEffect(MobEffects.NIGHT_VISION)
		}
	}

	override fun renderIcon(graphics: GuiGraphics, x: Float, y: Float, width: Float, height: Float, color: RGBAColor) {
		ResearchIcons.ICON_NIGHT_VISION.render(graphics, x, y, width, height, color = color)
	}
}
