package ru.dbotthepony.mc.otm.item.weapon

import net.minecraft.sounds.SoundSource
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack
import ru.dbotthepony.mc.otm.capability.energy.extractEnergyExact
import ru.dbotthepony.mc.otm.core.math.Angle
import ru.dbotthepony.mc.otm.core.math.Vector
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.entity.PlasmaProjectile
import ru.dbotthepony.mc.otm.core.position
import ru.dbotthepony.mc.otm.registry.MSoundEvents

class PlasmaRifleItem : PlasmaWeaponItem<PlasmaWeaponDataTable>(PlasmaWeaponDataTable::class, Decimal(200_000)) {
	override val roundsPerMinute: Int = 200
	override val scopingTime: Int = 7

	override val positionIdle: Vector
		get() = Vector(0.2, -0.4, -0.6)
	override val positionIronSights: Vector
		get() = Vector(0.0, -0.24, -1.0)
	override val rotIdle: Angle
		get() = Angle(0.0, -0.02, 0.0)
	override val rotIronSights: Angle
		get() = Angle(-0.02, 0.0, 0.0)

	override fun primaryFire(itemStack: ItemStack, player: Player, dt: PlasmaWeaponDataTable): Boolean {
		if (!player.level().isClientSide) {
			val arrow = PlasmaProjectile(player.level())
			arrow.position = player.eyePosition

			val calc = VelocityCalculation(player, force = 4.0, deviation = 0.3)
			calc.load(arrow)

			arrow.owner = player

			player.level().addFreshEntity(arrow)
		} else {
			dt.doFireAnim(deviation = rotFireAnimDeviation)
		}

		receiveHeat(itemStack, player, 10.0, dt)

		player.level().playSound(
			player,
			player,
			MSoundEvents.RIFLE_SHOT,
			SoundSource.PLAYERS,
			1f,
			1f
		)

		if (!player.abilities.instabuild)
			energyData(itemStack).extractEnergy(ENERGY_PER_SHOT, false)

		return true
	}

	override fun canPrimaryFire(itemStack: ItemStack, player: Player, dt: PlasmaWeaponDataTable): Boolean {
		return super.canPrimaryFire(itemStack, player, dt) && (player.abilities.instabuild || energyData(itemStack).extractEnergyExact(ENERGY_PER_SHOT, true))
	}

	companion object {
		private val ENERGY_PER_SHOT = Decimal(3_000)
	}
}
