// Path: src/main/java/clientapi/command/exception/CommandInitException.java
// public final class CommandInitException extends CommandException {
// 
//     public CommandInitException(Command command, String message) {
//         super(command, message);
//     }
// }
// 
// Path: src/main/java/clientapi/command/executor/ExecutionContext.java
// public interface ExecutionContext {
// 
//     /**
//      * @return The {@link CommandSender} responsible for the command execution
//      */
//     CommandSender sender();
// 
//     /**
//      * @return The {@link CommandHandler} that carried out command execution
//      */
//     CommandHandler handler();
// 
//     static ExecutionContext of(CommandSender sender, CommandHandler handler) {
//         return new Impl(sender, handler);
//     }
// 
//     /**
//      * Implementation of {@link ExecutionContext}, used when
//      * creating an instance of {@link ExecutionContext}
//      * from {@link ExecutionContext#of(CommandSender, CommandHandler)}
//      *
//      * @see ExecutionContext#of(CommandSender, CommandHandler)
//      */
//     class Impl implements ExecutionContext {
// 
//         /**
//          * {@link CommandSender} responsible for command execution
//          */
//         private final CommandSender sender;
// 
//         /**
//          * {@link CommandHandler} that carried out command execution
//          */
//         private final CommandHandler handler;
// 
//         private Impl(CommandSender sender, CommandHandler handler) {
//             this.sender = sender;
//             this.handler = handler;
//         }
// 
//         @Override
//         public CommandSender sender() {
//             return this.sender;
//         }
// 
//         @Override
//         public CommandHandler handler() {
//             return this.handler;
//         }
//     }
// }


import clientapi.command.annotation.Cmd;
import clientapi.command.annotation.Sub;
import clientapi.command.exception.CommandException;
import clientapi.command.exception.CommandInitException;
import clientapi.command.exception.UnknownSubCommandException;
import clientapi.command.executor.ExecutionContext;
import clientapi.command.executor.parser.CommandInputParser;
import clientapi.util.ClientAPIUtils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/*
 * Copyright 2018 ImpactDevelopment
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package clientapi.command;



/**
 * Implementation of {@link ICommand}
 *
 * @author Brady
 * @since 5/31/2017
 */
public class Command implements ICommand {

    /**
     * A list of this command's child commands
     */
    private final List<ChildCommand> children = new ArrayList<>();

    /**
     * A list of handles used to execute this command
     */
    private String[] handles;

    /**
     * A description of this command's usage
     */
    private String description;

    public Command() {
        if (!this.getClass().isAnnotationPresent(Cmd.class))
            throw new CommandInitException(this, "@Cmd annotation must be present if required parameters aren't passed through constructor");

        Cmd data = this.getClass().getAnnotation(Cmd.class);
        setup(data.handles(), data.description());
    }

    public Command(String[] handles, String description) {
        setup(handles, description);
    }

    private void setup(String[] handles, String description) {
        this.handles = handles;
        this.description = description;

        for (Method method : this.getClass().getDeclaredMethods()) {
            int parameters = method.getParameterCount();
// Path: src/main/java/clientapi/command/exception/CommandInitException.java
// public final class CommandInitException extends CommandException {
// 
//     public CommandInitException(Command command, String message) {
//         super(command, message);
//     }
// }
// 
// Path: src/main/java/clientapi/command/executor/ExecutionContext.java
// public interface ExecutionContext {
// 
//     /**
//      * @return The {@link CommandSender} responsible for the command execution
//      */
//     CommandSender sender();
// 
//     /**
//      * @return The {@link CommandHandler} that carried out command execution
//      */
//     CommandHandler handler();
// 
//     static ExecutionContext of(CommandSender sender, CommandHandler handler) {
//         return new Impl(sender, handler);
//     }
// 
//     /**
//      * Implementation of {@link ExecutionContext}, used when
//      * creating an instance of {@link ExecutionContext}
//      * from {@link ExecutionContext#of(CommandSender, CommandHandler)}
//      *
//      * @see ExecutionContext#of(CommandSender, CommandHandler)
//      */
//     class Impl implements ExecutionContext {
// 
//         /**
//          * {@link CommandSender} responsible for command execution
//          */
//         private final CommandSender sender;
// 
//         /**
//          * {@link CommandHandler} that carried out command execution
//          */
//         private final CommandHandler handler;
// 
//         private Impl(CommandSender sender, CommandHandler handler) {
//             this.sender = sender;
//             this.handler = handler;
//         }
// 
//         @Override
//         public CommandSender sender() {
//             return this.sender;
//         }
// 
//         @Override
//         public CommandHandler handler() {
//             return this.handler;
//         }
//     }
// }


// Path: src/main/java/clientapi/command/Command.java
import clientapi.command.annotation.Cmd;
import clientapi.command.annotation.Sub;
import clientapi.command.exception.CommandException;
import clientapi.command.exception.CommandInitException;
import clientapi.command.exception.UnknownSubCommandException;
import clientapi.command.executor.ExecutionContext;
import clientapi.command.executor.parser.CommandInputParser;
import clientapi.util.ClientAPIUtils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/*
 * Copyright 2018 ImpactDevelopment
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package clientapi.command;



/**
 * Implementation of {@link ICommand}
 *
 * @author Brady
 * @since 5/31/2017
 */
public class Command implements ICommand {

    /**
     * A list of this command's child commands
     */
    private final List<ChildCommand> children = new ArrayList<>();

    /**
     * A list of handles used to execute this command
     */
    private String[] handles;

    /**
     * A description of this command's usage
     */
    private String description;

    public Command() {
        if (!this.getClass().isAnnotationPresent(Cmd.class))
            throw new CommandInitException(this, "@Cmd annotation must be present if required parameters aren't passed through constructor");

        Cmd data = this.getClass().getAnnotation(Cmd.class);
        setup(data.handles(), data.description());
    }

    public Command(String[] handles, String description) {
        setup(handles, description);
    }

    private void setup(String[] handles, String description) {
        this.handles = handles;
        this.description = description;

        for (Method method : this.getClass().getDeclaredMethods()) {
            int parameters = method.getParameterCount();
            if (parameters == 0 || !method.getParameterTypes()[0].equals(ExecutionContext.class) || !method.isAnnotationPresent(Sub.class))