//Constants  
const SERIAL = 0;
const BEGIN_IP = 1;
const END_IP = 2;
const TOTAL_COUNT = 3;
const CIDR_MAX = 32;
const EXCLUDE_NETWORK_ADDRESS = 1;


//Imports
var CIDR = require('cidr-js');
var cidr = CIDR();
var csv_writer = require('fast-csv');
var csvReaderStream = require('csv-reader');
var fs = require('fs');



exports.generate_ip_list = function (read_file_path, write_file_path) {
    // CSV Stream 
    var csv_stream = fs.createReadStream(read_file_path, 'utf-8');

    var ip_count_dict = [];
    var ip_list = [];

    // Get CIDR based on count 
    function get_CIDR(count) {
        return (CIDR_MAX - Math.log2(count))
    }

    // Returns list of valid ip
    function get_valid_ip_list(start_ip, count) {
        var _cidr = get_CIDR(+count);
        var query_ip = start_ip + "/" + _cidr;
        return cidr.list(query_ip).slice(EXCLUDE_NETWORK_ADDRESS, count - EXCLUDE_NETWORK_ADDRESS);
    }


    function generate_csv(generated_ip_list, filepath) {
        // Store the IP Addresses as 2D
        var ip_matrix = [];

        // Adding all ip
        generated_ip_list.forEach(function (ip) {
            ip_matrix.push([ip]);
        });

        // Adding Header to the CSV
        ip_matrix.unshift(["ip_address"]);

        // Writing the csv file 
        csv_writer.writeToPath(filepath, ip_matrix, {
            headers: true
        }).on('finish', function () {
            console.log("Finished Writing the CSV File");
        });
    }

    // Load each row after finished reading write in a file 
    csv_stream.pipe(csvReaderStream()).on('data', function (row) {
            ip_count_dict.push({
                'ip': "" + row[BEGIN_IP],
                'count': "" + row[TOTAL_COUNT]
            });
        })
        .on('end', function (data) {
            console.log("FINISHED READING");
            // Finally generate all ips 
            ip_count_dict.slice(1, ip_count_dict.length).forEach(function (ip) {
                var generated_ip_from_range = get_valid_ip_list(ip['ip'], ip['count']);
                ip_list = ip_list.concat(generated_ip_from_range);
            });
        });

};


exports.read_csv = function (read_file_path) {

    var ips = [];
    // CSV Stream 
    var csv_stream = fs.createReadStream(read_file_path, 'utf-8');

    csv_stream.pipe(csvReaderStream()).on('data', function (row) {
            // console.log(row);
            ips.push(row);
        })
        .on('end', function (data) {
            console.log("FINISHED READING");
        });

    return ips;
};
@@ -355,39 +355,15 @@
 on (
-read_file_path, write_file_path
+ip_cidr
 ) %7B%0A
@@ -504,121 +504,8 @@
 %5D;%0A%0A
-    // Get CIDR based on count %0A    function get_CIDR(count) %7B%0A        return (CIDR_MAX - Math.log2(count))%0A    %7D
 %0A%0A  
@@ -781,65 +781,61 @@
 %7D%0A%0A%0A
-    function generate_csv(generated_ip_list, 
+%0A%7D;%0A%0A%0Aexports.read_csv = function (read_
 file
+_
 path) %7B%0A
     
@@ -834,1075 +834,513 @@
 ) %7B%0A
-    
+%0A
     // 
-Store the IP Addresses as 2D%0A  
+exclude first row %0A
     
-  
 var 
-ip_matrix = %5B%5D;%0A%0A        // Adding all ip%0A        generated_ip_list.forEach(function (ip) %7B%0A            ip_matrix.push(%5Bip%5D);%0A        %7D);%0A%0A        // Adding Header to the CSV%0A        ip_matrix.unshift(%5B%22ip_address%22%5D);%0A%0A        // Writing the csv file
+first_row = true;%0A%0A    // Get CIDR based on count %0A    function get_CIDR(count) %7B%0A        return (CIDR_MAX - Math.log2(count))%0A    %7D%0A%0A    var ips = %5B%5D;%0A    // CSV Stream
  %0A    
-   
+var
  csv_
-writer.writeToPath(filepath, ip_matrix, %7B%0A            headers: true%0A        %7D).on('finish', function () %7B%0A            console.log(%22Finished Writing the CSV File%22);%0A        %7D);%0A    %7D%0A%0A    // Load each row after finished reading write in a file %0A    csv_stream.pipe(csvReaderStream()).on('data', function (row) %7B%0A            ip_count_dict.push(%7B%0A                'ip': %22%22 + row%5BBEGIN_IP%5D,%0A                'count': %22%22 + row%5BTOTAL_COUNT%5D%0A            %7D);%0A        %7D)%0A        .on('end', function (data) %7B%0A            console.log(%22FINISHED READING%22);%0A            // Finally generate all ips %0A            ip_count_dict.slice(1, ip_count_dict.length).forEach(function (ip) %7B%0A                var generated_ip_from_range = get_valid_ip_list(ip%5B'ip'%5D, ip%5B'count'%5D);
+stream = fs.createReadStream(read_file_path, 'utf-8');%0A%0A    csv_stream.pipe(csvReaderStream()).on('data', function (row) %7B%0A            // console.log(row);%0A            if (!first_row) %7B%0A                ips.push(%7B%0A                    ip: row%5BBEGIN_IP%5D + %22/%22 + get_CIDR(+row%5BTOTAL_COUNT%5D),
 %0A   
@@ -1356,58 +1356,36 @@
     
-ip_list = ip_list.concat(generated_ip_from_range);
+    count: +row%5BTOTAL_COUNT%5D
 %0A   
@@ -1389,24 +1389,16 @@
         
-    %7D);%0A
         
@@ -1405,292 +1405,51 @@
 %7D);%0A
-%0A%7D;%0A%0A%0Aexports.read_csv = function (read_file_path) %7B%0A%0A    var ips = %5B%5D;%0A    // CSV Stream %0A    var csv_stream = fs.createReadStream(read_file_path, 'utf-8');%0A%0A    csv_stream.pipe(csvReaderStream()).on('data', function (row) %7B%0A            // console.log(row);%0A            ips.push(row)
+            %7D%0A            first_row = false
 ;%0A  

fix read csv