#!/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/04/21
@Summary:           Evaluation entrance
"""

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

if __name__ == "__main__":

    if len(sys.argv) >= 3:
        bagPath = sys.argv[1]
        resultPath = sys.argv[2]
        # graphPath = sys.argv[3]

        logFileName = f'{resultPath}/log.txt'
        log.setup_logger(logFileName)
        logger = log.get_logger()

        case_name = bagPath.split('/')[-1].split('.')[0]
        # case_name = os.path.basename(os.path.dirname(dataPath))

        if not os.path.exists(bagPath):
            print('Invalid bagPath!')
            logger.error(f"[case:{case_name}] SINGLE_CASE_EVAL: Invalid bagPath!")
            sys.exit(-1)
        else:
            try:
                logger.info(f"[case:{case_name}] SINGLE_CASE_EVAL: Start evaluating:")
                print("SINGLE_CASE_EVAL程序开始运行：")
                configPath = r"./config.json"
                print(f"   configPath: {configPath},\n   resultPath: {resultPath}")
                t1 = time.time()
                parsebag(bagPath, resultPath)
                single_case_eval(configPath, resultPath, case_name)
                t2 = time.time()
                print(f"程序结束，执行时间：{int(t2 - t1)} s")
                logger.info(f"[case:{case_name}] SINGLE_CASE_EVAL: End.")
                sys.exit(0)
            except Exception as e:
                print("异常，退出...")
                print(repr(e))
                sys.exit(-1)
    else:
        print('No enough arguments!')
        sys.exit(-1)

    # bagPath = './bag/2024-04-22-08-48-30_obstacledetection_30.bag'
    # resultPath = './result/integration'
    #
    # logFileName = f'{resultPath}/log.txt'
    # log.setup_logger(logFileName)
    # logger = log.get_logger()
    #
    # case_name = bagPath.split('/')[-1].split('.')[0]
    # # case_name = os.path.basename(os.path.dirname(dataPath))
    #
    # if not os.path.exists(bagPath):
    #     print('Invalid bagPath!')
    #     logger.error(f"[case:{case_name}] SINGLE_CASE_EVAL: Invalid bagPath!")
    #     sys.exit(-1)
    # else:
    #     try:
    #         logger.info(f"[case:{case_name}] SINGLE_CASE_EVAL: Start evaluating:")
    #         print("SINGLE_CASE_EVAL程序开始运行：")
    #         configPath = r"./config.json"
    #         print(f"   configPath: {configPath},\n   resultPath: {resultPath}")
    #         t1 = time.time()
    #         parsebag(bagPath, resultPath)
    #         single_case_eval(configPath, resultPath, case_name)
    #         t2 = time.time()
    #         print(f"程序结束，执行时间：{int(t2 - t1)} s")
    #         logger.info(f"[case:{case_name}] SINGLE_CASE_EVAL: End.")
    #         sys.exit(0)
    #     except Exception as e:
    #         print("异常，退出...")
    #         print(repr(e))
    #         sys.exit(-1)
