import { world, system } from "@minecraft/server";
import * as utils from "./utils";
import * as scorpion from "./mobs/scorpion";
import * as book from "./book";
//Weapons
import * as alien_blaster from "./weapons/alien_blaster";
import * as amethyst_sword from "./weapons/amethyst_sword";
import * as boomerang from "./weapons/boomerang";
import * as carrot_sword from "./weapons/carrot_sword";
import * as cooked_meat_bone from "./weapons/cooked_meat_bone";
import * as demon_sword from "./weapons/demon_sword";
import * as desert_scythe from "./weapons/desert_scythe";
import * as fire_sword from "./weapons/fire_sword";
import * as frozen_viking_battle_axe from "./weapons/frozen_viking_battle_axe";
import * as katana from "./weapons/katana";
import * as lightning_sword from "./weapons/lightning_sword";
import * as necromancer_wand from "./weapons/necromancer_wand";
import * as ninja_star from "./weapons/ninja_star";
import * as pink_sword from "./weapons/pink_sword";
import * as poison_sword from "./weapons/poison_sword";
import * as rgb_sword from "./weapons/rgb_sword";
import * as sarbacane from "./weapons/sarbacane";
import * as scifi_hammer from "./weapons/scifi_hammer";
import * as spears from "./weapons/spears";
//Items
import * as aloe_vera_flower from "./items/aloe_vera_flower";
import * as healing_bag from "./items/healing_bag";
import * as scifi_medkit from "./items/scifi_medkit";
import * as pirate_compass from "./items/pirate_compass";
//Armor
import * as crown from "./armor/crown";
import * as demon_armor from "./armor/demon_armor";
import * as fire_dragon_armor from "./armor/fire_dragon_armor";
import * as kimono from "./armor/kimono";
import * as rgb_armor from "./armor/rgb_armor";
import * as water_armor from "./armor/water_armor";
import * as wooden_jetpack from "./armor/wooden_jetpack";

export async function onLoadFirstTime()
{
	// If needed - call init functions
	await utils.runCommand("gamerule commandblockoutput false");
	await system.runTimeout(() => { utils.runPlayer0Command("loot spawn ~ ~ ~ loot \"pu/sg/book\"") }, 90);
	await system.runTimeout(() => { utils.runPlayer0Command("loot spawn ~ ~ ~10 loot \"pu/sg/book\"") }, 90);
	await system.runTimeout(() => { utils.runPlayer0Command("tp @e[rm=7,r=20,type=item,name=\"Structures Guide Book\"] ~ ~-250 ~") }, 95);
	await system.runTimeout(() => { utils.runPlayer0Command("tp @e[rm=7,r=20,type=item,name=\"Livro Guia de Estruturas\"] ~ ~-250 ~") }, 95);
	await system.runTimeout(() => { utils.runPlayer0Command("tp @e[rm=7,r=20,type=item,name=\"Livre Guide des Structures\"] ~ ~-250 ~") }, 95);
	await system.runTimeout(() => { utils.runPlayer0Command("tp @e[rm=7,r=20,type=item,name=\"Libro Guía de Estructuras\"] ~ ~-250 ~") }, 95);
	await system.runTimeout(() => { utils.runPlayer0Command("tp @e[rm=7,r=20,type=item,name=\"Strukturen-Leitfadenbuch\"] ~ ~-250 ~") }, 95);
}

export async function onLoad(){
}

