require "devops-client/options/common_options"
require "set"

class EnvironmentOptions < CommonOptions

  commands :create, :delete, :deploy, :list, {:servers => [:list, :delete]}, :stacks, {:set => [:run_list]}, :show, :update, {:user => [:add, :delete]}

  COMMAND_NAME = "env"

  def initialize args, def_options
    super(args, def_options)
    self.header = I18n.t("headers.environment")
    id = "PROJECT_ID"
    env = "ENVIRONMENT"
    self.list_params = [id]
    self.show_params = [id, env]
    self.create_params = [id, env]
    self.delete_params = [id, env]
    self.deploy_params = [id, env]
    self.set_run_list_params = [id, env, "[RUN_LIST...]"]
    self.servers_list_params = [id, env]
    #self.stacks_params = [id, "[#{env}]", "[#{cat}]"]
    self.update_params = [id, env, "FILE"]
    self.user_add_params = [id, env]
    self.user_delete_params = [id, env, "USER..."]
    #self.delete_servers_params = [id, "[#{env}]"]
  end

  def create_options
    self.options do |parser, options|
      parser.banner << self.create_banner
      parser.resource_name = :project

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

      parser.recognize_option_value(:file) do |file|
        abort("File '#{file}' does not exist") unless File.exist?(file)
        options[:file] = file
      end

      parser.recognize_option_value(:subnets, variable: 'SUBNET_1,SUBNET_2...') do |subnets|
        options[:subnets] = subnets.split(",")
      end

      parser.recognize_option_value(:users, variable: 'USER_1,USER_2...') do |users|
        options[:users] = Set.new(users.split(","))
      end

      parser.recognize_option_value(:run_list, variable: 'ROLE_1,ROLE_2...') do |run_list|
        options[:run_list] = Set.new(run_list.split(",")).to_a
      end

      parser.recognize_option_value(:deploy_env, option_key: :identifier)
      parser.recognize_option_value(:flavor)
      parser.recognize_option_value(:image)
      parser.recognize_option_value(:run_list)
      parser.recognize_option_value(:provider)
      parser.recognize_option_value(:no_expires, type: :switch, switch_value: true, default: false)
      parser.recognize_option_value(:expires)


      # TODO:
      # support short options names
      #
      # options[:file] = nil
      # parser.on("-f", "--file FILE", I18n.t("options.project.create.file")) do |file|
      #   abort("File '#{file}' does not exist") unless File.exist?(file)
      #   options[:file] = file
      # end

    end
  end

  def user_add_options
    self.options do |parser, options|
      parser.banner << self.user_add_banner

      parser.recognize_option_value(:deploy_env, resource_name: :project, command_name: 'user_add')
    end
  end

  def user_delete_options
    self.options do |parser, options|
      parser.banner << self.user_delete_banner

      parser.recognize_option_value(:deploy_env, resource_name: :project, command_name: 'user_delete')
    end
  end

  def deploy_options
    options do |parser, options|
      parser.banner << self.deploy_banner

      parser.recognize_option_value(:servers, resource_name: :project, command_name: 'deploy') do |servers|
        options[:servers] = servers.split(",")
      end
    end
  end

  def delete_servers_options
    self.options do |parser, options|
      parser.recognize_option_value(:dry_run, resource_name: :project, type: :switch, default: false, switch_value: true, command_name: 'delete_servers')
    end
  end

end
