import { world, system } from "@minecraft/server";
import * as defaultWorldArrays from "../arrays/default_world_arrays.js"
import * as dragonArrays from "../arrays/dragon_arrays.js"
import * as dragonData from "../data/dragon_data.js"
import * as dragonUtilities from "../utilities/dragon_utilities.js"

system.runInterval(() => {
	for (const dim of defaultWorldArrays.addonDimensions) {
		const entities = world.getDimension(dim).getEntities(dragonUtilities.dragonTypes);
		for (const dragon of entities) {
			dragonUtilities.dragonsMainComponents(dragon);
		}
	}
},0);
const fireDragonNamesArray = [
	"Drogon",
	"Viserion",
	"Rhaegal",
	"Balerion",
	"Vhagar",
	"Syrax",
	"Caraxes",
	"Vermithor",
	"Arrax",
	"Seasmoke",
	"Silverwing",
	"Terrax",
	"Sunfyre",
	"Dreamfyre",
	"Meraxes",
	"Quicksilver",
	"Meleys",
	"Tessarion",
	"Moondancer",
	"Stormcloud",
	"Sheepstealer",
	"Cannibal",
	"Mentulla",
	"Vagginnae"
];
world.afterEvents.entityLoad.subscribe(({entity:dragon})=>{
	if (dragon.typeId=="megaverse:fire_dragon"&&(!dragon.nameTag||dragon.nameTag=="Unknown"||dragon.nameTag==""||dragon.nameTag==" ")) {
		const fireDragonNames = fireDragonNamesArray[Math.floor(Math.random()*fireDragonNamesArray.length)];
		dragon.nameTag = fireDragonNames;
	}
});
world.afterEvents.entitySpawn.subscribe(({entity:dragon})=>{
	if (dragon.typeId=="megaverse:fire_dragon"&&(!dragon.nameTag||dragon.nameTag=="Unknown"||dragon.nameTag==""||dragon.nameTag==" ")) {
		const fireDragonNames = fireDragonNamesArray[Math.floor(Math.random()*fireDragonNamesArray.length)];
		dragon.nameTag = fireDragonNames;
	}
});
world.afterEvents.dataDrivenEntityTrigger.subscribe(({entity,eventId}) => {
	const dragonOnList = dragonArrays.dragonEggLayingTypesList.includes(entity.typeId);
	if (!dragonOnList) return;
	dragonUtilities.dragonEggLaying(entity,eventId);
});
world.afterEvents.dataDrivenEntityTrigger.subscribe(({entity:dragonProjectile,eventId}) => {
	const projectiles = dragonArrays.dragonProjectilesIdentifierTypes.includes(dragonProjectile.typeId);
	const data = dragonData.dragonProjectileDataTypes[dragonProjectile.typeId];
	if (!dragonProjectile.isValid) return;
	if (!projectiles) return;
	if (!data) return;
    if (data.projectile_type=="firebreathing") {
    	if (eventId==data.projectile_event_trigger) {
			dragonUtilities.dragonfireBurningArea(dragonProjectile,dragonProjectile.dimension,data.projectile_area_size);
			dragonProjectile.dimension.spawnParticle("megaverse:fire_dragon_firebreath",dragonProjectile.location);
		}
    }
    else return;
});
world.afterEvents.entityHurt.subscribe(({damage,damageSource,hurtEntity:dragon})=>{
	const dragonOnList = dragonArrays.dragonTypesList.includes(dragon.typeId);
	if (!dragonOnList) return;
	if (!damageSource.damagingProjectile) return;
	if (damageSource.damagingProjectile.typeId!="megaverse:ballista_bolt") return;
	dragon.triggerEvent("minecraft:fire_dragon_critical_hit");
});
world.beforeEvents.playerInteractWithEntity.subscribe((event) => {
	const {player,target} = event;
    if (target.typeId!="megaverse:fire_dragon") return;
	const age = target.getProperty("megaverse:age_variant");
	if (age!="baby") return; 
	system.runTimeout(() => { 
		const typeFamily = target.getComponent("minecraft:type_family");
		const required = ["parrot_tame"];
		if (required.some(family=>typeFamily.hasTypeFamily(family))) {
			target.teleport(player.location);
		}
	},0);
});
world.afterEvents.dataDrivenEntityTrigger.subscribe(({entity,eventId}) => {
	if (entity.typeId!="megaverse:fire_dragon") return;
	if (eventId=="minecraft:on_not_riding_player") {
		entity.teleport(entity.location);
	}
});
