import { MolangVariableMap } from '@minecraft/server';
import { Vector3 } from "./vector3";
import * as utils from "./utils"

export let hurtParticlesType = 1; // 0-6, -1 to disable

const HURT_PARTICLES = {
	"goe_bos:spider_queen": [ "goe_bos:hit_purple_star" ],
	"goe_bos:lair_spider": [ "goe_bos:hit_purple_star" ],
	"goe_bos:spider_minion": [ "goe_bos:hit_green_star" ],

	"goe_bos:hoglin_warrior": [ "goe_bos:hit_orange_star", "goe_bos:hit_black_star" ],
	"goe_bos:piglin_swordsman": [ "goe_bos:hit_orange_star", "goe_bos:hit_black_star" ],
	"goe_bos:piglin_crossbowman": [ "goe_bos:hit_orange_star", "goe_bos:hit_black_star" ],
	"goe_bos:piglin_spearman": [ "goe_bos:hit_orange_star", "goe_bos:hit_black_star" ],

	"goe_bos:skeleton_king": [ "goe_bos:hit_gray_star", "goe_bos:hit_dark_blue_star" ],
	"goe_bos:skeleton_warrior": [ "goe_bos:hit_gray_star", "goe_bos:hit_blue_star" ],
	"goe_bos:skeleton_archer": [ "goe_bos:hit_gray_star", "goe_bos:hit_light_purple_star" ],
	"goe_bos:skeleton_wolf": [ "goe_bos:hit_gray_star", "goe_bos:hit_light_green_star" ],

	"goe_bos:necromancer": [ "goe_bos:hit_black_star", "goe_bos:hit_light_green_star" ],
	"goe_bos:skelly": [ "goe_bos:hit_navy_star", "goe_bos:hit_gray_star" ],

	"goe_bos:warden_king": [ "goe_bos:hit_blue_star", "goe_bos:hit_navy_star" ],
	"goe_bos:warden_wraith": [ "goe_bos:hit_blue_star", "goe_bos:hit_navy_star" ],

	"goe_bos:winter_warlock": [ "goe_bos:hit_gray_star", "goe_bos:hit_blue_star" ],
	"goe_bos:ice_golem": [ "goe_bos:hit_gray_star", "goe_bos:hit_blue_star" ],

	"goe_bos:wither_wizard": [ "goe_bos:hit_blue_star", "goe_bos:hit_navy_star" ],
	"goe_bos:wither_knight": [ "goe_bos:hit_black_star", "goe_bos:hit_blue_star" ],

	"goe_bos:void_abomination": [ "goe_bos:hit_black_star", "goe_bos:hit_purple_star" ],
	"goe_bos:chorus_shroom": [ "goe_bos:hit_black_star", "goe_bos:hit_purple_star" ],
	"goe_bos:void_golem": [ "goe_bos:hit_black_star", "goe_bos:hit_purple_star" ]
}

export async function onEntityHurt(event) {
	const hurtEntity = event.hurtEntity;
	const damageDealer = event.damageSource.damagingEntity;
	const projectile = event.damageSource.damagingProjectile;

	if (!HURT_PARTICLES[hurtEntity.typeId]) return;
	
	let damage = event.damage;
	
    // Show damage numbers
	const particleVariables = new MolangVariableMap();
	particleVariables.setFloat("damage", Math.min(Math.max(Math.round(damage), 0), 99));
	let direction = new Vector3(0,0,0);
	direction = new Vector3(damageDealer.getViewDirection());

	particleVariables.setVector3("direction", direction.multiply(-0.5));
	particleVariables.setFloat("offset_x", Math.random(-1,1) * 0.05);
	particleVariables.setFloat("offset_z", Math.random(-1,1) * 0.05);
	event.hurtEntity.dimension.spawnParticle("goe_bos:damage_numbers", event.hurtEntity.location, particleVariables);
	
    // Show hit particles
	hurtParticlesType = Math.floor(Math.random() * HURT_PARTICLES.length);

	HURT_PARTICLES[hurtEntity.typeId].forEach(particle => {
		event.hurtEntity.dimension.spawnParticle(particle, event.hurtEntity.location);
	});
}