import os, csv

preamble = \
"""
/**
 * WARNING: THIS FILE IS AUTOGENERATED. ALL CHANGES WILL BE OVERWRITTEN.
 */

#include "flow_data.hpp"

const std::vector<DownlinkProducer::FlowData> PAN::flow_data = {
"""
preamble = preamble[1:]

class FlowListParser(object):
    def __init__(self, inputFile):
        with open(inputFile, "r") as csvfile:
            self.data_list = list(csv.reader(csvfile, delimiter = ','))

    def parse(self):
        data = []
        for row_data in self.data_list:
            if row_data[1].lower() not in ["true", "false"]:
                # This is just an annotation/comment row, ignore it.
                continue

            reformatted_row_data = [len(data) + 1, row_data[1].lower(), [item for item in row_data[2:] if item != ""]] # Flow ID, flow active/inactive, flow fields
            parsed_row = str(reformatted_row_data).replace("[", "{").replace("]", "}").replace("'", "\"")
            parsed_row = parsed_row.replace("\"true\"", "true").replace("\"false\"", "false")
            data.append(parsed_row)

        return ",\n".join(data)

end = \
"""
};
"""

with open("src/flow_data.cpp", "w") as output_f:
    output_f.write(preamble)

    parsed_flow_list = FlowListParser("src/flow_data.csv").parse()
    output_f.write(parsed_flow_list)

    output_f.write(end)
