var assert = require('assert')
var scripts = require('../src/scripts')

var Address = require('../src/address')
var ECKey = require('../src/eckey')
var Transaction = require('../src/transaction')
var Script = require('../src/script')

var fixtures = require('./fixtures/transaction')

// FIXME: what is a better way to do this, seems a bit odd
fixtures.valid.forEach(function(f) {
  var Script = require('../src/script')

  f.raw.ins.forEach(function(fin) {
    fin.hash = new Buffer(fin.hash, 'hex')
    fin.script = Script.fromHex(fin.script)
  })

  f.raw.outs.forEach(function(fout) {
    fout.script = Script.fromHex(fout.script)
  })
})

describe('Transaction', function() {
  describe('fromBuffer/fromHex', function() {
    fixtures.valid.forEach(function(f) {
      it('imports ' + f.txid + ' correctly', function() {
        var actual = Transaction.fromHex(f.hex)

        assert.deepEqual(actual, f.raw)
      })
    })

    fixtures.invalid.fromBuffer.forEach(function(f) {
      it('throws on ' + f.exception, function() {
        assert.throws(function() {
          Transaction.fromHex(f.hex)
        }, new RegExp(f.exception))
      })
    })
  })

  describe('toBuffer/toHex', function() {
    fixtures.valid.forEach(function(f) {
      it('exports ' + f.txid + ' correctly', function() {
        var actual = Transaction.prototype.toBuffer.call(f.raw)

        assert.equal(actual.toString('hex'), f.hex)
      })
    })
  })

  describe('addInput', function() {
    // FIXME: not as pretty as could be
    // Probably a bit representative of the API
    var prevTxHash, prevTxId, prevTx
    beforeEach(function() {
      var f = fixtures.valid[0]
      prevTx = Transaction.fromHex(f.hex)
      prevTxHash = prevTx.getHash()
      prevTxId = prevTx.getId()
    })

    it('accepts a transaction id', function() {
      var tx = new Transaction()
      tx.addInput(prevTxId, 0)

      assert.deepEqual(tx.ins[0].hash, prevTxHash)
    })

    it('accepts a transaction hash', function() {
      var tx = new Transaction()
      tx.addInput(prevTxHash, 0)

      assert.deepEqual(tx.ins[0].hash, prevTxHash)
    })

    it('accepts a Transaction object', function() {
      var tx = new Transaction()
      tx.addInput(prevTx, 0)

      assert.deepEqual(tx.ins[0].hash, prevTxHash)
    })

    it('returns an index', function() {
      var tx = new Transaction()
      assert.equal(tx.addInput(prevTxHash, 0), 0)
      assert.equal(tx.addInput(prevTxHash, 0), 1)
    })

    it('defaults to DEFAULT_SEQUENCE', function() {
      var tx = new Transaction()
      tx.addInput(prevTxHash, 0)

      assert.equal(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE)
    })

    it('defaults to empty script', function() {
      var tx = new Transaction()
      tx.addInput(prevTxHash, 0)

      assert.equal(tx.ins[0].script, Script.EMPTY)
    })

    fixtures.valid.forEach(function(f) {
      it('should add the inputs for ' + f.txid + ' correctly', function() {
        var tx = new Transaction()

        f.raw.ins.forEach(function(txIn, i) {
          var script = txIn.script ? Script.fromHex(txIn.script) : undefined
          var j = tx.addInput(txIn.hash, txIn.index, txIn.sequence, script)

          assert.equal(i, j)
          assert.deepEqual(tx.ins[i].hash, txIn.hash)
          assert.equal(tx.ins[i].index, txIn.index)

          var sequence = txIn.sequence
          if (sequence === undefined) sequence = Transaction.DEFAULT_SEQUENCE

          assert.equal(tx.ins[i].sequence, sequence)
          assert.equal(tx.ins[i].script, script || Script.EMPTY)
        })
      })
    })

    fixtures.invalid.addInput.forEach(function(f) {
      it('throws on ' + f.exception, function() {
        var tx = new Transaction()
        var hash = new Buffer(f.hash, 'hex')

        assert.throws(function() {
          tx.addInput(hash, f.index)
        }, new RegExp(f.exception))
      })
    })
  })

  describe('addOutput', function() {
    // FIXME: not as pretty as could be
    // Probably a bit representative of the API
    var destAddressB58, destAddress, destScript
    beforeEach(function() {
      destAddressB58 = '15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3'
      destAddress = Address.fromBase58Check(destAddressB58)
      destScript = destAddress.toOutputScript()
    })

    it('accepts an address string', function() {
      var tx = new Transaction()
      tx.addOutput(destAddressB58, 40000)

      assert.deepEqual(tx.outs[0].script, destScript)
      assert.equal(tx.outs[0].value, 40000)
    })

    it('accepts an Address', function() {
      var tx = new Transaction()
      tx.addOutput(destAddress, 40000)

      assert.deepEqual(tx.outs[0].script, destScript)
      assert.equal(tx.outs[0].value, 40000)
    })

    it('accepts a scriptPubKey', function() {
      var tx = new Transaction()
      tx.addOutput(destScript, 40000)

      assert.deepEqual(tx.outs[0].script, destScript)
      assert.equal(tx.outs[0].value, 40000)
    })

    it('returns an index', function() {
      var tx = new Transaction()
      assert.equal(tx.addOutput(destScript, 40000), 0)
      assert.equal(tx.addOutput(destScript, 40000), 1)
    })

    fixtures.valid.forEach(function(f) {
      it('should add the outputs for ' + f.txid + ' correctly', function() {
        var tx = new Transaction()

        f.raw.outs.forEach(function(txOut, i) {
          var j = tx.addOutput(txOut.script, txOut.value)

          assert.equal(i, j)
        })

        assert.deepEqual(tx.outs, f.raw.outs)
      })
    })
  })

  describe('clone', function() {
    fixtures.valid.forEach(function(f) {
      var expected = Transaction.fromHex(f.hex)
      var actual = expected.clone()

      it('should have value equality', function() {
        assert.deepEqual(actual, expected)
      })

      it('should not have reference equality', function() {
        assert.notEqual(actual, expected)
      })
    })
  })

  describe('getId', function() {
    fixtures.valid.forEach(function(f) {
      it('should return the txid for ' + f.txid, function() {
        var tx = Transaction.fromHex(f.hex)
        var actual = tx.getId()

        assert.equal(actual, f.txid)
      })
    })
  })

  describe('getHash', function() {
    fixtures.valid.forEach(function(f) {
      it('should return the hash for ' + f.txid, function() {
        var tx = Transaction.fromHex(f.hex)
        var actual = tx.getHash().toString('hex')

        assert.equal(actual, f.hash)
      })
    })
  })

  // TODO:
  //  hashForSignature: [Function],

  // FIXME: remove in 2.x.y
  describe('signInput/validateInput', function() {
    it('works for multi-sig redeem script', function() {
      var tx = new Transaction()
      tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0)
      tx.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1)

      var privKeys = [
        '5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf',
        '5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAvUcVfH'
      ].map(function(wif) {
        return ECKey.fromWIF(wif)
      })
      var pubKeys = privKeys.map(function(eck) { return eck.pub })
      var redeemScript = scripts.multisigOutput(2, pubKeys)

      var signatures = privKeys.map(function(privKey) {
        return tx.signInput(0, redeemScript, privKey)
      })

      var redeemScriptSig = scripts.multisigInput(signatures)
      var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
      tx.setInputScript(0, scriptSig)

      signatures.forEach(function(sig, i){
        assert(tx.validateInput(0, redeemScript, privKeys[i].pub, sig))
      })

      var expected = '010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d600000000fd1b0100483045022100e5be20d440b2bbbc886161f9095fa6d0bca749a4e41d30064f30eb97adc7a1f5022061af132890d8e4e90fedff5e9365aeeb77021afd8ef1d5c114d575512e9a130a0147304402205054e38e9d7b5c10481b6b4991fde5704cd94d49e344406e3c2ce4d18a43bf8e022051d7ba8479865b53a48bee0cce86e89a25633af5b2918aa276859489e232f51c014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0101000000000000001976a914751e76e8199196d454941c45d1b3a323f1433bd688ac00000000'
      assert.equal(tx.toHex(), expected)
    })
  })
})