require 'nugrant'
require 'nugrant/vagrant/v2/command/env'
require 'nugrant/vagrant/v2/command/parameters'
require 'nugrant/vagrant/v2/command/restricted_keys'
require 'nugrant/version'

module Nugrant
  module Vagrant
    module V2
      module Command
        class Root < ::Vagrant.plugin("2", :command)
          def self.synopsis
            "manage Nugrant user defined parameters (config.user)"
          end

          def initialize(arguments, environment)
            super(arguments, environment)

            @arguments, @subcommand, @subarguments = split_main_and_subcommand(arguments)

            # Change super class available arguments to main ones only
            @argv = @arguments

            @subcommands = ::Vagrant::Registry.new()
            @subcommands.register(:env) do
              Command::Env
            end

            @subcommands.register(:parameters) do
              Command::Parameters
            end

            @subcommands.register(:'restricted-keys') do
              Command::RestrictedKeys
            end

            @show_help = false
            @show_version = false
          end

          def create_parser()
            return OptionParser.new do |parser|
              parser.banner = "Usage: vagrant user [-h] [-v] <subcommand> [<args>]"

              parser.separator ""
              parser.on("-h", "--help", "Prints this help") do
                @show_help = true
              end

              parser.on("-v", "--version", "Prints plugin version and exit.") do
                @show_version = true
              end

              parser.separator ""
              parser.separator "Available subcommands:"

              keys = []
              @subcommands.each { |key, value| keys << key.to_s }

              keys.sort.each do |key|
                parser.separator "     #{key}"
              end

              parser.separator ""
              parser.separator "For help on any individual command run `vagrant user <subcommand> -h`"
            end
          end

          def execute
            parser = create_parser()
            arguments = parse_options(parser)

            return version() if @show_version
            return help(parser) if @show_help

            command_class = @subcommands.get(@subcommand.to_sym) if @subcommand
            return help(parser) if !command_class || !@subcommand

            @logger.debug("Invoking nugrant command class: #{command_class} #{@subarguments.inspect}")

            command_class.new(@subarguments, @env).execute
          end

          def help(parser)
            @env.ui.info(parser.help, :prefix => false)
          end

          def version()
            @env.ui.info("Nugrant version #{Nugrant::VERSION}", :prefix => false)
          end
        end
      end
    end
  end
end
