import "@fluencelabs/aqua-lib/builtin.aqua"
import "@fluencelabs/registry/subnetwork.aqua"
import Registry, Record from "@fluencelabs/registry/registry-service.aqua"
import "@fluencelabs/spell/spell_service.aqua"

import "workers.aqua"
import "services.aqua"

export put, get
export put_workers, get_workers, get_and_save_workers

func getWorkers() -> []Record:
  workersInfo <- getWorkersInfo()
  dealId = workersInfo.deals.defaultWorker.dealId
  workersOp <- resolveSubnetwork(dealId)
  <- workersOp!

func put(file_name: string, file_content: []u8) -> string:
  workers <- getWorkers()
  w = workers[0]
  on w.metadata.peer_id via w.metadata.relay_id:
    res <- UrlDownloader.put(file_name, file_content)
  <- res


func get(file_name: string) -> []u8:
  workers <- getWorkers()
  w =  workers[0]
  on w.metadata.peer_id via w.metadata.relay_id:
    res <- UrlDownloader.get(file_name)
  <- res

func get_and_save(url: string, fname: string) -> string:
    workers <- getWorkers()
    w =  workers[0]
    on w.metadata.peer_id via w.metadata.relay_id:
        res = UrlDownloader.get_n_save(url, fname)
    <- res

func put_workers(file_name: string, file_content: []u8) -> string:
  info <- getWorkersInfo()
  spell = info.hosts.defaultWorker.installationSpells[0]
  on spell.workerId via spell.hostId:
    res <- UrlDownloader.put(file_name, file_content)
  <- res

func get_workers(file_name: string) -> []u8:
  info <- getWorkersInfo()
  spell = info.hosts.defaultWorker.installationSpells[0]
  on spell.workerId via spell.hostId:
    res <- UrlDownloader.get(file_name)
  <- res

func get_and_save_workers(url: string, fname: string) -> string:
  info <- getWorkersInfo()
  spell = info.hosts.defaultWorker.installationSpells[0]
  on spell.workerId via spell.hostId:
      res = UrlDownloader.get_n_save(url, fname)
  <- res