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


world.beforeEvents.worldInitialize.subscribe(initEvent => {
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:pick_cattail', {
		onPlayerInteract: e => {
			const block = e.block
			const player = e.player
			if (e.player.getComponent('equippable').getEquipment('Mainhand')?.typeId != 'minecraft:bone_meal') {
				if (block.permutation.getState('bumble:has_reeds') > 1) {
					switch (block.typeId) {
						case "bumble:cattails":
							pick_reed(player, block, '"plants/plant_cattails_reed"', "dig.grass")
							break;
					}
				}
				if (block.permutation.getState('bumble:has_reeds') == 1) {
					switch (block.typeId) {
						case "bumble:cattails":
							pick_reed(player, block, '"plants/plant_cattails_reed"', "dig.grass")
							block.setPermutation(BlockPermutation.resolve('minecraft:air'))
							break;
					}
				}
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:cattails_place', {
		onPlace: e => {
			const block = e.block
			let num = Math.round(Math.random(0, 2))
			switch (block.typeId) {
				case "bumble:cattails":
					if (block.above(1).isAir) {
						block.above(1).setType("bumble:cattails_top")
						block.above(1).setPermutation(block.above(1).permutation.withState('minecraft:cardinal_direction', block.permutation.getState('minecraft:cardinal_direction')))
						block.above(1).setPermutation(block.above(1).permutation.withState('bumble:shape', num))
						block.above(1).setPermutation(block.above(1).permutation.withState('bumble:has_reeds', block.permutation.getState('bumble:has_reeds')))
					}
					break;
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:grow_cattail', {
		onRandomTick: e => {
			const block = e.block
			let num = Math.round(Math.random() * 8)
			if (num <= 1) {
				block.setPermutation(block.permutation.withState('bumble:has_reeds', block.permutation.getState('bumble:has_reeds') - 1))
				block.above(1).setPermutation(block.above(1).permutation.withState('bumble:has_reeds', block.permutation.getState('bumble:has_reeds')))
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:thatch_refresh', {
		onPlayerInteract: e => {
			const block = e.block
			const player = e.player
			const item = e.player.getComponent('equippable').getEquipment('Mainhand')
			if (item?.typeId == 'minecraft:water_bucket') {

				if (block.typeId == 'bumble:dry_thatch_block') {
					decrementstack(item, player)
					player.runCommandAsync('give @s bucket')
					player.runCommandAsync('playsound ' + 'random.splash' + '@a ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
					block.setType("bumble:faded_thatch_block")
				}
				else {
					decrementstack(item, player)
					player.runCommandAsync('give @s bucket')
					player.runCommandAsync('playsound ' + 'random.splash' + '@a ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
					block.setType("bumble:fresh_thatch_block")
				}
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:thatch_stairs_refresh', {
		onPlayerInteract: e => {
			const block = e.block
			const player = e.player
			const item = e.player.getComponent('equippable').getEquipment('Mainhand')
			let half = block.permutation.getState('minecraft:vertical_half')
			let card = block.permutation.getState('minecraft:cardinal_direction')
			let upsd = block.permutation.getState('bumble:is_upside_down')
			let dire = block.permutation.getState('bumble:direction')

			if (item?.typeId == 'minecraft:water_bucket') {

				if (block.typeId == 'bumble:dry_thatch_stairs') {
					decrementstack(item, player)
					player.runCommandAsync('give @s bucket')
					player.runCommandAsync('playsound ' + 'random.splash' + '@a ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
					block.setType("bumble:faded_thatch_stairs")
					block.setPermutation(block.permutation.withState('minecraft:vertical_half', half))
					block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', card))
					block.setPermutation(block.permutation.withState('bumble:is_upside_down', upsd))
					block.setPermutation(block.permutation.withState('bumble:direction', dire))
				}
				else {
					decrementstack(item, player)
					player.runCommandAsync('give @s bucket')
					player.runCommandAsync('playsound ' + 'random.splash' + '@a ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
					block.setType("bumble:fresh_thatch_stairs")
					block.setPermutation(block.permutation.withState('minecraft:vertical_half', half))
					block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', card))
					block.setPermutation(block.permutation.withState('bumble:is_upside_down', upsd))
					block.setPermutation(block.permutation.withState('bumble:direction', dire))
				}
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:thatch_slab_refresh', {
		onPlayerInteract: e => {
			const block = e.block
			const player = e.player
			const item = e.player.getComponent('equippable').getEquipment('Mainhand')
			let half = block.permutation.getState('minecraft:vertical_half')

			if (item?.typeId == 'minecraft:water_bucket') {

				if (block.typeId == 'bumble:dry_thatch_slab') {
					decrementstack(item, player)
					player.runCommandAsync('give @s bucket')
					player.runCommandAsync('playsound ' + 'random.splash' + '@a ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
					block.setType("bumble:faded_thatch_slab")
					block.setPermutation(block.permutation.withState('minecraft:vertical_half', half))
				}
				else {
					decrementstack(item, player)
					player.runCommandAsync('give @s bucket')
					player.runCommandAsync('playsound ' + 'random.splash' + '@a ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')
					block.setType("bumble:fresh_thatch_slab")
					block.setPermutation(block.permutation.withState('minecraft:vertical_half', half))
				}
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:thatch_dry', {
		onRandomTick: e => {
			const block = e.block
			const player = e.player
			let num = Math.round(Math.random() * 16)
			if (num <= 1) {
				if (block.typeId == 'bumble:fresh_thatch_block') {
					block.setType("bumble:faded_thatch_block")
				}
				else {
					block.setType("bumble:dry_thatch_block")
				}
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:thatch_stairs_dry', {
		onRandomTick: e => {
			const block = e.block
			const player = e.player
			let half = block.permutation.getState('minecraft:vertical_half')
			let card = block.permutation.getState('minecraft:cardinal_direction')
			let upsd = block.permutation.getState('bumble:is_upside_down')
			let dire = block.permutation.getState('bumble:direction')
			let num = Math.round(Math.random() * 16)
			if (num <= 1) {
				if (block.typeId == 'bumble:fresh_thatch_stairs') {
					block.setType("bumble:faded_thatch_stairs")
					block.setPermutation(block.permutation.withState('minecraft:vertical_half', half))
					block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', card))
					block.setPermutation(block.permutation.withState('bumble:is_upside_down', upsd))
					block.setPermutation(block.permutation.withState('bumble:direction', dire))
				}
				else {
					block.setType("bumble:dry_thatch_stairs")
					block.setPermutation(block.permutation.withState('minecraft:vertical_half', half))
					block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', card))
					block.setPermutation(block.permutation.withState('bumble:is_upside_down', upsd))
					block.setPermutation(block.permutation.withState('bumble:direction', dire))
				}
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:thatch_slab_dry', {
		onRandomTick: e => {
			const block = e.block
			const player = e.player
			let half = block.permutation.getState('minecraft:vertical_half')
			let num = Math.round(Math.random() * 16)
			if (num <= 1) {
				if (block.typeId == 'bumble:fresh_thatch_slab') {
					block.setType("bumble:faded_thatch_slab")
					block.setPermutation(block.permutation.withState('minecraft:vertical_half', half))
				}
				else {
					block.setType("bumble:dry_thatch_slab")
					block.setPermutation(block.permutation.withState('minecraft:vertical_half', half))
				}
			}
		},
	});
	initEvent.blockComponentRegistry.registerCustomComponent('bumble:thatch_combust', {
		onRandomTick: e => {
			const block = e.block
			const player = e.player
			let num = Math.round(Math.random() * 8)
			if (num <= 1) {
				block.dimension.runCommandAsync("fill ~-1 ~-1 ~-1 ~+1 ~+1 ~+1 fire replace torch")
				block.dimension.runCommandAsync("fill ~-1 ~-1 ~-1 ~+1 ~+1 ~+1 fire replace campfire")
				block.dimension.runCommandAsync("fill ~-1 ~-1 ~-1 ~+1 ~+1 ~+1 fire replace furnace")
				block.dimension.runCommandAsync("fill ~-1 ~-1 ~-1 ~+1 ~+1 ~+1 soul_fire replace soul_torch")
				block.dimension.runCommandAsync("fill ~-1 ~-1 ~-1 ~+1 ~+1 ~+1 soul_fire replace soul_campfire")
			}
		},
	});
});
function pick_reed(player, block, loot, sound) {
	player.runCommandAsync('loot spawn ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y + 1) + ' ' + JSON.stringify(block.location.z) + ' loot ' + loot)
	player.runCommandAsync('playsound ' + sound + '@a ' + JSON.stringify(block.location.x) + ' ' + JSON.stringify(block.location.y) + ' ' + JSON.stringify(block.location.z) + ' 0.5')

	if (block.permutation.getState('bumble:has_reeds') != undefined) {
		block.setPermutation(block.permutation.withState('bumble:has_reeds', block.permutation.getState('bumble:has_reeds') - 1))
		block.above(1).setPermutation(block.above(1).permutation.withState('bumble:has_reeds', block.permutation.getState('bumble:has_reeds')))
	}
}
//Decrement stack
function decrementstack(item, user) {
	if (!(item?.typeId == undefined)) {
		if (item.amount > 1) {
			item.amount--;
			user.getComponent('equippable').setEquipment('Mainhand', item)
		}
		else if (item.amount === 1) {
			user.getComponent('equippable').setEquipment('Mainhand', undefined)
		}
	}
}