module Singly
  class Account
    attr_reader :access_token

    def initialize(access_token, account=nil)
      @access_token = access_token
      @account = account
    end

    def account_id
      @account ||= profiles.fetch["id"]
    end

    def profile
      Singly::Profile.new(access_token: access_token)
    end

    def profiles(params={})
      Singly::Profiles.new(params.merge(access_token: access_token))
    end

    def types
      Singly::Types.new(access_token: access_token)
    end

    def services
      Singly::Services.new(access_token: access_token)
    end

    def friends
      Singly::Friends.new(access_token: access_token)
    end

    def id(id, params={})
      Singly::Id.new(params.merge(access_token: access_token, id: id))
    end

    def proxy
      Singly::Proxy.new(access_token)
    end

    # Alternative to calling #profiles.<service>.delete.
    # Useful if you don't know the exact service at runtime.
    def delete(service, id=nil)
      endpoint = Singly::Profiles::Service.new(access_token: access_token)
      endpoint.service = service
      endpoint.delete(id)
    end

    # Shortcut to Singly.auth.merge(source: <source>, dest: <dest>)
    # where <dest> is always this account's access_token.
    def merge(source)
      Singly.auth.merge(source: source, dest: access_token)
    end

    # Shortcut to Singly.auth.<service>.apply(account: <account_id>, token: <token> [, token_secret: <token_secret>])
    # where <account_id> is always this account's account_id.
    # If the account_id is not present, it is fetched from the api.
    def apply(service, params)
      Singly.auth.send(service).apply(params.merge(account: account_id))
    end
  end
end