import { world, system, ItemComponentTypes } from "@minecraft/server";
import * as utils from "./utils";
import * as shields from "./shields";
import * as tnt_shield from "./tnt_shield";
import * as dragon_shield from "./dragon_shield";
import * as warden_shield from "./warden_shield";
import * as ice_shield from "./ice_shield";
import * as skeleton_shield from "./skeleton_shield";
import * as enderman_shield from "./enderman_shield";
import * as spore_blossom_shield from "./spore_blossom_shield";
import * as magma_shield from "./magma_shield";
import * as book from "./book";

export async function onLoadFirstTime()
{
	// If needed - call init functions
	await system.runTimeout(() => { world.sendMessage({ translate: 'welcome.text' })}, 80);
	await system.runTimeout(() => { world.sendMessage({ translate: 'welcome.text2' })}, 81);
	await system.runTimeout(() => { utils.runCommand("playsound note.chime @a") }, 80);
	await system.runTimeout(() => { utils.runPlayer0Command("structure load \"pu_se/pu_se_book\" ~ ~ ~") }, 90);
	await system.runTimeout(() => { utils.runPlayer0Command("structure load \"pu_se/pu_se_book\" ~ ~ ~10") }, 90);
	await system.runTimeout(() => { utils.runPlayer0Command("tp @e[rm=7,r=20,type=item,name=\"Adventure Book\"] ~ ~-250 ~") }, 95);
	await system.runTimeout(() => { utils.runPlayer0Command("tp @e[rm=7,r=20,type=item,name=\"Livro de Aventuras\"] ~ ~-250 ~") }, 95);
	await system.runTimeout(() => { utils.runPlayer0Command("tp @e[rm=7,r=20,type=item,name=\"Livre d'Aventure\"] ~ ~-250 ~") }, 95);
	await system.runTimeout(() => { utils.runPlayer0Command("tp @e[rm=7,r=20,type=item,name=\"Libro de Aventuras\"] ~ ~-250 ~") }, 95);
	await system.runTimeout(() => { utils.runPlayer0Command("tp @e[rm=7,r=20,type=item,name=\"Abenteuerbuch\"] ~ ~-250 ~") }, 95);
}

export async function onLoad(){
}

let nextTick = -100;
let foundLanguage = false;
let french = false;
let english = false;
let portugese = false;
let spanish = false;
let german = false;
export async function onTick(){

	await shields.onTick();
	await dragon_shield.onTick();
	if (english){
		await book.giveEnglishBook();
	}
	if (french){
		await book.giveFrenchBook();
	}
	if (spanish){
		await book.giveSpanishBook();
	}
	if (portugese){
		await book.givePortugeseBook();
	}
	if (german){
		await book.giveGermanBook();
	}

    // if(system.currentTick >= nextTick)
	// {
	// 	nextTick = system.currentTick + 20;
	// 	await fart_tnt.onTick();
	// }
	if (foundLanguage==false){
		let entities = utils.player0.dimension.getEntities({ location: utils.player0.location,maxDistance: 15, type: "minecraft:item"});
		for (let entity of entities){
			const item = entity.getComponent("minecraft:item").itemStack
			if (item.typeId=="pu_se:book"){
				utils.runEntityCommand(entity, "execute if entity @s[name=\"Adventure Book\"] run tag @p add pu_se_english");
				utils.runEntityCommand(entity, "execute if entity @s[name=\"Livro de Aventuras\"] run tag @p add pu_se_portugese");
				utils.runEntityCommand(entity, "execute if entity @s[name=\"Livre d'Aventure\"] run tag @p add pu_se_french");
				utils.runEntityCommand(entity, "execute if entity @s[name=\"Libro de Aventuras\"] run tag @p add pu_se_spanish");
				utils.runEntityCommand(entity, "execute if entity @s[name=\"Abenteuerbuch\"] run tag @p add pu_se_german");
				foundLanguage = true;
			}
		}
	}
	if (english == false && french == false && spanish == false && portugese == false && german == false){
		if (utils.player0.hasTag("pu_se_english"))
			english = true;
		if (utils.player0.hasTag("pu_se_french"))
			french = true;
		if (utils.player0.hasTag("pu_se_spanish"))
			spanish = true;
		if (utils.player0.hasTag("pu_se_portugese"))
			portugese = true;
		if (utils.player0.hasTag("pu_se_german"))
			german = true;
	}
}
export async function onPlayerJoin(event)
{
	//event.playerId event.playerName
}

