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.InvalidUseException;
import core.util.Utils;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.User;


public class CmdBully extends Command {

	private final Integer COOLDOWN_TIME_MINUTES = 5;
	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;
	}

	@Override
	public Message execCommand(Server server, Member member, String[] args) {
		if (args.length == 1) {
			// Match user to given name
			Member m = server.getMember(args[0]);

			if (m != null) {
				User u = m.getUser();
				// Check command invoker is not on cooldown
				if (!cooldownCollection.containsKey(member.getUser()) || System.currentTimeMillis()
						- (60000 * COOLDOWN_TIME_MINUTES) >= cooldownCollection.get(member.getUser())) {
					if (u.getId().equals(Constants.OWNER_ID)) {
						u = member.getUser();
					}
					// Create message with random string in actionList
					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", 
									COOLDOWN_TIME_MINUTES -
									((System.currentTimeMillis() - cooldownCollection.get(member.getUser())) / 60000)), false);
				}
			} else {
				throw new InvalidUseException("User does not exist");
			}
		} else {
			throw new BadArgumentsException();
		}
		System.out.println(success());
		
		return response;
	}
	
	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;
	}
}

@@ -181,34 +181,8 @@
 st;%0A
-import java.util.HashMap;%0A
 impo
@@ -200,16 +200,16 @@
 l.List;%0A
+
 %0Aimport 
@@ -548,58 +548,8 @@
  %7B%0A%0A
-%09private final Integer COOLDOWN_TIME_MINUTES = 5;%0A
 %09pri
@@ -638,85 +638,8 @@
 m();
-%0A%09private HashMap%3CUser, Long%3E cooldownCollection = new HashMap%3CUser, Long%3E();
 %0A%0A%09p
@@ -1018,959 +1018,149 @@
 %09%09%09%09
-// Check command invoker is not on cooldown%0A%09%09%09%09if (!cooldownCollection.containsKey(member.getUser()) %7C%7C System.currentTimeMillis()%0A%09%09%09%09%09%09- (60000 * COOLDOWN_TIME_MINUTES) %3E= cooldownCollection.get(member.getUser())) %7B%0A%09%09%09%09%09if (u.getId().equals(Constants.OWNER_ID)) %7B%0A%09%09%09%09%09%09u = member.getUser();%0A%09%09%09%09%09%7D%0A%09%09%09%09%09// Create message with random string in actionList%0A%09%09%09%09%09this.response = Utils%0A%09%09%09%09%09%09%09.createMessage(String.format(actionList.get(random.nextInt(actionList.size())), u.getId()));%0A%09%09%09%09%09// Put user in cooldownCollection%0A%09%09%09%09%09if (!member.getUser().getId().equals(Constants.OWNER_ID)) %7B%0A%09%09%09%09%09%09cooldownCollection.put(member.getUser(), System.currentTimeMillis());%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09this.response = Utils.createMessage(%22Your bullying is on cooldown%22, %0A%09%09%09%09%09%09%09String.format(%22Time remaining: %25d Minutes%22, %0A%09%09%09%09%09%09%09%09%09COOLDOWN_TIME_MINUTES -%0A%09%09%09%09%09%09%09%09%09((System.currentTimeMillis() - cooldownCollection.get(member.getUser())) / 60000)), false
+String action = actionList.get(random.nextInt(actionList.size()));%0A%09%09%09%09response = Utils.createMessage(String.format(action, u.getId())
 );%0A%09%09%09%09
-%7D
 %0A%09%09%09

Remove cooldown