var fs = require('fs'); 
var parse = require('csv-parse');
var db = require("../database/mongo");


var filename = "item.csv";
var csvData=[];

fs.createReadStream(filename)
    //.pipe(parse({delimiter: '`'}))
    .on('data', function(csvrow) {
        //console.log(csvrow[0]);
        //do something with csvrow
        // /var data = JSON.parse(csvrow) ;
        var buf = csvrow.toString();
        //console.log(buf);
        buf = buf.replace(/"/g, '');
        buf = buf.replace(/\t/g, '');
        var buflines = buf.split('\r\n');
        for(i=0;i<buflines.length;i++)
        {
            var itemparts = buflines[i].split('`');
             csvData.push(itemparts);  

        }        
    })
    .on('end',function() {
      //do something wiht csvData
      console.log(csvData.length);
      var idata = csvData[0];
      //console.log(csvData[0][2]/100);
      for(i =0;i<csvData.length;i++)
      {
          console.log(i);
        console.log(csvData[i]);
        console.log("_____________________________");
        var idata = {};
        idata.pk =csvData[i][0];
        idata.title = csvData[i][1];
        idata.description = csvData[i][2];
        idata.available = csvData[i][3];
        idata.fiels_complete= csvData[i][4];
        idata.tital_files= csvData[i][5];
        idata.kind= csvData[i][6];
        idata.parent_id= csvData[i][7];
        idata.id= csvData[i][8];
        idata.slug= csvData[i][9];
        idata.path= csvData[i][10];
        //idata.extra_fields= csvData[i][11];
        idata.youtube_id= csvData[i][11];
        idata.size_on_disk= csvData[i][12];
        idata.remote_size= csvData[i][13];
        idata.sort_order = csvData[i][14];

      
          db.khanitems.insert(idata,function(err,data){
                if(err)
                {
                    //console.log(err);
                }
                else{
                    console.log(idata);
                }
          }); 

      }
      return;

    });