export async function onEntitySpawn(event)
{
	if(event.entity.typeId == "pu_se:tnt_projectile" || event.entity.typeId == "pu_se:dragon_projectile" || event.entity.typeId == "pu_se:ice_projectile" || event.entity.typeId == "pu_se:warden_projectile" ||  event.entity.typeId == "pu_se:magma_projectile")
	{
		let projectileComponent = event.entity.getComponent('minecraft:projectile');
		projectileComponent.gravity=-0.03;
		const entities = event.entity.dimension.getEntities({ location: event.entity.location,maxDistance: 3});
		for(const entity of entities){
			if(entity.typeId == "pu_se:tnt_shooter_utility"){
				projectileComponent.owner=entity
				let velocity = {x: entity.getViewDirection().x*2, y: entity.getViewDirection().y, z: entity.getViewDirection().z*2};
				projectileComponent.shoot(velocity);
			}
		}
	}
}

export async function onEntityHurt(event)
{
	shields.onEntityHurt(event);

}

export async function onEntityHurtEntity(event)
{
	shields.onEntityHurtEntity(event);
}

export async function onProjectileHitEntity(event)
{
	if (event.projectile.typeId === "pu_se:warden_projectile")
		warden_shield.onProjectileHitEntity(event);
	if (event.projectile.typeId === "pu_se:magma_projectile")
		magma_shield.onProjectileHitEntity(event);
	if (event.projectile.typeId === "pu_se:ice_projectile")
		ice_shield.onProjectileHitEntity(event);
}

export async function onEntityDie(event)
{

}


export async function onItemUse(event)
{
	if (event.itemStack.typeId === "pu_se:tnt_shield" && event.source.isSneaking){
		const cooldown = event.itemStack.getComponent("minecraft:cooldown");
		const remainingCooldown = cooldown.getCooldownTicksRemaining(event.source);
		if (remainingCooldown === 0){
			tnt_shield.ItemUse(event);
			cooldown.startCooldown(event.source)
		}
	}
	if (event.itemStack.typeId === "pu_se:dragon_shield" && event.source.isSneaking){
		const cooldown = event.itemStack.getComponent("minecraft:cooldown");
		const remainingCooldown = cooldown.getCooldownTicksRemaining(event.source);
		if (remainingCooldown === 0){
			dragon_shield.ItemUse(event);
			cooldown.startCooldown(event.source)
		}
	}
	if (event.itemStack.typeId === "pu_se:warden_shield" && event.source.isSneaking){
		const cooldown = event.itemStack.getComponent("minecraft:cooldown");
		const remainingCooldown = cooldown.getCooldownTicksRemaining(event.source);
		if (remainingCooldown === 0){
			warden_shield.ItemUse(event);
			cooldown.startCooldown(event.source)
		}
	}
	if (event.itemStack.typeId === "pu_se:ice_shield" && event.source.isSneaking){
		const cooldown = event.itemStack.getComponent("minecraft:cooldown");
		const remainingCooldown = cooldown.getCooldownTicksRemaining(event.source);
		if (remainingCooldown === 0){
			ice_shield.ItemUse(event);
			cooldown.startCooldown(event.source)
		}
	}
	if (event.itemStack.typeId === "pu_se:skeleton_shield" && event.source.isSneaking){
		const cooldown = event.itemStack.getComponent("minecraft:cooldown");
		const remainingCooldown = cooldown.getCooldownTicksRemaining(event.source);
		if (remainingCooldown === 0){
			skeleton_shield.ItemUse(event);
		}
	}
	if (event.itemStack.typeId === "pu_se:enderman_shield" && event.source.isSneaking){
		const cooldown = event.itemStack.getComponent("minecraft:cooldown");
		const remainingCooldown = cooldown.getCooldownTicksRemaining(event.source);
		if (remainingCooldown === 0){
			enderman_shield.ItemUse(event);
		}
	}
	if (event.itemStack.typeId === "pu_se:spore_blossom_shield" && event.source.isSneaking){
		const cooldown = event.itemStack.getComponent("minecraft:cooldown");
		const remainingCooldown = cooldown.getCooldownTicksRemaining(event.source);
		if (remainingCooldown === 0){
			spore_blossom_shield.ItemUse(event);
		}
	}
	if (event.itemStack.typeId === "pu_se:magma_shield" && event.source.isSneaking){
		const cooldown = event.itemStack.getComponent("minecraft:cooldown");
		const remainingCooldown = cooldown.getCooldownTicksRemaining(event.source);
		if (remainingCooldown === 0){
			magma_shield.ItemUse(event);
			cooldown.startCooldown(event.source)
		}
	}
}

export async function onItemStartUse(event)
{
	//if (event.itemStack.typeId === "some_name")
	//	some_module.itemStartUse(event);
}


export async function onEntityHitBlock(event)
{

}

/*export async function onPlayerInteractWithEntity(event)
{

}*/