#!/usr/bin/env python
# -*- coding: utf-8 -*-
##################################################################
#
# Copyright (c) 2023 CICV, Inc. All Rights Reserved
#
##################################################################
"""
@Authors:           yangzihao(yangzihao@china-icv.cn)
@Data:              2024/01/30
@Last Modified:     2024/01/30
@Summary:           Evaluateion functions
"""


import os
import importlib.util
from collections import defaultdict


# from process.data_process import DataProcess
# from process.config_parser import ConfigParse


def custom_run(folder_path, all_data, metric_list, case_name):
    # 初始化一个字典来存储结果
    results = defaultdict(dict)
    # metric_list = all_data.config.metric_list
    # metric_list = ['ldw_miss_warning_count', 'ldw_miss_warning_count2', 'ldw_miss_warning_count3']

    # 遍历文件夹中的所有文件
    for filename in os.listdir(folder_path):
        if filename.endswith('.py') and not filename.startswith('__') and filename[
                                                                          :-3] in metric_list:  # 过滤掉非Python文件或特殊文件
            # 构造模块的完整路径
            module_path = os.path.join(folder_path, filename)

            # 使用importlib来动态导入模块
            spec = importlib.util.spec_from_file_location(filename[:-3], module_path)
            module = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(module)

            # 假设每个模块都有一个CustomMetric类
            if hasattr(module, 'CustomMetric'):
                CustomMetric = getattr(module, 'CustomMetric')

                # 实例化CustomMetric类
                metric_instance = CustomMetric(all_data, case_name)
                metric_result = metric_instance.result

                # 将结果存储到字典中，key为文件名，value为各个方法的结果
                results[filename[:-3]] = metric_result

    return results

# if __name__ == '__main__':
#     # 文件夹路径，自定义指标Python文件都在此文件夹下
#     folder_path = '../custom'
#     path = r"..\task_1228\case1228\data"
#     configPath = r"../configs/config.json"
#     config = ConfigParse(configPath)
#     all_data = DataProcess(path, config)
#
#     custom_run(folder_path, all_data)
