var request = require('postman-request');

var base_url = 'https://bleutrade.com/api/v3/public';

function get_summary(coin, exchange, cb) {
  var req_url = base_url + '/getmarketsummary?market=' + coin + '_' + exchange;
  request({uri: req_url, json: true, headers: {'User-Agent': 'eiquidus'}}, function (error, response, body) {
    if (error) {
      return cb(error, null);
    } else {
      if (body.message) {
        return cb(body.message, null)
      } else {
        var retVal = {
          'high': body.result.High,
          'low': body.result.Low,
          'volume': body.result.Volume,
          'bid': body.result.Bid,
          'ask': body.result.Ask,
          'last': body.result.Last,
          'prev': body.result.PrevDay
        };

        return cb (null, retVal);
      }
    }
  });
}

function get_trades(coin, exchange, cb) {
  var req_url = base_url + '/getmarkethistory?market=' + coin + '_' + exchange + '&count=50';
  request({uri: req_url, json: true, headers: {'User-Agent': 'eiquidus'}}, function (error, response, body) {
    if (body.success) {
      var trades = [];

      if (body.result.length > 0) {
          for (var i = 0; i < body.result.length; i++) {
            var trade = {
              ordertype: body.result[i]['OrderType'],
              price: body.result[i]['Price'],
              quantity: body.result[i]['Quantity'],
              total: body.result[i]['Total'],
              timestamp: body.result[i]['TimeStamp']
            }

            trades.push(trade);
          }
      }

      return cb(null, trades);
    } else {
      return cb(body.message, null);
    }
  });
}

function get_orders(coin, exchange, cb) {
  var req_url = base_url + '/getorderbook?market=' + coin + '_' + exchange + '&type=all' + '&depth=50';
  request({uri: req_url, json: true, headers: {'User-Agent': 'eiquidus'}}, function (error, response, body) {
    if (body.success) {
      var orders = body.result;
      var buys = [];
      var sells = [];

      if (orders['buy'].length > 0) {
        for (var i = 0; i < orders['buy'].length; i++) {
          var order = {
            price: orders.buy[i].Rate,
            quantity: orders.buy[i].Quantity
          }

          buys.push(order);
        }
      }

      if (orders['sell'].length > 0) {
        for (var i = 0; i < orders['sell'].length; i++) {
          var order = {
            price: orders.sell[i].Rate,
            quantity: orders.sell[i].Quantity
          }

          sells.push(order);
        }
      }

      return cb(null, buys, sells);
    } else {
      return cb(body.message, [], [])
    }
  });
}

function get_chartdata(coin, exchange, cb) {
  var end = Date.now();

  end = end / 1000;
  start = end - 86400; 
  
  var req_url = base_url + '/getcandles/?market=' + coin + '_' + exchange + '&period=1h';

  request({ uri: req_url, json: true, headers: {'User-Agent': 'eiquidus'} }, function (error, response, chartdata) {
    if (error) {
      return cb(error, []);
    } else {
      if (chartdata.success) {
        var processed = [];

        for (var i = 0; i < chartdata.result.length; i++) {
          // only take values more recent than the last 24 hours
          if (new Date(chartdata.result[i].TimeStamp).getTime()/1000 > start) {
            processed.push([new Date(chartdata.result[i].TimeStamp).getTime(), parseFloat(chartdata.result[i].Open), parseFloat(chartdata.result[i].High), parseFloat(chartdata.result[i].Low), parseFloat(chartdata.result[i].Close)]);
          }
        }

        return cb(null, processed);
      }
      else
        return cb(chartdata.message, []);
    }
  });
}

module.exports = {
  market_name: 'Bleutrade',
  market_logo: 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAAF8WlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNi4wLWMwMDIgNzkuMTY0NDYwLCAyMDIwLzA1LzEyLTE2OjA0OjE3ICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdEV2dD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS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0eynYNT0ZKTlJvS5WS7EduaL7e8cntDfb3n9x3nAfxAjeOfQqDGJVO/xwnOsK1OVlb7s44f934HJzD7B1FKUrmvEE0w/LiZFT3jtgrvXO83YXe2TzNcPY6gXOMXFraYfXD1aRVBUoW0sI0AtfpmUfGaeynxZrV6YfR9gaYNjC5Gdt4/rG2ntiWS2hTgDO5fnCPNBUXUBxj3/0OJ8l5US+DxQlYmtlpqxp2XjoEZBosSjkNVYA4I85BCxAcplk6jaYHlBxkyoI3iM54hWZWN+DBA40zK0cPghMAR4Iq9il4Ckj1b5LFyUdipf8Cba+9zaJkr9qMf7GT9HC1HxwVxuD2+DbwIHqX4uxBqpcug/SzEOxpFAbcDz9k1id/At3gdXn7zpxjdKXaHcDNC4hCR4aL83HZAbSeLSzL7R2aQLxrGFR1A9Hx35frm18y69NFYCuBc62z09Zmthwmw1aU9Aslzxspbqq0QFKydttR+qoYxI21J/at+dOPXpA31pZFEONfwAie3PFmXOIAAAAASUVORK5CYII=',
  get_data: function(settings, cb) {
    var error = null;
    get_chartdata(settings.coin, settings.exchange, function (err, chartdata){
      if (err) { chartdata = []; error = err; }
      get_orders(settings.coin, settings.exchange, function(err, buys, sells) {
        if (err) { error = err; }
        get_trades(settings.coin, settings.exchange, function(err, trades) {
          if (err) { error = err; }
          get_summary(settings.coin, settings.exchange, function(err, stats) {
            if (err) { error = err; }
            return cb(error, {buys: buys, sells: sells, chartdata: chartdata, trades: trades, stats: stats});
          });
        });
      });
    });
  }
};