var newMetric       = require('./metric.js')
  , implementation  = require('./implementation.js')
  , putMetricData   = implementation.putMetricData
  , listMetrics     = implementation.listMetrics
  , transport       = implementation.transport
  , ISODateString   = implementation.ISODateString

module.exports = newNameSpace

function newNameSpace(awskey, secret, host, protocol) {
  function NameSpace(namespace) {
    // TODO validate namespace len < 255

    var metric = newMetric( awskey
                          , secret
                          , host
                          , protocol
                          , namespace)

    metric.put = put
    metric.list = list
    return metric

    function put() {

      var args = Array.prototype.slice.call(arguments)
        , cb
        , metrics = []

      // Pop off the last argument if it is a callback
      if (typeof args[args.length - 1] === 'function') {
        cb = args.pop()
      }

      // if you give me some kind of crazy crap, find the
      // objects so I can send them up
      if (args.length === 1 && !Array.isArray(args[0])) {
        metrics = args
      } else if (args.length === 1 && !Array.isArray(args[0])) {
        args[0].forEach(mergeMetrics)
      } else {
        args.forEach(mergeMetrics)
      }

      transport(host
              , protocol
              , putMetricData(awskey
                            , secret
                            , host
                            , namespace
                            , ISODateString(new Date())
                            , metrics)
              , cb)

      function mergeMetrics(each) {
        if (Array.isArray(each)) {
          each.forEach(mergeMetrics)
        } else {
          metrics.push(each)
        }
      }
    }

    function list(cb) {

      if (typeof cb !== 'function') {
        // throw
      }

      transport(host
              , protocol
              , listMetrics(awskey
                          , secret
                          , host
                          , namespace)
              , makeList)

      function makeList(err, reqId, obj) {

        if (err) {
          cb(err, reqId, obj)
        }

        // TODO some kind of structure check
        obj = obj.listmetricsresponse
                  .listmetricsresult
                  .metrics
                  .member
                  .map(function(member) {
                    return NameSpace(namespace)
                            .metric(member.metricname)
                            .dimensions(member.dimensions
                                                .member)})

        cb(err, obj)
      }
    }
  }

  NameSpace.namespace = NameSpace
  return NameSpace
}


@@ -1807,862 +1807,101 @@
 ist(
-cb) %7B%0A%0A      if (typeof cb !== 'function') %7B%0A        // throw%0A      %7D%0A%0A      transport(host%0A              , protocol%0A              , listMetrics(awskey%0A                          , secret%0A                          , host%0A                          , namespace)%0A              , makeList)%0A%0A      function makeList(err, reqId, obj
+op, cb
 ) %7B%0A
-%0A  
       
-if (err) %7B%0A          cb(err, reqId, obj)%0A        %7D%0A%0A        // TODO some kind of structure check%0A        obj = obj.listmetricsresponse%0A                  .listmetricsresult%0A                  .metrics%0A                  .member%0A                  .map(function(member) %7B%0A                    return NameSpace(namespace)%0A                            .metric(member.metricname)%0A                            .dimensions(member.dimensions%0A                                                .member)%7D)%0A%0A        cb(err, obj)%0A      %7D
+var ns = NameSpace(namespace)%0A      ns.metric().list(op, cb)%0A%0A      return ns
 %0A   

simplify code, only use one list function