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

import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.ai.attributes.AttributeModifier
import net.minecraft.world.entity.ai.attributes.Attributes
import net.minecraft.world.entity.player.Player
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.android.AndroidFeature
import ru.dbotthepony.mc.otm.capability.MatteryPlayer
import ru.dbotthepony.mc.otm.capability.matteryPlayer
import ru.dbotthepony.mc.otm.core.ResourceLocation
import ru.dbotthepony.mc.otm.registry.game.AndroidFeatures

class LimbOverclockingFeature(android: MatteryPlayer) : AndroidFeature(AndroidFeatures.LIMB_OVERCLOCKING, android) {
	override fun applyModifiers() {
		val speed = ply.getAttribute(Attributes.MOVEMENT_SPEED)

		if (speed != null) {
			speed.removeModifier(MODIFIER_ID)
			speed.addPermanentModifier(AttributeModifier(MODIFIER_ID,(level + 1) * 0.08, AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL))
		}

		val attackSpeed = ply.getAttribute(Attributes.ATTACK_SPEED)

		if (attackSpeed != null) {
			attackSpeed.removeModifier(MODIFIER_ID)
			attackSpeed.addPermanentModifier(AttributeModifier(MODIFIER_ID, (level + 1) * 0.06, AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL))
		}
	}

	override fun removeModifiers() {
		ply.getAttribute(Attributes.MOVEMENT_SPEED)?.removeModifier(MODIFIER_ID)
		ply.getAttribute(Attributes.ATTACK_SPEED)?.removeModifier(MODIFIER_ID)
	}

	companion object {
		private val MODIFIER_ID = ResourceLocation(OverdriveThatMatters.MOD_ID, "limb_overclocking")

		@JvmStatic
		fun getBrushCooldown(entity: LivingEntity): Int {
			if (entity !is Player) return 10
			val matteryPlayer = entity.matteryPlayer ?: return 10
			if (!matteryPlayer.isAndroid || !matteryPlayer.hasFeature(AndroidFeatures.LIMB_OVERCLOCKING)) return 10

			val level = matteryPlayer.getFeature(AndroidFeatures.LIMB_OVERCLOCKING)!!.level + 1
			return (10 - level * 2).coerceAtLeast(2)
		}

		@JvmStatic
		fun getBrushTick(entity: LivingEntity): Int {
			if (entity !is Player) return 5
			val matteryPlayer = entity.matteryPlayer ?: return 5
			if (!matteryPlayer.isAndroid || !matteryPlayer.hasFeature(AndroidFeatures.LIMB_OVERCLOCKING)) return 5

			val level = matteryPlayer.getFeature(AndroidFeatures.LIMB_OVERCLOCKING)!!.level + 1
			return (5 - level).coerceAtLeast(1)
		}

		@JvmStatic
		fun getBrushableBlockCooldown(player: Player): Long {
			val matteryPlayer = player.matteryPlayer ?: return 10L
			if (!matteryPlayer.isAndroid || !matteryPlayer.hasFeature(AndroidFeatures.LIMB_OVERCLOCKING)) return 10L

			val level = matteryPlayer.getFeature(AndroidFeatures.LIMB_OVERCLOCKING)!!.level + 1
			return (10L - level * 2L).coerceAtLeast(2L)
		}
	}
}
