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

class ProjectOptions < CommonOptions

  commands :create, :delete, :list, {:set => [:run_list]}, :show, :update, {:user => [:add, :delete]}, :archive, :unarchive

  COMMAND_NAME = "project"

  def initialize args, def_options
    super(args, def_options)
    self.header = I18n.t("headers.project")
    id = "PROJECT_ID"
    env = "ENVIRONMENT"
    cat = "CATEGORY"
    self.list_params = []
    self.show_params = [id]
    self.archive_params = [id]
    self.unarchive_params = [id]
    self.create_params = [id]
    self.delete_params = [id]
    self.set_run_list_params = [id, "[RUN_LIST...]"]
    self.update_params = [id, "FILE"]
    self.user_add_params = [id, "USER_NAME..."]
    self.user_delete_params = [id, "USER_NAME..."]
  end

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

      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(:description) do |description|
        options[:description] = description
      end

      parser.recognize_option_value(:project_users, variable: 'USER_1,USER_2...') do |users|
        options[:users] = Set.new(users.split(",")).to_a
      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

      # 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

end
