var _ = require('lodash')
var request = require('request')
var verify = require('cc-wallet-core').verify

var errors = require('./errors')
var cwpp = require('./cwpp')


/**
 * @class PaymentRequestModel
 * @param {external:cc-wallet-core.Wallet} wallet
 * @param {external:cc-wallet-core.AssetDefinition} assetdef
 * @param {Object} props
 * @param {number} props.amount
 * @param {string} [props.address]
 * @param {string} [props.cwpp_host]
 */
function PaymentRequestModel(wallet, assetdef, props) {
  verify.Wallet(wallet)
  verify.AssetDefinition(assetdef)
  verify.object(props)
  // props.amount verified in parseValue
  if (!_.isUndefined(props.address)) { verify.string(props.address) }
  if (!_.isUndefined(props.cwpp_host)) { verify.string(props.cwpp_host) }

  this.paymentURI = null

  this.wallet = wallet
  this.assetdef = assetdef
  this.props = props

  if (_.isUndefined(props.address)) {
    props.address = wallet.getSomeAddress(assetdef, false)
  } else {
    // need uncolored address
    props.address = wallet.getBitcoinAddress(assetdef, props.address)
  }

  if (_.isUndefined(props.cwpp_host)) {
    props.cwpp_host = 'cwpp.chromapass.net'
  }

  var value = assetdef.parseValue(props.amount)
  this.cwppPayReq = cwpp.make_cinputs_payment_request(
    value, props.address, assetdef.getId(), assetdef.getColorSet().colorDescs[0])
}

/**
 * @callback PaymentRequestModel~getPaymentURICallback
 * @param {?Error} error
 * @param {string} uri
 */

/**
 * @param {PaymentRequestModel~getPaymentURICallback} cb
 */
PaymentRequestModel.prototype.getPaymentURI = function (cb) {
  var self = this

  if (self.paymentURI) { return cb(null, self.paymentURI) }

  var requestOpts = {
    method: 'POST',
    uri: 'http://' + self.props.cwpp_host + '/cwpp/new-request',
    body: JSON.stringify(self.cwppPayReq)
  }

  request(requestOpts, function (error, response, body) {
    if (error) {
      return cb(error)
    }

    if (response.statusCode !== 200) {
      return cb(new errors.RequestError('PaymentRequestModel: ' + response.statusMessage))
    }

    var result = JSON.parse(body)
    self.paymentURI = cwpp.make_cwpp_uri(self.props.cwpp_host, result.hash)

    return cb(null, self.paymentURI)
  })
}

module.exports = PaymentRequestModel

@@ -52,16 +52,37 @@
 quest')%0A
+var Q = require('q')%0A
 var veri
@@ -630,16 +630,25 @@
 fied in 
+assetdef.
 parseVal
@@ -1622,16 +1622,39 @@
  (cb) %7B%0A
+  verify.function(cb)%0A%0A
   var se
@@ -1663,17 +1663,16 @@
  = this%0A
-%0A
   if (se
@@ -1688,48 +1688,56 @@
 tURI
-) %7B return cb(null, self.paymentURI) %7D%0A%0A
+ === null) %7B%0A    // @todo Try add %60json:true%60%0A  
   va
@@ -1758,16 +1758,18 @@
 = %7B%0A    
+  
 method: 
@@ -1776,16 +1776,18 @@
 'POST',%0A
+  
     uri:
@@ -1847,16 +1847,18 @@
 t',%0A    
+ 
+ 
 body: JS
@@ -1891,23 +1891,56 @@
 eq)%0A
+  
   %7D%0A%0A  
+  self.paymentURI = Q.ninvoke(
 request
-(
+, 
 requ
@@ -1946,18 +1946,25 @@
 uestOpts
-, 
+).spread(
 function
@@ -1969,15 +1969,8 @@
 on (
-error, 
 resp
@@ -1989,53 +1989,8 @@
 %7B%0A  
-  if (error) %7B%0A      return cb(error)%0A    %7D%0A%0A
     
@@ -2030,26 +2030,24 @@
 %7B%0A      
-return cb(
+  throw 
 new erro
@@ -2111,18 +2111,19 @@
 Message)
-)
 %0A
+  
     %7D%0A%0A 
@@ -2129,59 +2129,16 @@
     
-var result = JSON.parse(body)%0A    self.paymentURI =
+  return
  cwp
@@ -2179,41 +2179,44 @@
 st, 
-result
+JSON.parse(body)
 .hash)%0A
-%0A
     
-return cb(null,
+%7D)%0A  %7D%0A%0A 
  sel
@@ -2227,21 +2227,98 @@
 ymentURI
-)%0A  %7D
+.done(%0A    function (uri) %7B cb(null, uri) %7D,%0A    function (error) %7B cb(error) %7D%0A  
 )%0A%7D%0A%0Amod

exclude replay requests to cwpp service