package com.publicuhc.commands.requests;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.List;
import java.util.regex.MatchResult;

public class DefaultCommandRequestBuilder implements CommandRequestBuilder {

    private Command m_command;
    private List<String> m_arguments;
    private CommandSender m_commandSender;
    private MatchResult m_matchResult;

    @Override
    public CommandRequest build() {
        if (!isValid()) {
            throw new IllegalStateException("Command request state is invalid");
        }
        return new CommandRequest(m_command, m_arguments, m_commandSender, m_matchResult);
    }

    @Override
    @Nonnull
    public CommandRequestBuilder setCommand(Command command) {
        if (null == command) {
            throw new IllegalArgumentException("Command cannot be null");
        }
        m_command = command;
        return this;
    }

    @Override
    public CommandRequestBuilder setMatchResult(MatchResult result) {
        m_matchResult = result;
        return this;
    }

    @Override
    public CommandRequestBuilder setArguments(List<String> arguments) {
        if (null == arguments) {
            throw new IllegalArgumentException("Argument list cannot be null");
        }
        m_arguments = arguments;
        return this;
    }

    @Override
    public CommandRequestBuilder setArguments(String[] arguments) {
        return setArguments(Arrays.asList(arguments));
    }

    @Override
    public CommandRequestBuilder setSender(CommandSender sender) {
        if (null == sender) {
            throw new IllegalArgumentException("Sender cannot be null");
        }
        m_commandSender = sender;
        return this;
    }

    @Override
    public boolean isValid() {
        return !(m_commandSender == null || m_command == null || m_arguments == null);
    }

    @Override
    public void clear() {
        m_command = null;
        m_commandSender = null;
        m_arguments = null;
    }
}

@@ -979,32 +979,102 @@
 rn this;%0A    %7D%0A%0A
+    protected Command getCommand() %7B%0A        return m_command;%0A    %7D%0A%0A
     @Override%0A  
@@ -1153,51 +1153,252 @@
     
-m_matchResult = result;%0A        return this
+if (null == result) %7B%0A            throw new IllegalArgumentException(%22Match result cannot be null%22);%0A        %7D%0A        m_matchResult = result;%0A        return this;%0A    %7D%0A%0A    protected MatchResult getMatchResult() %7B%0A        return m_matchResult
 ;%0A  
@@ -1664,32 +1664,111 @@
 rn this;%0A    %7D%0A%0A
+    protected List%3CString%3E getArguments() %7B%0A        return m_arguments;%0A    %7D%0A%0A
     @Override%0A  
@@ -1825,32 +1825,155 @@
 %5B%5D arguments) %7B%0A
+        if (null == arguments) %7B%0A            throw new IllegalArgumentException(%22Argument list cannot be null%22);%0A        %7D%0A
         return s
@@ -2266,32 +2266,113 @@
 rn this;%0A    %7D%0A%0A
+    protected CommandSender getSender() %7B%0A        return m_commandSender;%0A    %7D%0A%0A
     @Override%0A  
@@ -2479,16 +2479,41 @@
 ments ==
+ null %7C%7C m_matchResult ==
  null);%0A
@@ -2641,20 +2641,50 @@
 nts = null;%0A
+        m_matchResult = null;%0A
     %7D%0A%7D%0A

add missing null checks add protected getters add matchresult checking