# Path: crowd_anki/representation/json_serializable.py
# class JsonSerializableAnkiDict(JsonSerializable):
#     export_filter_set = JsonSerializable.export_filter_set | {"anki_dict"}
# 
#     def __init__(self, anki_dict=None):
#         super(JsonSerializableAnkiDict, self).__init__()
#         self.anki_dict = anki_dict
# 
#     def serialization_dict(self):
#         return utils.merge_dicts(super(JsonSerializableAnkiDict, self).serialization_dict(),
#                                  self.anki_dict)
# 
#     def _update_fields(self):
#         self.anki_dict.setdefault(UUID_FIELD_NAME, str(uuid1()))
# 
#     def get_uuid(self):
#         super(JsonSerializableAnkiDict, self).get_uuid()
#         return self.anki_dict[UUID_FIELD_NAME]
# 
#     def post_import_filter(self):
#         for entry in self.import_filter_set:
#             if entry in self.anki_dict:
#                 del self.anki_dict[entry]
# 
#     @classmethod
#     def from_json(cls, json_dict):
#         anki_dict_object = cls(json_dict)
#         anki_dict_object.post_import_filter()
#         anki_dict_object._update_fields()
#         return anki_dict_object
# 
# Path: crowd_anki/utils/uuid.py
# class UuidFetcher:
#     collection: Collection
# 
#     def get_deck_config(self, uuid: str):
#         return get_value_by_uuid(self.collection.decks.all_config(), uuid)
# 
#     def get_deck(self, uuid: str):
#         return get_value_by_uuid(self.collection.decks.all(), uuid)
# 
#     def get_model(self, uuid: str):
#         return get_value_by_uuid(self.collection.models.all(), uuid)
# 
#     def get_note(self, uuid: str):
#         query = "select id from notes where guid=?"
#         note_id = self.collection.db.scalar(query, uuid)
#         if not note_id:
#             return None
# 
#         return AnkiNote(self.collection, id=note_id)


from anki import Collection
from .json_serializable import JsonSerializableAnkiDict
from ..utils.uuid import UuidFetcher


class DeckConfig(JsonSerializableAnkiDict):
    def __init__(self, anki_deck_config=None):
        super(DeckConfig, self).__init__(anki_deck_config)

    @classmethod
    def from_collection(cls, collection, deck_config_id):
        decks = collection.decks
        # TODO Remove compatibility shims for Anki 2.1.46 and lower.
        get_conf = decks.get_config if hasattr(decks, 'get_config') else decks.getConf
        anki_dict = get_conf(deck_config_id)
        deck_config = DeckConfig(anki_dict)
        deck_config._update_fields()

        return deck_config

    def save_to_collection(self, collection: Collection):
        # Todo whole uuid matching thingy
        # For now only create scenario


# Path: crowd_anki/representation/json_serializable.py
# class JsonSerializableAnkiDict(JsonSerializable):
#     export_filter_set = JsonSerializable.export_filter_set | {"anki_dict"}
# 
#     def __init__(self, anki_dict=None):
#         super(JsonSerializableAnkiDict, self).__init__()
#         self.anki_dict = anki_dict
# 
#     def serialization_dict(self):
#         return utils.merge_dicts(super(JsonSerializableAnkiDict, self).serialization_dict(),
#                                  self.anki_dict)
# 
#     def _update_fields(self):
#         self.anki_dict.setdefault(UUID_FIELD_NAME, str(uuid1()))
# 
#     def get_uuid(self):
#         super(JsonSerializableAnkiDict, self).get_uuid()
#         return self.anki_dict[UUID_FIELD_NAME]
# 
#     def post_import_filter(self):
#         for entry in self.import_filter_set:
#             if entry in self.anki_dict:
#                 del self.anki_dict[entry]
# 
#     @classmethod
#     def from_json(cls, json_dict):
#         anki_dict_object = cls(json_dict)
#         anki_dict_object.post_import_filter()
#         anki_dict_object._update_fields()
#         return anki_dict_object
# 
# Path: crowd_anki/utils/uuid.py
# class UuidFetcher:
#     collection: Collection
# 
#     def get_deck_config(self, uuid: str):
#         return get_value_by_uuid(self.collection.decks.all_config(), uuid)
# 
#     def get_deck(self, uuid: str):
#         return get_value_by_uuid(self.collection.decks.all(), uuid)
# 
#     def get_model(self, uuid: str):
#         return get_value_by_uuid(self.collection.models.all(), uuid)
# 
#     def get_note(self, uuid: str):
#         query = "select id from notes where guid=?"
#         note_id = self.collection.db.scalar(query, uuid)
#         if not note_id:
#             return None
# 
#         return AnkiNote(self.collection, id=note_id)


# Path: crowd_anki/representation/deck_config.py
from anki import Collection
from .json_serializable import JsonSerializableAnkiDict
from ..utils.uuid import UuidFetcher



class DeckConfig(JsonSerializableAnkiDict):
    def __init__(self, anki_deck_config=None):
        super(DeckConfig, self).__init__(anki_deck_config)

    @classmethod
    def from_collection(cls, collection, deck_config_id):
        decks = collection.decks
        # TODO Remove compatibility shims for Anki 2.1.46 and lower.
        get_conf = decks.get_config if hasattr(decks, 'get_config') else decks.getConf
        anki_dict = get_conf(deck_config_id)
        deck_config = DeckConfig(anki_dict)
        deck_config._update_fields()

        return deck_config

    def save_to_collection(self, collection: Collection):
        # Todo whole uuid matching thingy
        # For now only create scenario


        config_dict = self.fetch_or_create_config(collection)