class SccpExecutor implements ShellExecutor {

    private String manageAddress(String[] options) throws Exception {
        // Minimum 3 needed. Show
        if (options.length < 3) {
            return SccpOAMMessage.INVALID_COMMAND;
        }

        String command = options[2];

        if (command == null) {
            return SccpOAMMessage.INVALID_COMMAND;
        }

        if (command.equals("create")) {
            if (options.length < 4) {
                return SccpOAMMessage.INVALID_COMMAND;
            }
            int addressId = Integer.parseInt(options[3]);

            int count = 11;

            while (count < options.length) {
                String key = options[count++];
                if (key == null) {
                    return SccpOAMMessage.INVALID_COMMAND;
                }

                if (key.equals("stackname")) {
                    String m3uaStackName = options[count++];

                    SccpStackImpl sccpStaclImpl = this.sccpStacks.get(m3uaStackName);

                    if (sccpStaclImpl == null) {
                        return String.format(SccpOAMMessage.NO_SCCP_MANAGEMENT_BEAN_FOR_NAME, m3uaStackName);
                    }

                    this.sccpStack = sccpStaclImpl;
                } else {
                    return SccpOAMMessage.INVALID_COMMAND;
                }

            }

            this.setDefaultValue();
            SccpAddress address = this.createAddress(options, 4, false);

            this.sccpStack.getRouter().addRoutingAddress(addressId, address);
            return String.format(SccpOAMMessage.ADDRESS_SUCCESSFULLY_ADDED, this.sccpStack.getName());
        } else if (command.equals("modify")) {
            if (options.length < 4) {
                return SccpOAMMessage.INVALID_COMMAND;
            }
            int addressId = Integer.parseInt(options[3]);

            int count = 11;

            while (count < options.length) {
                String key = options[count++];
                if (key == null) {
                    return SccpOAMMessage.INVALID_COMMAND;
                }

                if (key.equals("stackname")) {
                    String m3uaStackName = options[count++];

                    SccpStackImpl sccpStaclImpl = this.sccpStacks.get(m3uaStackName);

                    if (sccpStaclImpl == null) {
                        return String.format(SccpOAMMessage.NO_SCCP_MANAGEMENT_BEAN_FOR_NAME, m3uaStackName);
                    }

                    this.sccpStack = sccpStaclImpl;
                } else {
                    return SccpOAMMessage.INVALID_COMMAND;
                }

            }

            this.setDefaultValue();

            SccpAddress address = this.createAddress(options, 4, false);

            this.sccpStack.getRouter().modifyRoutingAddress(addressId, address);
            return String.format(SccpOAMMessage.ADDRESS_SUCCESSFULLY_MODIFIED, this.sccpStack.getName());
        } else if (command.equals("delete")) {
            // sccp address delete <id> stackname <stack-name>

            if (options.length < 4) {
                return SccpOAMMessage.INVALID_COMMAND;
            }
            int addressId = Integer.parseInt(options[3]);

            int count = 4;

            while (count < options.length) {
                String key = options[count++];
                if (key == null) {
                    return SccpOAMMessage.INVALID_COMMAND;
                }

                if (key.equals("stackname")) {
                    String m3uaStackName = options[count++];

                    SccpStackImpl sccpStaclImpl = this.sccpStacks.get(m3uaStackName);

                    if (sccpStaclImpl == null) {
                        return String.format(SccpOAMMessage.NO_SCCP_MANAGEMENT_BEAN_FOR_NAME, m3uaStackName);
                    }

                    this.sccpStack = sccpStaclImpl;
                } else {
                    return SccpOAMMessage.INVALID_COMMAND;
                }

            }

            this.setDefaultValue();

            this.sccpStack.getRouter().removeRoutingAddress(addressId);
            return String.format(SccpOAMMessage.ADDRESS_SUCCESSFULLY_DELETED, this.sccpStack.getName());

        } else if (command.equals("show")) {
            // sccp address show id <id> stackname <stack-name>

            int count = 3;
            int addressId = -1;
            while (count < options.length) {
                String key = options[count++];
                if (key == null) {
                    return SccpOAMMessage.INVALID_COMMAND;
                }

                if (key.equals("stackname")) {
                    String m3uaStackName = options[count++];

                    SccpStackImpl sccpStaclImpl = this.sccpStacks.get(m3uaStackName);

                    if (sccpStaclImpl == null) {
                        return String.format(SccpOAMMessage.NO_SCCP_MANAGEMENT_BEAN_FOR_NAME, m3uaStackName);
                    }

                    this.sccpStack = sccpStaclImpl;
                } else if (key.equals("id")) {
                    addressId = Integer.parseInt(options[count++]);
                } else {
                    return SccpOAMMessage.INVALID_COMMAND;
                }

            }

            this.setDefaultValue();

            if (addressId != -1) {
                SccpAddress pa = this.sccpStack.getRouter().getRoutingAddress(addressId);
                if (pa == null) {
                    return String.format(SccpOAMMessage.ADDRESS_DOESNT_EXIST, this.sccpStack.getName());
                }
                return pa.toString();
            }

            if (this.sccpStack.getRouter().getRoutingAddresses().size() == 0) {
                return String.format(SccpOAMMessage.ADDRESS_DOESNT_EXIST, this.sccpStack.getName());
            }

            StringBuffer sb = new StringBuffer();

            Map<Integer, SccpAddress> idVsPrimAdd = this.sccpStack.getRouter().getRoutingAddresses();
            for (Integer e : idVsPrimAdd.keySet()) {
                SccpAddress address = idVsPrimAdd.get(e);
                sb.append("key=");
                sb.append(e);
                sb.append("  ");
                sb.append(address);
                sb.append("\n");
            }
            return sb.toString();
        }

        return SccpOAMMessage.INVALID_COMMAND;
    }

    public  SccpExecutor();

}