package ru.dbotthepony.mc.otm.entity

import net.minecraft.world.entity.AnimationState
import net.minecraft.world.entity.EntityType
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.ai.attributes.AttributeSupplier
import net.minecraft.world.entity.ai.attributes.Attributes
import net.minecraft.world.entity.ai.goal.LeapAtTargetGoal
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal
import net.minecraft.world.entity.ai.goal.MeleeAttackGoal
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal
import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal
import net.minecraft.world.entity.ai.navigation.GroundPathNavigation
import net.minecraft.world.entity.ai.navigation.PathNavigation
import net.minecraft.world.entity.monster.AbstractIllager
import net.minecraft.world.entity.monster.AbstractSkeleton
import net.minecraft.world.entity.monster.Monster
import net.minecraft.world.entity.monster.Zombie
import net.minecraft.world.entity.npc.Villager
import net.minecraft.world.entity.player.Player
import net.minecraft.world.level.Level
import ru.dbotthepony.mc.otm.registry.game.MEntityTypes

class BreadMonster(type: EntityType<BreadMonster>, level: Level) : Monster(type, level) {
	constructor(level: Level) : this(MEntityTypes.BREAD_MONSTER, level)

	val idleState = AnimationState()

	init {
	    idleState.start(tickCount)
	}

	override fun registerGoals() {
		goalSelector.addGoal(8, RandomLookAroundGoal(this))
		goalSelector.addGoal(7, WaterAvoidingRandomStrollGoal(this, 0.8))
		goalSelector.addGoal(8, LookAtPlayerGoal(this, Player::class.java, 8f))
		goalSelector.addGoal(3, NearestAttackableTargetGoal(this, Player::class.java , true, true))
		goalSelector.addGoal(3, NearestAttackableTargetGoal(this, LivingEntity::class.java, 10, true, true) { entity ->
			entity !is BreadMonster
		})

		goalSelector.addGoal(1, LeapAtTargetGoal(this, 0.4f))
		goalSelector.addGoal(2, MeleeAttackGoal(this, 1.3, true))

		targetSelector.addGoal(1, HurtByTargetGoal(this))
	}

	override fun createNavigation(level: Level): PathNavigation = GroundPathNavigation(this, level)

	companion object {
		fun createAttributes() : AttributeSupplier.Builder {
			return createMonsterAttributes()
				.add(Attributes.MAX_HEALTH, 16.0)
				.add(Attributes.MOVEMENT_SPEED, 0.3)
				.add(Attributes.ATTACK_DAMAGE, 3.0)
		}
	}
}
