package me.loganb1max.minionsplus.command;

import com.google.common.collect.ImmutableList;
import me.loganb1max.minionsplus.MinionsPlus;
import me.loganb1max.minionsplus.model.Minion;
import me.loganb1max.minionsplus.model.minions.*;
import me.loganb1max.minionsplus.util.ItemUtil;
import me.loganb1max.minionsplus.util.Text;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class MinionCommand implements CommandExecutor, TabCompleter {

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

		if (!sender.hasPermission("minions.command")) {
			sender.sendMessage(Text.color("&cYou don't have permission for this command."));
			return true;
		}

		if (args.length < 1) {
			sendHelp(sender);
			return true;
		}

		if (args.length == 1) {
			if (args[0].equalsIgnoreCase("reload")) {
				MinionsPlus.getInstance().reloadConfig();
				sender.sendMessage(Text.color("&aSuccessfully reloaded config."));
			} else {
				sendHelp(sender);
			}
		}

		if (args.length >= 3) {
			if (!args[0].equalsIgnoreCase("give")) {
				sendHelp(sender);
				return true;
			}

			final Player target = Bukkit.getPlayer(args[1]);
			if (target == null) {
				sender.sendMessage(Text.color("&cPlayer Not Found."));
				return true;
			}

			Minion minion = null;

			final String type = args[2].toLowerCase();
			switch (type) {
				case "miner":
					minion = new MinerMinion(target.getUniqueId(), target.getName());
					break;
				case "farmer":
					minion = new FarmerMinion(target.getUniqueId(), target.getName());
					break;
				case "fisher":
					minion = new FisherMinion(target.getUniqueId(), target.getName());
					break;
				case "seller":
					minion = new SellerMinion(target.getUniqueId(), target.getName());
					break;
				case "butcher":
					minion = new ButcherMinion(target.getUniqueId(), target.getName());
					break;
				case "collector":
					minion = new CollectorMinion(target.getUniqueId(), target.getName());
					break;
				case "breeder":
					minion = new BreederMinion(target.getUniqueId(), target.getName());
					break;
			}

			if (minion == null) {
				sender.sendMessage(Text.color("&cInvalid Minion Type."));
				return true;
			}

			ItemUtil.giveAndDrop(target, minion.toItemStack());
			target.sendMessage(Text.color("&aYou have received a " + minion.getName()));
			sender.sendMessage(Text.color("&aYou gave " + target.getName() + " a " + minion.getName()));

		}

		return true;
	}

	@Override
	public List<String> onTabComplete(CommandSender commandSender, Command command, String label, String[] args) {
		List<String> toReturn = new ArrayList<>();
		switch (args.length) {
			case 1:
				toReturn = ImmutableList.of(
						"give",
						"reload"
				);
				break;
			case 2:
				toReturn = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
				break;
			case 3:
				toReturn = ImmutableList.of(
						"miner",
						"farmer",
						"fisher",
						"seller",
						"butcher",
						"collector",
						"breeder"
				);
				break;
		}
		return toReturn;
	}

	private void sendHelp(final CommandSender sender) {
		sender.sendMessage(Text.color("&7============================================"));
		sender.sendMessage(Text.color("&6             Minions Help Menu"));
		sender.sendMessage(Text.color("&7============================================"));
		sender.sendMessage(Text.color("&6      /minions give <player> <type>"));
		sender.sendMessage(Text.color("&6             /minions reload"));
	}

}
