const Uppy = require('@uppy/core/src')
const Dashboard = require('@uppy/dashboard/src')
const Instagram = require('@uppy/instagram/src')
const Facebook = require('@uppy/facebook/src')
const OneDrive = require('@uppy/onedrive/src')
const Dropbox = require('@uppy/dropbox/src')
const Box = require('@uppy/box/src')
const GoogleDrive = require('@uppy/google-drive/src')
const Unsplash = require('@uppy/unsplash/src')
const Zoom = require('@uppy/zoom/src')
const Url = require('@uppy/url/src')
const Webcam = require('@uppy/webcam/src')
const ScreenCapture = require('@uppy/screen-capture/src')
const GoldenRetriever = require('@uppy/golden-retriever/src')
const Tus = require('@uppy/tus/src')
const AwsS3 = require('@uppy/aws-s3/src')
const AwsS3Multipart = require('@uppy/aws-s3-multipart/src')
const XHRUpload = require('@uppy/xhr-upload/src')
const Transloadit = require('@uppy/transloadit/src')
const Form = require('@uppy/form/src')
const ImageEditor = require('@uppy/image-editor/src')

// DEV CONFIG: pick an uploader

const UPLOADER = 'tus'
// const UPLOADER = 's3'
// const UPLOADER = 's3-multipart'
// const UPLOADER = 'xhr'
// const UPLOADER = 'transloadit'

// DEV CONFIG: Endpoint URLs

const COMPANION_URL = 'http://localhost:3020'
const TUS_ENDPOINT = 'https://tusd.tusdemo.net/files/'
const XHR_ENDPOINT = 'https://xhr-server.herokuapp.com/upload'

// DEV CONFIG: Transloadit keys

const TRANSLOADIT_KEY = '...'
const TRANSLOADIT_TEMPLATE = '...'

// DEV CONFIG: enable or disable Golden Retriever

const RESTORE = false

// Rest is implementation! Obviously edit as necessary...

module.exports = () => {
  const uppyDashboard = new Uppy({
    logger: Uppy.debugLogger,
    meta: {
      username: 'John',
      license: 'Creative Commons'
    }
  })
    .use(Dashboard, {
      trigger: '#pick-files',
      // inline: true,
      target: '.foo',
      metaFields: [
        { id: 'license', name: 'License', placeholder: 'specify license' },
        { id: 'caption', name: 'Caption', placeholder: 'add caption' }
      ],
      showProgressDetails: true,
      proudlyDisplayPoweredByUppy: true,
      note: '2 files, images and video only'
    })
    .use(GoogleDrive, { target: Dashboard, companionUrl: COMPANION_URL })
    .use(Instagram, { target: Dashboard, companionUrl: COMPANION_URL })
    .use(Dropbox, { target: Dashboard, companionUrl: COMPANION_URL })
    .use(Box, { target: Dashboard, companionUrl: COMPANION_URL })
    .use(Facebook, { target: Dashboard, companionUrl: COMPANION_URL })
    .use(OneDrive, { target: Dashboard, companionUrl: COMPANION_URL })
    .use(Zoom, { target: Dashboard, companionUrl: COMPANION_URL })
    .use(Url, { target: Dashboard, companionUrl: COMPANION_URL })
    .use(Unsplash, { target: Dashboard, companionUrl: COMPANION_URL })
    .use(Webcam, {
      target: Dashboard,
      showVideoSourceDropdown: true,
      showRecordingLength: true
    })
    .use(ScreenCapture, { target: Dashboard })
    .use(Form, { target: '#upload-form' })
    .use(ImageEditor, { target: Dashboard })

  switch (UPLOADER) {
    case 'tus':
      uppyDashboard.use(Tus, { endpoint: TUS_ENDPOINT, limit: 6 })
      break
    case 's3':
      uppyDashboard.use(AwsS3, { companionUrl: COMPANION_URL, limit: 6 })
      break
    case 's3-multipart':
      uppyDashboard.use(AwsS3Multipart, { companionUrl: COMPANION_URL, limit: 6 })
      break
    case 'xhr':
      uppyDashboard.use(XHRUpload, { endpoint: XHR_ENDPOINT, limit: 6, bundle: true })
      break
    case 'transloadit':
      uppyDashboard.use(Transloadit, {
        params: {
          auth: { key: TRANSLOADIT_KEY },
          template_id: TRANSLOADIT_TEMPLATE
        }
      })
      break
  }

  if (RESTORE) {
    uppyDashboard.use(GoldenRetriever, { serviceWorker: true })
  }

  window.uppy = uppyDashboard

  uppyDashboard.on('complete', (result) => {
    if (result.failed.length === 0) {
      console.log('Upload successful 😀')
    } else {
      console.warn('Upload failed 😞')
    }
    console.log('successful files:', result.successful)
    console.log('failed files:', result.failed)
    if (UPLOADER === 'transloadit') {
      console.log('Transloadit result:', result.transloadit)
    }
  })

  const modalTrigger = document.querySelector('#pick-files')
  if (modalTrigger) modalTrigger.click()
}

@@ -1158,16 +1158,91 @@
 sloadit'
+%0A// const UPLOADER = 'transloadit-s3'%0A// const UPLOADER = 'transloadit-xhr'
 %0A%0A// DEV
@@ -3628,24 +3628,55 @@
 nsloadit, %7B%0A
+        waitForEncoding: true,%0A
         para
@@ -3798,16 +3798,741 @@
   break%0A
+    case 'transloadit-s3':%0A      uppyDashboard.use(AwsS3, %7B companionUrl: COMPANION_URL %7D)%0A      uppyDashboard.use(Transloadit, %7B%0A        waitForEncoding: true,%0A        importFromUploadURLs: true,%0A        params: %7B%0A          auth: %7B key: TRANSLOADIT_KEY %7D,%0A          template_id: TRANSLOADIT_TEMPLATE%0A        %7D%0A      %7D)%0A      break%0A    case 'transloadit-xhr':%0A      uppyDashboard.setMeta(%7B%0A        params: JSON.stringify(%7B%0A          auth: %7B key: TRANSLOADIT_KEY %7D,%0A          template_id: TRANSLOADIT_TEMPLATE%0A        %7D)%0A      %7D)%0A      uppyDashboard.use(XHRUpload, %7B%0A        method: 'POST',%0A        endpoint: 'https://api2.transloadit.com/assemblies',%0A        metaFields: %5B'params'%5D,%0A        bundle: true%0A      %7D)%0A      break%0A
   %7D%0A%0A  i

add transloadit+S3, transloadit+XHR test uploaders