let foundLanguage = false;
let french = false;
let english = false;
let portugese = false;
let spanish = false;
let german = false;
let nextTick = -100;
export async function onTick(){
	if (english){
		await book.giveEnglishBook();
	}
	if (french){
		await book.giveFrenchBook();
	}
	if (spanish){
		await book.giveSpanishBook();
	}
	if (portugese){
		await book.givePortugueseBook();
	}
	if (german){
		await book.giveGermanBook();
	}

	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_sg:book"){
				utils.runEntityCommand(entity, "execute if entity @s[name=\"Structures Guide Book\"] run tag @p add pu_sg_english");
				utils.runEntityCommand(entity, "execute if entity @s[name=\"Livro Guia de Estruturas\"] run tag @p add pu_sg_portugese");
				utils.runEntityCommand(entity, "execute if entity @s[name=\"Livre Guide des Structures\"] run tag @p add pu_sg_french");
				utils.runEntityCommand(entity, "execute if entity @s[name=\"Libro Guía de Estructuras\"] run tag @p add pu_sg_spanish");
				utils.runEntityCommand(entity, "execute if entity @s[name=\"Strukturen-Leitfadenbuch\"] run tag @p add pu_sg_german");
				foundLanguage = true;
			}
		}
	}
	if (english == false && french == false && spanish == false && portugese == false && german == false){
		if (utils.player0.hasTag("pu_sg_english"))
			english = true;
		if (utils.player0.hasTag("pu_sg_french"))
			french = true;
		if (utils.player0.hasTag("pu_sg_spanish"))
			spanish = true;
		if (utils.player0.hasTag("pu_sg_portugese"))
			portugese = true;
		if (utils.player0.hasTag("pu_sg_german"))
			german = true;
	}

	crown.onTick();
	demon_armor.onTick();
	fire_dragon_armor.onTick();
	kimono.onTick();
	rgb_armor.onTick();
	water_armor.onTick();
	wooden_jetpack.onTick();
	sarbacane.onTick();
	pirate_compass.onTick();
	ninja_star.onTick();
	spears.onTick();
	alien_blaster.onTick();
	book.onTick();
	

    // if(system.currentTick >= nextTick)
	// {
	// 	nextTick = system.currentTick + 20;
	// }
}
// export async function onItemStartUse(event)
// {
// }

export async function onItemReleaseUse(event)
{
	//ninja_star.onItemReleaseUse(event);
}

export async function onItemUseOn(event)
{
	
}

// export async function onEntityRemove(event)
// {
// }

// export async function onItemStopUse(event)
// {
// }

export async function onItemUse(event)
{
	fire_sword.onItemUse(event);
	sarbacane.onItemUse(event);
	necromancer_wand.onItemUse(event);
	boomerang.onItemUse(event);
	demon_sword.onItemUse(event);
	lightning_sword.onItemUse(event);
	spears.onItemUse(event);
	pirate_compass.onItemUse(event);
	scifi_medkit.onItemUse(event);
	alien_blaster.onItemUse(event);
}

// export async function onProjectileHitBlock(event)
// {
// }




// export async function onPlayerJoin(event)
// {
// }

// export async function onPlayerSpawn(event)
// {
// }

// export async function onEntityDie(event)
// {
// }

// export async function onPlayerInteractWithEntity(event)
// {
// }

export async function onEntitySpawn(event)
{
	boomerang.onEntitySpawn(event);
	ninja_star.onEntitySpawn(event);
}

export async function onEntityHurt(event) {
	sarbacane.onEntityHurt(event);
	amethyst_sword.onEntityHurt(event);
	desert_scythe.onEntityHurt(event);
	fire_dragon_armor.onEntityHurt(event);
	frozen_viking_battle_axe.onEntityHurt(event);
	poison_sword.onEntityHurt(event);
	scorpion.onEntityHurt(event);
	demon_sword.onEntityHurt(event);
	fire_sword.onEntityHurt(event);
	katana.onEntityHurt(event);
	lightning_sword.onEntityHurt(event);
	pink_sword.onEntityHurt(event);
	rgb_sword.onEntityHurt(event);
	scifi_hammer.onEntityHurt(event);
}

export async function onitemCompleteUse(event)
{
	aloe_vera_flower.onItemCompleteUse(event);
	healing_bag.onItemCompleteUse(event);
	carrot_sword.onItemCompleteUse(event);
	cooked_meat_bone.onItemCompleteUse(event);
}

// export async function onitemUse(event)
// {

// }



// export async function onEntityHitBlock(event)
// {

// }