import { world, system, ItemStack, BlockPermutation, MolangVariableMap } from "@minecraft/server";
import { decrementstack } from "./bum_crops_custom_components.js";

// 			bonemeal foo

world.beforeEvents.worldInitialize.subscribe(initEvent => {

	initEvent.blockComponentRegistry.registerCustomComponent('bumble:bonemealable_flower', {
		onPlayerInteract: e => {
			const block = e.block
			const player = e.player
			const item = e.player.getComponent('equippable').getEquipment('Mainhand')
			if (item?.typeId == "minecraft:bone_meal") {
				switch (block.typeId) {
					case "bumble:snapdragons":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_snapdragons"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:small_rose":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_small_rose"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:dafodil_white":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_dafodil_white"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:dafodil_yellow":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_dafodil_yellow"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:hyacinth_white":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_hyacinth_white"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:hyacinth_pink":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_hyacinth_pink"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:hyacinth_purple":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_hyacinth_purple"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:hyacinth_blue":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_hyacinth_blue"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:iris":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_iris"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:lupines_pink":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_lupines"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:lupines_blue":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_lupines_blue"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:mums_orange":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_mums_orange"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:mums_red":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_mums_red"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:mums_yellow":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_mums_yellow"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:mums_white":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_mums_white"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:morning_glory_pink":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_morning_glory_pink"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
					case "bumble:pansies_purple":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot "plants/flowers/flower_pansies_purple"')
						block.setPermutation(block.permutation.withState('bumble:can_clone', 0))
						break;
				}
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:refresh_flower', {
		onRandomTick: e => {
			const block = e.block
			let num = Math.round(Math.random() * 8)
			if (num <= 1) {
				block.setPermutation(block.permutation.withState('bumble:can_clone', 1))
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:small_rose_gold', {
		onPlayerInteract: e => {
			const block = e.block
			const player = e.player
			const item = e.player.getComponent('equippable').getEquipment('Mainhand')
			if (item?.typeId == "minecraft:gold_ingot") {
				switch (block.typeId) {
					case "bumble:small_rose":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						small_rose_transform(block, player)
						break;
					case "bumble:pot_small_rose":
						block.dimension.spawnParticle('minecraft:crop_growth_emitter', block.center())
						block.dimension.playSound('item.bone_meal.use', block.center())
						decrementstack(item, player)
						small_rose_pot_transform(block, player)
						break;
				}
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:gold_rose_AOE', {
		onTick: e => {
			const block = e.block
			for (let j = 0; j < 5; j++) {
				for (let k = 0; k < 5; k++) {
					var entity = block.dimension.getEntitiesAtBlockLocation({ x: block.location.x - 2 + j, y: block.location.y, z: block.location.z - 2 + k })
					if (entity.length > 0) {
						for (let i = 0; i < entity.length; i++) {
							entity[i].runCommandAsync('effect @s regeneration 4 3 true')
						}
					}
				}
			}
		},
	});
})
function small_rose_transform(block, player) {
	let num = Math.round(Math.random(1, 1000))
	if (num == 1) {
		player.runCommandAsync('summon lightning_bolt ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z))
		block.setType("bumble:gold_rose")
	}
}
function small_rose_pot_transform(block, player) {
	let num = Math.round(Math.random(1, 1000))
	if (num == 1) {
		player.runCommandAsync('summon lightning_bolt ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z))
		block.setType("bumble:pot_gold_rose")
	}
}