import http from 'http';
import GoogleFinanceParser from './parsers/google-finance.js';
import param from 'jquery-param';
import _ from 'lodash';

/**
 * Get a configuration object to be serialized and used in the GET request.
 * @param  {String} options.symbol   - Symbol of the stock in the following format- STOCKEXC:STOCK
 * @param  {Int} options.interval    - Interval in seconds
 * @param  {Int} options.period      - Period of time (ex: 5d - five days, 1Y - one year)
 * @param  {Array} options.fields    - Fields that will be retrieved. ('d' - date, 'o' - open, 'c' - close,
 *                                   'h' - high, 'l' - low, 'v' - volume).
 * @return {Object} JSON Object with the new data.
 */
let getConfigObject = ({symbol,interval, period, fields}) => {
    let f = _.intersection(fields, ['d', 'o', 'h', 'c', 'l', 'v']).join(',');
    let [x,q] = symbol.split(':');
    let p = period;
    let i = interval;
    return {x, q, f, i, p};
};

export let prices = (config, cb) => {
    let options = {
        host: 'www.google.com',
        path: `/finance/getprices?${param(getConfigObject(config))}`
    };
    let parser;
    http.get(options, (resp) => {
        resp.setEncoding('utf8');
        resp.on('data', (data) => {
            parser = new GoogleFinanceParser(data);
            parser.parse(cb);
        });
    });
};



@@ -859,13 +859,15 @@
 let 
-%5Bx,q%5D
+symbols
  = s
@@ -896,112 +896,668 @@
 let 
-p = period;%0A    let i = interval;%0A    return %7Bx, q, f, i, p%7D;%0A%7D;%0A%0Aexport let
+x,q;%0A    if (symbols.length == 1) %7B%0A        %5Bq%5D = symbols;%0A    %7D else %7B%0A        %5Bx, q%5D = symbols;%0A    %7D%0A    let p = period;%0A    let i = interval;%0A    return %7Bx, q, f, i, p%7D;%0A%7D;%0A%0Aclass Traderjs %7B%0A    constructor() %7B%0A        this._config = %7Binterval: 86400,
  p
+e
 ri
-ces = (config, cb) =%3E %7B%0A
+od: '30d', fields: %5B'd', 'o', 'h', 'c', 'l', 'v'%5D%7D;%0A        this._writeTo = null;%0A        this._transformer = null;%0A    %7D%0A    config(config) %7B%0A        this._config = config;%0A        return this;%0A    %7D%0A    writeTo(writeTo) %7B%0A        this._writeTo = writeTo;%0A        return this;%0A    %7D%0A    transformer(transformer) %7B%0A        this._transformer = transformer;%0A        return this;%0A    %7D%0A    do(cb) %7B%0A    
     
@@ -1572,16 +1572,20 @@
 ons = %7B%0A
+    
         
@@ -1616,16 +1616,20 @@
         
+ 
+   
 path: %60/
@@ -1670,16 +1670,22 @@
 gObject(
+this._
 config))
@@ -1691,19 +1691,27 @@
 )%7D%60%0A    
+  
+  
 %7D;%0A
+    
     let 
@@ -1714,24 +1714,28 @@
 let parser;%0A
+    
     http.get
@@ -1760,24 +1760,28 @@
 %3E %7B%0A        
+    
 resp.setEnco
@@ -1798,24 +1798,28 @@
 ');%0A        
+  
+  
 resp.on('dat
@@ -1842,24 +1842,28 @@
             
+   
+ 
 parser = new
@@ -1898,24 +1898,28 @@
             
+    
 parser.parse
@@ -1936,16 +1936,20 @@
     
+    
 %7D);%0A    
 %7D);%0A
@@ -1948,13 +1948,53 @@
     
+ 
+   
 %7D);%0A
-%7D;%0A%0A
+%0A    %7D%0A%7D%0A%0Aexport default new Traderjs();
 %0A

add support for fluent interfaces