import types
from algorithmContract.dataSource import DataSource
from algorithmContract.dataFilter import DataFilter
from algorithmContract.customFilter import CustomFilter
from algorithmContract.configAnalysis import ConfigAnalysis
from algorithmContract.graphSet import GraphSet


class DataContract:
    def __init__(self,autoOrManual:str , dataFilter: DataFilter, configAnalysis: list[ConfigAnalysis], graphSets: dict[str,GraphSet]):
        self.autoOrManual=autoOrManual
        # self.dataSource = dataSource
        self.dataFilter = dataFilter
        self.configAnalysis = configAnalysis
        self.graphSets = graphSets

    def _graph_sets_to_dict(self, graph_sets):
        result = {}
        for key, value in graph_sets.items():
            if isinstance(value, GraphSet):
                result[key] = value.to_dict()
            elif isinstance(value, dict):
                result[key] = self._graph_sets_to_dict(value)
            else:
                raise TypeError(f"Unsupported type for graph set value: {type(value)}")
        return result

    def to_dict(self):
        return {
            "autoOrManual":self.autoOrManual,
            # "dataSource": self.dataSource.to_dict(),
            "dataFilter": self.dataFilter.to_dict(),            
            "configAnalysis": [analysis.to_dict() for analysis in self.configAnalysis],  # 修改这一行
            "graphSets": {key: graph.to_dict() for key, graph in self.graphSets.items()}  # 假设 graphSets 是一个字典 # self._graph_sets_to_dict(self.graphSets) # 
        }
