class Envirobly::AccessToken

def as_http_bearer

def as_http_bearer
  "Bearer #{@token}"
end

def destroy

def destroy
  if File.exist?(path)
    FileUtils.rm path
  end
end

def dir

def dir
  if ENV["XDG_CONFIG_HOME"]
    Pathname.new(ENV["XDG_CONFIG_HOME"]).join("envirobly")
  else
    Pathname.new(Dir.home).join(".envirobly")
  end
end

def initialize(token = ENV["ENVIROBLY_ACCESS_TOKEN"].presence, shell: nil)

def initialize(token = ENV["ENVIROBLY_ACCESS_TOKEN"].presence, shell: nil)
  @shell = shell
  if token.blank? && File.exist?(self.class.path)
    @token = File.read(self.class.path)
  else
    @token = token
  end
end

def path

def path
  dir.join "access_token"
end

def require!

def require!
  return if @token.present?
  shell.say "This action requires you to be signed in."
  shell.say "Please visit https://on.envirobly.com/profile/access_tokens"
  shell.say "to generate an access token and then paste it in here."
  shell.say
  set
end

def save

def save
  FileUtils.mkdir_p self.class.dir
  File.write self.class.path, @token
  File.chmod 0600, self.class.path
end

def set

def set
  @token = nil
  while @token.blank?
    begin
      @token = shell.ask("Access Token:", echo: false)
    rescue Interrupt
      shell.say
      shell.say_error "Cancelled"
      exit
    end
    api = Envirobly::Api.new(access_token: self)
    # TODO: Eventually replace with custom `whoami` API that returns name, email...
    if api.list_accounts.success?
      save
      shell.say
      shell.say "Successfully signed in "
      shell.say green_check
    else
      shell.say
      shell.say_error "This token is invalid. Please try again"
      @token = nil
    end
  end
end