"""SynopReader class

reads a nested JSON structure and flattens it for easier access"""

import json
from collections import OrderedDict

class SynopReader():

    def __init__(self, filename):
        """
        initializes object with filename and reads the JSON file
        """
        # ToDo: interpret URL as alternative to filename and access JSON from URL if filename startswith http
        with open(filename, 'r') as f:
            content = f.read()
        self.filename = filename
        self.content = json.loads(content)
        ## print(self.content)

    @staticmethod
    def condense(d, keys=None):
        """
        converts a "DWD dct" with key, value, units tags into dict entry key: (value, units)
        :param d: the input dictionary
        :param keys: existing key values (to avoid overwriting)
        :return: dict with {key: (value, units)}
        """
        print(d)
        key = d.get('key', 'undefined_key')
        value = d.get('value')
        units = d.get('units')
        # ToDo: improve this logic to make result more readable
        if keys is not None and key in keys:
            suffix = '123456789abcdefghijklmnopqrstuvwxyz'
            key_c = [key+'_'+x for x in suffix]
            for k in key_c:
                if not k in keys:
                    key = key_c
                    break
        return {key: (value, units)}

    def process_one(self, element):
        """
        browse through one part of the JSON structure and condense all information
        :return: condensed dict
        """
        res = OrderedDict()
        # check if current element is list
        if isinstance(element, list):
            for item in element:
                res.update(self.process_one(item))
            return res
        # ToDo: implement solution for list
        # assume current element is dict
        return self.condense(element, res.keys())

    def process(self):
        """
        browse through the entire JSON structure and condense all information
        :return: condensed dict
        """
        res = self.process_one(self.content)
        print(res)




if __name__ == "__main__":
    TESTDATA = "testdata/snippet_synop_dwd_opendata.json"
    SynopReader(TESTDATA).process()