/*
 * Copyright  2014 - 2017 | Wurst-Imperium | All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package tk.wurst_client.features.mods;

import net.minecraft.block.Block;
import net.minecraft.block.BlockCocoa;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.BlockGrass;
import net.minecraft.block.BlockSapling;
import net.minecraft.block.BlockStem;
import net.minecraft.block.IGrowable;
import net.minecraft.item.ItemDye;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import tk.wurst_client.events.listeners.UpdateListener;
import tk.wurst_client.features.special_features.YesCheatSpf.BypassLevel;
import tk.wurst_client.settings.CheckboxSetting;
import tk.wurst_client.settings.SliderSetting;
import tk.wurst_client.settings.SliderSetting.ValueDisplay;
import tk.wurst_client.utils.BlockUtils;
import tk.wurst_client.utils.InventoryUtils;

@Mod.Info(
	description = "Automatically uses bone meal on specific types of plants.\n"
		+ "Use the checkboxes to specify the types of plants.",
	name = "BonemealAura",
	tags = "bonemeal aura, bone meal aura, AutoBone, auto bone",
	help = "Mods/BonemealAura")
@Mod.Bypasses(ghostMode = false)
public class BonemealAuraMod extends Mod implements UpdateListener
{
	public final SliderSetting range =
		new SliderSetting("Range", 4.25, 1, 6, 0.05, ValueDisplay.DECIMAL);
	
	private final CheckboxSetting saplings =
		new CheckboxSetting("Saplings", true);
	private final CheckboxSetting crops = new CheckboxSetting("Crops", true);
	private final CheckboxSetting stems = new CheckboxSetting("Stems", true);
	private final CheckboxSetting cocoa = new CheckboxSetting("Cocoa", true);
	private final CheckboxSetting other = new CheckboxSetting("Other", false);
	
	@Override
	public void initSettings()
	{
		settings.add(range);
		settings.add(saplings);
		settings.add(crops);
		settings.add(stems);
		settings.add(cocoa);
		settings.add(other);
	}
	
	@Override
	public void onEnable()
	{
		wurst.events.add(UpdateListener.class, this);
	}
	
	@Override
	public void onDisable()
	{
		wurst.events.remove(UpdateListener.class, this);
	}
	
	@Override
	public void onUpdate()
	{
		// wait for right click timer
		if(mc.rightClickDelayTimer > 0)
			return;
		
		// check held item
		ItemStack stack = mc.player.inventory.getCurrentItem();
		if(InventoryUtils.isEmptySlot(stack)
			|| !(stack.getItem() instanceof ItemDye)
			|| stack.getMetadata() != 15)
			return;
		
		boolean legit = wurst.special.yesCheatSpf.getBypassLevel()
			.ordinal() > BypassLevel.MINEPLEX.ordinal();
		
		if(legit)
		{
			// use bone meal on closest valid block
			for(BlockPos pos : BlockUtils.getValidBlocksByDistance(
				range.getValue(), false, (p) -> isCorrectBlock(p)))
			{
				BlockUtils.rightClickBlockLegit(pos);
				break;
			}
			
		}else
		{
			boolean shouldSwing = false;
			
			// use bone meal on all valid blocks
			for(BlockPos pos : BlockUtils.getValidBlocks(range.getValue(),
				(p) -> isCorrectBlock(p)))
			{
				shouldSwing = true;
				BlockUtils.rightClickBlockSimple(pos);
			}
			
			// swing arm
			if(shouldSwing)
				mc.player.swingArm(EnumHand.MAIN_HAND);
		}
	}
	
	@Override
	public void onYesCheatUpdate(BypassLevel bypassLevel)
	{
		switch(bypassLevel)
		{
			default:
			case OFF:
			case MINEPLEX:
				range.unlock();
				break;
			case ANTICHEAT:
			case OLDER_NCP:
			case LATEST_NCP:
			case GHOST_MODE:
				range.lockToMax(4.25);
				break;
		}
	}
	
	private boolean isCorrectBlock(BlockPos pos)
	{
		Block block = BlockUtils.getBlock(pos);
		
		if(!(block instanceof IGrowable) || block instanceof BlockGrass
			|| !((IGrowable)block).canGrow(mc.world, pos,
				BlockUtils.getState(pos), false))
			return false;
		
		if(block instanceof BlockSapling)
			return saplings.isChecked();
		else if(block instanceof BlockCrops)
			return crops.isChecked();
		else if(block instanceof BlockStem)
			return stems.isChecked();
		else if(block instanceof BlockCocoa)
			return cocoa.isChecked();
		else
			return other.isChecked();
	}
}

@@ -2665,24 +2665,170 @@
 %09%0A%09%09
-boolean legit = 
+// get valid blocks%0A%09%09Iterable%3CBlockPos%3E validBlocks = BlockUtils%0A%09%09%09.getValidBlocks(range.getValue(), (p) -%3E isCorrectBlock(p));%0A%09%09%0A%09%09// check bypass level%0A%09%09if(
 wurs
@@ -2916,23 +2916,8 @@
 al()
-;%0A%09%09%0A%09%09if(legit
 )%0A%09%09
@@ -2945,14 +2945,11 @@
  on 
-closes
+nex
 t va
@@ -2984,110 +2984,28 @@
 s : 
-BlockUtils.getValidBlocksByDistance(%0A%09%09%09%09range.getValue(), false, (p) -%3E isCorrectBlock(p)))%0A%09%09%09%7B%0A%09%09%09%09
+validBlocks)%0A%09%09%09%09if(
 Bloc
@@ -3036,18 +3036,19 @@
 git(pos)
-;%0A
+)%0A%09
 %09%09%09%09brea
@@ -3051,20 +3051,16 @@
 break;%0A%09
-%09%09%7D%0A
 %09%09%09%0A%09%09%7De
@@ -3169,116 +3169,28 @@
 s : 
-BlockUtils.getValidBlocks(range.getValue(),%0A%09%09%09%09(p) -%3E isCorrectBlock(p)))%0A%09%09%09%7B%0A%09%09%09%09shouldSwing = true;%0A%09%09%09%09
+validBlocks)%0A%09%09%09%09if(
 Bloc
@@ -3226,15 +3226,36 @@
 pos)
-;%0A%09%09%09%7D%0A
+)%0A%09%09%09%09%09shouldSwing = true;%0A%09
 %09%09%09%0A

Update BonemealAura