require "devops-client/options/common_options"

class ServerOptions < CommonOptions

  commands :add, :bootstrap, :create, :delete, :list, :pause, :reserve, :show, :unpause, :unreserve, :unbootstrap, :delete_list, :add_and_bootstrap_list

  COMMAND_NAME = "server"

  def initialize args, def_options
    super(args, def_options)
    self.header = I18n.t("headers.server")
    self.list_params = ["[PROVIDER]"]
    self.create_params = ["PROJECT", "ENVIRONMENT", "CATEGORY"]
    server_id = ["SERVER_ID"]
    self.delete_list_params = ["SERVER_ID..."]
    self.delete_params = server_id
    self.show_params = server_id
    self.pause_params = server_id
    self.unpause_params = server_id
    self.reserve_params = server_id
    self.unreserve_params = server_id
    self.bootstrap_params = server_id
    self.unbootstrap_params = server_id
    self.add_params = ["PROJECT", "ENVIRONMENT", "CATEGORY", "IP", "SSH_USER", "KEY_ID", "NAME"]
    self.add_and_bootstrap_list_params = ["PROJECT_ID", "DEPLOY_ENV",  "SSH_USER", "KEY_ID", "IPS_FILE"]
  end

  def delete_options
    options do |parser, options|
      parser.banner << self.delete_banner
      parser.resource_name = :server
      parser.command_name = :delete

      parser.recognize_option_value(:no_ask,
        type: :switch,
        default: false,
        switch_value: true
      )
    end
  end

  def create_options
    options do |parser, options|
      parser.banner << self.create_banner
      parser.resource_name = :server
      parser.command_name = :create

      parser.recognize_option_value('without_bootstrap',
        type: :switch,
        switch_value: true,
        option_key: :without_bootstrap,
        i18n_scope: 'create'
      )

      parser.recognize_option_value(:name, short: '-N')
      parser.recognize_option_value('skip_rollback',
        type: :switch,
        switch_value: true,
        option_key: :skip_rollback,
        i18n_scope: 'create'
      )
      parser.recognize_option_value(:ssh_key)

=begin
      parser.recognize_option_value(:groups,
        short: '-G',
        variable: 'GROUP_1,GROUP_2...'
      ) do |groups|
        options[:groups] = groups.split(",")
      end
=end

      parser.recognize_option_value(:private_ip, i18n_scope: 'create')

      # it was disabled somewhy
      # parser.on('--public-ip', "Associate public IP with server") do
      #   options[:public_ip] = true
      # end
    end
  end

  def bootstrap_options
    options do |parser, options|
      parser.banner << self.bootstrap_banner
      parser.resource_name = :server
      parser.command_name = :bootstrap

      parser.recognize_option_value(:cm_name, short: '-N')
      parser.recognize_option_value(:bootstrap_template)
      parser.recognize_option_value(:run_list) do |list|
        options[:run_list] = list.split(",")
      end
    end
  end

  def add_options
    options do |parser, options|
      parser.banner << self.add_banner

      parser.recognize_option_value('public-ip', resource_name: :server, command_name: :add, option_key: :public_ip)
    end
  end

  def add_and_bootstrap_list_options
    options do |parser, options|
      parser.banner << self.add_and_bootstrap_list_banner
      parser.separator ''
      parser.separator "\tIPS_FILE is path to a file with list of ip:node_name pairs; example of such file:"
      parser.separator "\type127.0.0.1:node1"
      parser.separator "\type127.0.0.2:node2"
    end
  end

end
