from pathlib import Path

from pyodm.core.resource.xml_path_resoruce import XmlPathResource
from pyodm.core.xml.reader.mapper.xml_mapper_reader import XMLMapperReader
from pyodm.factory.cdisc_registry import CdiscRegistry
from pyodm.model.definition import Attribute
from pyodm.unit_tests.sql_to_odm.cdisc_element import CdiscElement
from pyodm.utils.class_utils import ClassUtils
from pyodm.utils.dict_utils import DictUtils


def CdiscSQL(sql_path: Path, mapper_path: Path):
    def __inner__(cls):
        return cdisc_sql_factory(cls, sql_path, mapper_path)
    return __inner__


def cdisc_sql_factory(clazz, sql_path: Path, mapper_path: Path):
    setattr(clazz, "sql_file", sql_path)
    setattr(clazz, "mapper_file", mapper_path)
    setattr(clazz, "isCdiscMapper", True)
    setattr(clazz, "mapper", __mapper)
    return clazz


def __mapper(self, registry: CdiscRegistry):
    if self.isCdiscMapper:
        elements = XMLMapperReader(registry, self).read(XmlPathResource(self.mapper_file))
        return elements
    return []
