#!/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 sys

sys.path.append('../config')
sys.path.append('../common')
sys.path.append('../modules')
sys.path.append('../results')

import json
import traceback

import log
from config_parser import ConfigParse
from single_case_evaluate import single_case_evaluate
# from data_quality import frame_loss_statistic
# from pdf_report_generate import report_generate
from report_generate import report_generate


def single_case_eval(configPath, dataPath, resultPath, trackPath, case_name):
    """

    :param configPath: 配置文件路径
    :param dataPath: 评价数据路径
    :param resultPath: 评价结果路径
    :param trackPath: 轨迹图片路径
    :param case_name: 用例名称

    :return: None
    """
    logger = log.get_logger()

    # 判断文件夹是否为空
    if len(os.listdir(dataPath)) == 0:
        print("No files in data_path!")  # 路径异常
        logger.error(f"[case:{case_name}] SINGLE_CASE_EVAL: No files in data_path!")
        sys.exit(-1)

    # 解析配置文件信息，获得各指标权重及评分系数
    try:
        config = ConfigParse(configPath)
        print("config is", config)
    except Exception as e:
        print('Config file parsing ERROR!', e)
        traceback.print_exc()
        logger.error(f"[case:{case_name}] SINGLE_CASE_EVAL: Config file parsing ERROR: {repr(e)}!", exc_info=True)
        sys.exit(-1)

    # 单用例评价，并生成报告
    try:
        reportDict = single_case_evaluate(config, dataPath, resultPath, case_name)  # 评估单用例
        # reportDict = single_case_statistic(case_dict)  # 对单用例结果增加内容

        # 生成过程文件report.json
        with open(f'{resultPath}/report.json', 'w', encoding='utf-8') as f:
            f.write(json.dumps(reportDict, ensure_ascii=False))

        # 通过report.json生成报告
        reportPdf = os.path.join(resultPath, 'report.pdf')
        report_generate(reportDict, reportPdf, trackPath)

    except Exception as e:
        traceback.print_exc()
        logger.error(f"[case:{case_name}] SINGLE_CASE_EVAL: Evaluate single case ERROR: {repr(e)}!", exc_info=True)
        sys.exit(-1)
