const Client = require('./Client')

/**
 * Instance class
 */
class InstanceCLI extends Client {
  instanceBasePath = () => `v1/instances/${this.instance}`
  preauthedBasePath = () => 'v1/preauthed'

  /**
   * Upgrade iOS version on instance
   * @returns {Promise<any>}
   */
  upgrade = async (options) => {
    const { os, osbuild } = options
    return await this._fetch('POST', `${this.instanceBasePath()}/upgrade`, { os, osbuild })
  }

  /**
   * Get websocket stream from token
   * @returns {wsstream.WebSocketDuplex}
   */
  wss = (token) => {
    return this._fetchWSS(token)
  }

  /**
   * Start instance
   */
  start = async (options) => {
    const { sockcap, paused } = options
    return await this._fetch('POST', `${this.instanceBasePath()}/start`, { sockcap, paused })
  }

  /**
   * Restore backup to instance
   * @returns {Promise<any>}
   */
  restoreBackup = async (password) => {
    return await this._fetch('POST', `${this.instanceBasePath()}/restoreBackup`, { password })
  }

  /**
   * Enable monitoring
   * @returns {Promise<any>}
   */
  enableMonitoring = async (options) => {
    return await this._fetch('POST', `${this.instanceBasePath()}/netdump/enable`, options)
  }

  /**
   * Disable monitoring
   * @returns {Promise<any>}
   */
  disableMonitoring = async () => {
    return await this._fetch('POST', `${this.instanceBasePath()}/netdump/disable`)
  }

  /**
   * Download file
   * @returns {Promise<any>}
   */
  download = async (file, outputFile) => {
    return await this._fetchStream(`${this.instanceBasePath()}/${file}`, outputFile)
  }

  /**
   * Fetch instance information
   * @returns {Promise<any>}
   */
  info = async () => {
    return await this._fetch('GET', `${this.instanceBasePath()}`, {})
  }

  input = async (data) => {
    return await this._fetch('POST', `${this.instanceBasePath()}/input`, data)
  }
}

module.exports = InstanceCLI
