import { Dcomm, BN, Buffer } from "dcomm/dist"
import {
  DVMAPI,
  KeyChain as DVMKeyChain,
  UTXOSet,
  UnsignedTx,
  Tx,
  MinterSet
} from "dcomm/dist/apis/dvm"
import { GetUTXOsResponse } from "dcomm/dist/apis/dvm/interfaces"
import {
  PrivateKeyPrefix,
  DefaultLocalGenesisPrivateKey,
  UnixNow
} from "dcomm/dist/utils"

const ip: string = "localhost"
const port: number = 9650
const protocol: string = "http"
const networkID: number = 1337
const dcomm: Dcomm = new Dcomm(ip, port, protocol, networkID)
const astchain: DVMAPI = dcomm.ASTChain()
const xKeychain: DVMKeyChain = astchain.keyChain()
const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}`
xKeychain.importKey(privKey)
const xAddresses: Buffer[] = astchain.keyChain().getAddresses()
const xAddressStrings: string[] = astchain.keyChain().getAddressStrings()
const threshold: number = 1
const locktime: BN = new BN(0)
const asOf: BN = UnixNow()
const memo: Buffer = Buffer.from(
  "DVM utility method buildCreateNFTAssetTx to create an NFT"
)
const name: string = "non fungible token"
const symbol: string = "NFT"

const main = async (): Promise<any> => {
  const dvmUTXOResponse: GetUTXOsResponse = await astchain.getUTXOs(
    xAddressStrings
  )
  const utxoSet: UTXOSet = dvmUTXOResponse.utxos
  const minterSets: MinterSet[] = [new MinterSet(threshold, xAddresses)]
  const unsignedTx: UnsignedTx = await astchain.buildCreateNFTAssetTx(
    utxoSet,
    xAddressStrings,
    xAddressStrings,
    minterSets,
    name,
    symbol,
    memo,
    asOf,
    locktime
  )

  const tx: Tx = unsignedTx.sign(xKeychain)
  const txid: string = await astchain.issueTx(tx)
  console.log(`Success! TXID: ${txid}`)
}

main()
