#!/usr/bin/env python
# -*- coding: utf-8 -*-
##################################################################
#
# Copyright (c) 2023 CICV, Inc. All Rights Reserved
#
##################################################################
"""
@Authors:           yangzihao(yangzihao@china-icv.cn)
@Data:              2024/04/21
@Last Modified:     2024/08/01
@Summary:           Pji outdoor robot evaluate entrance
"""

import os
import sys
import time
from single_case_evaluate import single_case_eval
from common import get_subfolders_name
# from pjirebot_evaluate_csv import parsebag
import log

if __name__ == "__main__":

    # 屏蔽warning，减少非必要问题的干扰
    from warnings import simplefilter

    # simplefilter(action="ignore", category=FutureWarning)
    simplefilter(action="ignore")

    # input_dict = {
    #     "data_path": dataPath,
    #     "resultPath": resultPath,
    #     "trackPath": trackPath
    # }

    # 判断参数数量是否足够
    if len(sys.argv) >= 3:
        dataPath = sys.argv[1]  # 数据地址路径，包含多个csv
        resultPath = sys.argv[2]  # 结果文件保存路径，结果文件包括几个过程图片、报告等
        trackPath = sys.argv[3]  # 轨迹图片地址路径，用于报告展示

        # 用例名称，可选参数，如果传输则          选用，不传输则选择文件路径名作为用例名称
        if len(sys.argv) > 4:
            case_name = sys.argv[4]
        else:
            case_name = dataPath.split('/')[-1]  # .split('.')[0]

        configPath = r"./config.json"  # 配置信息，包含指标列表及评分系数

        # logFileName = f'{resultPath}/log.log'
        logFileName = os.path.join(resultPath, 'log.log')  # 日志文件名
        log.setup_logger(logFileName)  # 日志设置

        # 获取日志记录器
        logger = log.get_logger()

        # 如果数据路径不存在，报错，存入日志，退出
        if not os.path.exists(dataPath):
            print('Invalid dataPath!')
            logger.error(f"[case:{case_name}] SINGLE_CASE_EVAL: Invalid dataPath!")
            sys.exit(-1)
        else:
            try:
                logger.info(f"[case:{case_name}] SINGLE_CASE_EVAL: Start evaluating:")
                print("SINGLE_CASE_EVAL程序开始运行：")
                print(
                    f"   configPath: {configPath},\n   dataPath: {dataPath},\n   resultPath: {resultPath},\n   trackPath: {trackPath}")
                # 统计运行时间
                t1 = time.time()
                # 将输入的bag进行解析
                # parsebag(dataPath, resultPath)  # EgoState_pji.csv存放resultPath下
                # 进入主要模块进行单用例评价分析
                single_case_eval(configPath, resultPath, resultPath, trackPath, case_name)
                t2 = time.time()
                print(f"程序结束，执行时间：{int(t2 - t1)} s")
                logger.info(f"[case:{case_name}] SINGLE_CASE_EVAL: End.")
                # 正常运行完成，返回状态0
                sys.exit(0)
            # 异常打印，退出
            except Exception as e:
                print("异常，退出...")
                print(repr(e))
                sys.exit(-1)
            # 增加兜底策略，输出问题报告（内容暂定为日志及问题总结）
            # finally:
            #     pass

    # 参数量不够，报错退出
    else:
        print('No enough arguments!')
        sys.exit(-1)
