import { createIO } from 'impure'

import { loadCustomSong } from '../CustomSongs'
import DndResources from '../../resources/dnd-resources'
import {
  downloadFileEntryFromURL,
  CustomSongResources,
} from '../../resources/custom-song-resources'
import { getIPFSResources } from '../../resources/ipfs-resources'

export function handleCustomSongFolderDrop(event) {
  return createIO(async ({ store, customSongLoader }) => {
    const resources = new DndResources(event)
    const initialLog = ['Examining dropped items...']
    return loadCustomSong(resources, initialLog, { store, customSongLoader })
  })
}

export function handleCustomSongURLLoad(url) {
  return createIO(async ({ store, customSongLoader }) => {
    const resources = new CustomSongResources({
      getFiles: async (log) => [await downloadFileEntryFromURL(url, log)],
    })
    const initialLog = ['Loading from ' + url]
    return loadCustomSong(resources, initialLog, { store, customSongLoader })
  })
}

export function handleClipboardPaste(e) {
  return createIO(async ({ store, customSongLoader }) => {
    let match
    const text = e.clipboardData.getData('text/plain')
    match = text.match(/(https?:\/\/[a-zA-Z0-9:.-]+)?(\/ipfs\/\S+)/)
    if (match) {
      const gateway = match[1] || undefined
      const path = gateway ? decodeURI(match[2]) : match[2]
      const resources = getIPFSResources(path, gateway)
      const initialLog = [
        'Loading from IPFS path ' + path + '...',
        `(Using ${resources.gatewayName})`,
      ]
      if (/^http:/.test(gateway) && window.location.protocol === 'https:') {
        initialLog.push(
          store,
          'WARNING: Loading http URL from https. This will likely fail!'
        )
      }
      return loadCustomSong(resources, initialLog, { store, customSongLoader })
    }
  })
}

@@ -305,16 +305,68 @@
 sources'
+%0Aimport %7B URLResources %7D from 'bemuse/resources/url'
 %0A%0Aexport
@@ -1130,22 +1130,8 @@
 %3E %7B%0A
-    let match%0A
     
@@ -1185,16 +1185,30 @@
 n')%0A    
+%7B%0A      const 
 match = 
@@ -1264,24 +1264,26 @@
 /%5CS+)/)%0A    
+  
 if (match) %7B
@@ -1279,24 +1279,26 @@
 f (match) %7B%0A
+  
       const 
@@ -1329,16 +1329,18 @@
 defined%0A
+  
       co
@@ -1393,24 +1393,26 @@
 ch%5B2%5D%0A      
+  
 const resour
@@ -1451,24 +1451,26 @@
 eway)%0A      
+  
 const initia
@@ -1486,16 +1486,18 @@
         
+ 
+ 
 'Loading
@@ -1538,16 +1538,18 @@
         
+  
 %60(Using 
@@ -1576,16 +1576,18 @@
 ame%7D)%60,%0A
+  
       %5D%0A
@@ -1588,24 +1588,26 @@
     %5D%0A      
+  
 if (/%5Ehttp:/
@@ -1673,16 +1673,18 @@
         
+  
 initialL
@@ -1706,15 +1706,19 @@
     
+ 
+ 
 store,%0A
+  
     
@@ -1798,18 +1798,22 @@
     
+ 
+ 
 )%0A
+  
       %7D%0A
     
@@ -1808,16 +1808,18 @@
       %7D%0A
+  
       re
@@ -1854,39 +1854,59 @@
 s, initialLog, %7B
- store,
+%0A          store,%0A         
  customSongLoade
@@ -1906,24 +1906,33 @@
 ngLoader
+,%0A       
  %7D)%0A    
 %7D%0A  %7D)%0A%7D
@@ -1927,13 +1927,556 @@
     
+  
 %7D%0A  
-%7D
+    %7B%0A        const match = text.match(%0A          /https?:%5C/%5C/%5Ba-zA-Z0-9:.-%5D+%5C/%5CS+%5C/bemuse-song%5C.json/%0A        )%0A        if (match) %7B%0A          const url = match%5B0%5D%0A          const initialLog = %5B'Loading prepared song...'%5D%0A          const resources = new PreparedSongResources(new URL(url))%0A          return loadCustomSong(resources, initialLog, %7B%0A            store,%0A            customSongLoader,%0A          %7D)%0A        %7D%0A      %7D%0A    %7D%0A  %7D)%0A%7D%0A%0Aclass PreparedSongResources extends URLResources %7B%0A  fileList = Promise.resolve(%5B'bemuse-song.json'%5D
 )%0A%7D%0A

Allow pasting URL to bemuse-song.json