import json
import typing as T

from pycs import app
from pycs.interfaces.LabelProvider import LabelProvider
from pathlib import Path

class FlatMothLabels(LabelProvider):

    def __init__(self, root_folder: str, configuration: dict):

        mapping_file = Path(root_folder, configuration["mapping"])
        with open(mapping_file) as f:
            self.mapping = json.load(f)

    def close(self):
        pass

    def get_labels(self) -> T.List[dict]:

        labels = []
        for key, entries in self.mapping.items():
            display_name = f"{entries['genus']} {entries['species']} ({entries['kr']})"
            reference = entries["kr"]
            app.logger.debug(f"{reference} -> {display_name}")
            label = self.create_label(reference, display_name)
            labels.append(label)

        return labels

