import config from 'config'
import chainify from '@zap/utils/chainify'
import { mainLog } from '@zap/utils/log'
import TokenBasedBackupService from '../base/TokenBasedBackupService'
import createClient from './db'

export default class BackupService extends TokenBasedBackupService {
  /**
   * Sets up dropbox  service for usage. This method must be called before calling any other methods
   *
   * @param {object} tokens google api compliant token desc
   * `{access_token,expiry_date,refresh_token,scope,token_type}`
   * @returns
   * @memberof BackupService
   */
  async init(tokens) {
    const { redirectUrl, clientId } = config.backup.dropbox
    await super.init(
      createClient.bind(null, {
        clientId,
        authRedirectUrl: redirectUrl,
        tokens,
      })
    )
  }

  /**
   * Saves specified backup
   *
   * @param {string} walletId desired file name
   * @param {Buffer} backup `Buffer` with backup data
   * @returns {string} google drive fileID
   * @memberof BackupService
   */
  saveBackup = chainify(async ({ walletId, backup }) => {
    const { connection } = this
    if (connection) {
      const { id } = await connection.uploadFromBuffer(`/${walletId}`, backup)
      return id
    } else {
      mainLog.warn('Attempting to call saveBackup in logged-out state')
    }
  })

  /**
   * Provider name
   *
   * @readonly
   * @memberof BackupService
   */
  get name() {
    return 'dropbox'
  }
}

@@ -792,16 +792,276 @@
  )%0A  %7D%0A%0A
+  /**%0A   * Loads backup for the specified wallet%0A   *%0A   * @param %7Bstring%7D walletId%0A   * @returns %7BBuffer%7D wallet backup as a %60Buffer%60%0A   * @memberof BackupService%0A   */%0A  async loadBackup(%7B walletId %7D) %7B%0A    return await super.loadBackup(%60/$%7BwalletId%7D%60)%0A  %7D%0A%0A
   /**%0A  

implement dropbox loadBackup