package core.commands;

import java.util.Random;
import java.util.Scanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import core.Constants;
import core.entities.Server;
import core.exceptions.BadArgumentsException;
import core.exceptions.DoesNotExistException;
import core.util.Utils;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.User;


public class CmdBully extends Command {

	private List<String> actionList = getActionList();
	
	private Random random = new Random();
	private HashMap<User, Long> cooldownCollection = new HashMap<User, Long>();

	public CmdBully() {
		this.helpMsg = Constants.BULLY_HELP;
		this.description = Constants.BULLY_DESC;
		this.name = Constants.BULLY_NAME;
		this.pugCommand = false;
	}

	@Override
	public void execCommand(Server server, Member member, String[] args) {
		try {
			if (args.length == 1) {
				// Match user to given name
				User u = null;
				for (Member m : member.getGuild().getMembers()) {
					if (m.getUser().getName().equalsIgnoreCase(args[0]) ||  m.getEffectiveName().equalsIgnoreCase(args[0])) {
						u = m.getUser();
						break;
					}
				}
				if (u != null) {
					// Check invoker is not on cooldown
					if (!cooldownCollection.containsKey(member.getUser()) || System.currentTimeMillis() - (60000 * 30) >= cooldownCollection.get(member.getUser())) {
						if (u.getId().equals(Constants.OWNER_ID)) {
							u = member.getUser();
						}
						this.response = Utils.createMessage(String.format(actionList.get(random.nextInt(actionList.size())), u.getId()));
						// Put user in cooldownCollection
						if(!member.getUser().getId().equals(Constants.OWNER_ID)){
							cooldownCollection.put(member.getUser(), System.currentTimeMillis());
						}
					} else {
						this.response = Utils.createMessage("Your bullying is on cooldown", String.format("Time remaining: %d Minutes",
								30 - ((System.currentTimeMillis() - cooldownCollection.get(member.getUser())) / 60000)), false);
					}
				} else {
					throw new DoesNotExistException("User");
				}
			} else {
				throw new BadArgumentsException();
			}
			System.out.println(success());
		} catch (BadArgumentsException | DoesNotExistException ex) {
			this.response = Utils.createMessage("Error!", ex.getMessage(), false);
		}
	}
	
	private List<String> getActionList(){
		List<String> bl = new ArrayList<String>();
		if (new File("app_data/bullylist.txt").exists()) {
			try {
				Scanner reader = new Scanner(new FileInputStream("app_data/bullylist.txt"));

				while (reader.hasNextLine()) {
					bl.add(reader.nextLine());
				}
				
				reader.close();
				return bl;
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}

}

@@ -526,16 +526,66 @@
 mand %7B%0A%0A
+%09private final Integer COOLDOWN_TIME_MINUTES = 5;%0A
 %09private
@@ -628,18 +628,16 @@
 List();%0A
-%09%0A
 %09private
@@ -1460,18 +1460,37 @@
 60000 * 
-30
+COOLDOWN_TIME_MINUTES
 ) %3E= coo
@@ -1613,24 +1613,81 @@
 ();%0A%09%09%09%09%09%09%7D%0A
+%09%09%09%09%09%09// Create message with random string in actionList%0A
 %09%09%09%09%09%09this.r
@@ -2127,10 +2127,29 @@
 %09%09%09%09
-30
+COOLDOWN_TIME_MINUTES
  - (

Add bully timeout time variable and reduced time to 5 minutes