#!/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, single_case_statistic
# 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):
    logger = log.get_logger()
    # case_name = os.path.basename(os.path.dirname(dataPath))
    # case_name = dataPath.split('/')[-1]

    # 判断文件夹是否为空
    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:
        # json_file = os.path.join(configPath, 'config.json')
        config = ConfigParse(configPath)
    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:
        case_dict = single_case_evaluate(config, dataPath, resultPath, case_name)  # 评估单用例
        single_case_dict = single_case_statistic(case_dict)  # 对单用例结果增加内容，并生成报告

        # 将单用例测试结果保存为json文件
        # single_case_dict.pop('playbackData')
        # single_case_dict.pop('evalData')

        # with open(f'{resultPath}/report.json', 'w', encoding='utf-8') as f:
        #     f.write(json.dumps(single_case_dict, ensure_ascii=False))

        reportDict = single_case_dict
        # reportPdf = f'{resultPath}/report.pdf'
        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)

# def data_quality_detect(dataPath, case_name):
#     logger = log.get_logger()
#
#     # first_order_loss = 0.01  # first : little loss 100
#     second_order_loss = 0.05  # second : could use  95
#     third_order_loss = 0.10  # third : could not use    90
#
#     frame_loss_dict = frame_loss_statistic(dataPath)
#
#     flag_frame_loss = False
#     for key, value in frame_loss_dict.items():
#         # sensor data
#         sensor_data = ["RoadMark", "RoadPos", "TrafficLight", "TrafficSign"]
#         if any(file in key for file in sensor_data):
#         # if "RoadMark" in key or "RoadPos" in key or "TrafficLight" in key or "TrafficSign" in key:
#         # if key[:-4] in ["RoadMark", "RoadPos", "TrafficLight", "TrafficSign"]:
#             logger.info(f"[case:{case_name}] SINGLE_CASE_EVAL: [{key}] : {value['result']}")
#             continue
#
#         if value['frame_loss_rate'] > third_order_loss:
#             logger.error(
#                 f"[case:{case_name}] SINGLE_CASE_EVAL: [{key}] frame loss rate > {third_order_loss * 100}%: {value['result']}")
#             flag_frame_loss = True
#         elif value['frame_loss_rate'] > second_order_loss:
#             logger.info(
#                 f"[case:{case_name}] SINGLE_CASE_EVAL: [{key}] frame loss rate > {second_order_loss * 100}%: {value['result']}")
#         else:
#             logger.info(
#                 f"[case:{case_name}] SINGLE_CASE_EVAL: [{key}] frame loss rate < {second_order_loss * 100}%: {value['result']}")
#
#     return flag_frame_loss
