#
# Copyright 2015-2023 Universidad Complutense de Madrid
#
# This file is part of Numina
#
# SPDX-License-Identifier: GPL-3.0-or-later
# License-Filename: LICENSE.txt
#

"""Simple monocromatic simulation"""

import json


class RunCounter(object):
    """Run number counter"""

    def __init__(self, template, last=1):
        self.template = template
        self.last = last

    def runstring(self):
        """Return the run number and the file name."""
        cfile = self.template % self.last
        self.last += 1
        return cfile


class PersistentRunCounter(RunCounter):
    """Persistent run number counter"""

    def __init__(self, template, last=1, pstore='index.json',):

        last = self.load(pstore, last)

        super(PersistentRunCounter, self).__init__(template, last)

        self.pstore = pstore

    def store(self):
        with open(self.pstore, 'w') as pkl_file:
            json.dump(self.last, pkl_file)

    @staticmethod
    def load(pstore, last):
        file_exists = True

        try:
            with open(pstore, 'rb') as pkl_file:
                last = json.load(pkl_file)
        except IOError:
            file_exists = False

        if not file_exists:
            with open(pstore, 'wb') as pkl_file:
                json.dump(last, pkl_file)

        return last

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.store()


def extract(header, meta, path, key, selector=None, default=None):
    m = meta
    if selector is None:
        def selector(x): return x  # noqa: E731
    try:
        for part in path:
            m = m[part]
        header[key] = selector(m)
    except KeyError:
        # Keyword missing
        if default is not None:
            header[key] = default


def extractm(meta, path, selector=None):
    m = meta
    if selector is None:
        def selector(x): return x  # noqa: E731
    for part in path:
        m = m[part]
    return selector(m)


if __name__ == '__main__':

    with PersistentRunCounter('r00%04d') as p:
        for i in range(10):
            print(p.runstring())

    with PersistentRunCounter('r00%04d') as p:
        for i in range(10):
            print(p.runstring())
