package me.despawningbone.discordbot.command;

import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Message;

public class CommandResult {
	
	//for logging
	private CommandResultType t;
	private String r;
	
	public enum CommandResultType {  //TODO add a noargs to specify please enter something to search for?
		INTERRUPTED,
		INVALIDARGS,  //print help?
		NORESULT,
		NOPERMS,
		TOOLONG,
		FAILURE,  //generic failure
		ERROR,
		SUCCESS,
		DISABLED
	}
	
	public CommandResult(CommandResultType type, String remarks) {  //TODO overload with CommandResult(CommandResultType, Exception)? 
		t = type;
		r = remarks;
	}
	
	public CommandResult(CommandResultType type) {
		t = type;
		r = null;
	}
	
	public CommandResultType getResultType() {
		return t;
	}
	
	public String getRemarks() {
		return t == CommandResultType.NORESULT || t == CommandResultType.NOPERMS || t == CommandResultType.TOOLONG ? null : r;
	}
	
	//make a getLogLevel() for log4j integration later?
	
	public Message getMessage() {
		MessageBuilder mb = new MessageBuilder();
		switch(t) {
		case DISABLED:
		case SUCCESS:  //already sent message
			return null;
		case INTERRUPTED:   //both are errors
		case ERROR: 
			String[] splits = r.split("\n");
			String line = "Unknown Source";
			for(String split : splits) {
				if(split.matches("(?s).*command\\..*") && split.trim().startsWith("at")) {
					line = split.replaceFirst(".*command\\..*\\((.*?)\\).*", "$1");
					break;
				}				
			}
			mb.append("Something went wrong at `" + line + "`: `" + splits[0].replaceAll(":.*", "") + "`");
			break;
		case NOPERMS: 
			mb.append("You need the `" + r + "` permission(s) for this channel to execute that command.");
			break;
		case NORESULT:
			mb.append("Unfortunately there was no results" + (r != null ? " for " + r : "") + " :cry:");
			break;
		case TOOLONG:
			mb.append("The description was too long discord can't stand it :joy:");
			break;
		default:  //TODO add "For help, do" for INVALIDARGS?
			mb.append(r);
		}
		return mb.build();
	}
	
}
