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

import net.minecraft.server.level.ServerPlayer
import net.minecraft.sounds.SoundSource
import net.neoforged.neoforge.network.PacketDistributor
import ru.dbotthepony.kommons.math.RGBAColor
import ru.dbotthepony.kommons.util.getValue
import ru.dbotthepony.kommons.util.setValue
import ru.dbotthepony.mc.otm.android.AndroidSwitchableFeature
import ru.dbotthepony.mc.otm.capability.MatteryPlayer
import ru.dbotthepony.mc.otm.capability.energy.extractEnergyExact
import ru.dbotthepony.mc.otm.client.render.MGUIGraphics
import ru.dbotthepony.mc.otm.client.render.ResearchIcons
import ru.dbotthepony.mc.otm.config.AndroidConfig
import ru.dbotthepony.mc.otm.config.ClientConfig
import ru.dbotthepony.mc.otm.core.math.Vector
import ru.dbotthepony.mc.otm.core.math.plus
import ru.dbotthepony.mc.otm.network.SmokeParticlesPacket
import ru.dbotthepony.mc.otm.network.TriggerJumpBoostPacket
import ru.dbotthepony.mc.otm.registry.game.AndroidFeatures
import ru.dbotthepony.mc.otm.registry.game.MSoundEvents

class JumpBoostFeature(capability: MatteryPlayer) : AndroidSwitchableFeature(AndroidFeatures.JUMP_BOOST, capability) {
	private var tickCooldownClient = false

	override val maxCooldown: Int
		get() = (AndroidConfig.JumpBoost.BASE_COOLDOWN - AndroidConfig.JumpBoost.COOLDOWN_REDUCTION * level).coerceAtLeast(0)

	override var cooldown by syncher.int(setter = setter@{ access, value ->
		access.accept(value)

		if (ply !is ServerPlayer) {
			tickCooldownClient = false
		}
	})

	private var lastGround = false

	fun movementTick(isJumping: Boolean, isShifting: Boolean) {
		check(ply !is ServerPlayer) { "Invalid side" }

		if (ply.isSpectator) {
			return
		}

		val old = lastGround
		lastGround = ply.onGround()

		if (isActive && cooldown <= 0 && old != lastGround && !lastGround && isJumping && isShifting && ply.xRot <= ClientConfig.JUMP_BOOST_LOOK_ANGLE && android.androidEnergy.extractEnergyExact(AndroidConfig.JumpBoost.ENERGY_COST, true)) {
			ply.deltaMovement += Vector(0.0, AndroidConfig.JumpBoost.POWER * (level + 1) / 20.0, 0.0)
			putOnCooldown()
			PacketDistributor.sendToServer(TriggerJumpBoostPacket)

			ply.level().playSound(
				ply, ply,
				MSoundEvents.ANDROID_JUMP_BOOST, SoundSource.PLAYERS,
				1f, 1f
			)

			SmokeParticlesPacket.makeSmoke(ply.x, ply.y, ply.z, ply.random, ply.level())
		}
	}

	override fun tickClient() {
		if (tickCooldownClient && cooldown > 0) {
			cooldown--
		}
	}

	override fun renderIcon(graphics: MGUIGraphics, x: Float, y: Float, width: Float, height: Float, color: RGBAColor) {
		ResearchIcons.ICON_JUMP_BOOST.render(graphics, x, y, width, height, color = if (cooldown > 0) color * RGBAColor.REDDISH else color)
	}
}
