//* Wheat farming */
/**
 * Instructions:
 * - For the area, click on top of the farmland area you want the bot to farm in
 * - For the containers you can set to barrel or chest or any other containers
 * - Set the chargepad for the android to recharge 
 * */

// These variables are exported to the settings UI
area = Self.exportBlockArea("area","Farm Area", null);
pos_container_seeds = Self.exportBlockPos("pos_container_seeds", "Seeds Container", null);
pos_container_wheat = Self.exportBlockPos("pos_container_wheat", "Wheat Container", null);
pos_chargepad = Self.exportBlockPos("pos_chargepad", "Chargepad", null)

// To keep store between ticks
grownBlockPositions = [];
emptyBlockPositions = [];

// Runs at the bot turning on once
function start() {
	// Safety check
	if(area == null || pos_container_seeds == null || pos_container_wheat == null || pos_chargepad == null)
		Self.shutdown();

	// allow the android to pickup dropped items
	Self.setPickupItem(true);
}

// Runs every tick unless halt (sleep, await etc.)
async function tick() {
	// Battery check
	if(Self.getBatteryPercentage() <= 10) {
		// Go to chargepad
		await Self.navigateToAsync(pos_chargepad, 200, 3);
		return;
	}

	// Check for wheat blocks in area
	let blockPositions = World.queryBlocksInAreaById(area, "minecraft:wheat");

	// Look for grown wheats
	if(grownBlockPositions.length == 0) {
		for(let pos of blockPositions) {
			if(World.blockCropIsMature(pos)) {
				grownBlockPositions.push(pos);
			}
		}
	}

	// Harvest grown wheats
	else {
		let target = grownBlockPositions.pop();
		await Self.navigateToAsync(target, 200, 3);
		Self.destroyBlock(target);
		await sleep(20); // delay to pickup items
	}

	// Put in chests
	if(Self.hasItem("minecraft:wheat")) {
		await Self.navigateToAsync(pos_container_wheat, 200, 3);
		Self.dumpInventoryToContainer(pos_container_wheat, "minecraft:wheat");
	}
	if(Self.hasItem("minecraft:wheat_seeds")) {
		await Self.navigateToAsync(pos_container_wheat, 200, 3);
		Self.dumpInventoryToContainer(pos_container_seeds, "minecraft:wheat_seeds");
	}

	// Check for lying items on the ground
	var items = Self.getItemsInArea(area, null);
	if(items.length > 0) {
		for(let pos of items) {
			await Self.navigateToAsync(pos, 200, 3);
		}
	}

	/// Replant empty fields
	if(emptyBlockPositions.length == 0) {
		emptyBlockPositions = World.queryBlocksInAreaById(area, "minecraft:air");
	} else {
		// calculate required seeds
		let seedsCount = emptyBlockPositions.length;
		let plantPositions = [];

		for(let pos of emptyBlockPositions) {
			if(World.getBlockAt([pos[0], pos[1]-1, pos[2]]) == "minecraft:farmland")
				plantPositions.push([pos[0], pos[1]-1, pos[2]]); // Block is plantable
			else
				seedsCount -= 1; // Block is not plantable
		}

		// take the seeds from the container
		await Self.navigateToAsync(pos_container_seeds, 200, 3);
		Self.getItemFromContainer(pos_container_seeds, "minecraft:wheat_seeds", seedsCount);

		// plant the seeds
		for(let pos of plantPositions) {
			if(World.getBlockAt(pos) == "minecraft:farmland") {
				await Self.navigateToAsync(pos, 200, 3);
				Self.plantSeed(pos, "minecraft:wheat_seeds");
			}
		}

		emptyBlockPositions = [];
	}

}