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

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

if __name__ == "__main__":

    # if len(sys.argv) >= 6:
    #     configPath = sys.argv[1]
    #     dataPath = sys.argv[2]
    #     reportPath = sys.argv[3]
    #     csvPath = sys.argv[4]
    #     customMetricPath = sys.argv[5]
    #     customScorePath = sys.argv[6]
    #
    #     case_name = os.path.basename(os.path.dirname(dataPath))
    #
    #     if not os.path.exists(configPath):
    #         print('Invalid configPath!')
    #         logger.error(f"[case:{case_name}] SINGLE_CASE_EVAL: Invalid configPath!")
    #         sys.exit(-1)
    #     elif not os.path.exists(dataPath):
    #         print('Invalid dataPath!')
    #         logger.error(f"[case:{case_name}] SINGLE_CASE_EVAL: Invalid dataPath!")
    #         sys.exit(-1)
    #     elif not os.path.exists(customMetricPath):
    #         print('Invalid customMetricPath!')
    #         logger.error(f"[case:{case_name}] SINGLE_CASE_EVAL: Invalid customMetricPath!")
    #         sys.exit(-1)
    #     elif not os.path.exists(customScorePath):
    #         print('Invalid customScorePath!')
    #         logger.error(f"[case:{case_name}] SINGLE_CASE_EVAL: Invalid customScorePath!")
    #         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   reportPath: {reportPath},\n   "
    #                   f"csvPath: {csvPath},\n   customMetricPath: {customMetricPath}\n   customScorePath: {customScorePath}")
    #             t1 = time.time()
    #             single_case_eval(configPath, dataPath, reportPath, csvPath, customMetricPath, customScorePath)
    #             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!')
    #     logger.error(f"SINGLE_CASE_EVAL: No enough arguments!")
    #     sys.exit(-1)


    task_path = r"./task_0416"

    cases = get_subfolders_name(task_path)

    for case in cases:
        dataPath = os.path.join(task_path, case)

        # dataPath = r"./task/pjibot_pjibot-P1YNYD1M228000127_data_merge_2024-04-07-02-01-35_obstacledetection_9"
        configPath = r"./config.json"

        # case_name = os.path.basename(os.path.dirname(dataPath))
        case_name = dataPath.split('/')[-1]
        casePath = os.path.join("./result", case_name)
        if not os.path.exists(casePath):
            os.makedirs(casePath)

        reportPath = rf"{casePath}/report.json"
        logFileName = './result/log.txt'

        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!")
        else:
            logger.info(f"[case:{case_name}] SINGLE_CASE_EVAL: Start evaluating:")
            print("SINGLE_CASE_EVAL程序开始运行：")
            print(f"  dataPath: {dataPath}, \n  reportPath: {reportPath}")
            t1 = time.time()
            single_case_eval(configPath, dataPath, reportPath, casePath)
            t2 = time.time()
            print(f"程序结束，执行时间：{int(t2 - t1)} s")
            logger.info(f"[case:{case_name}] SINGLE_CASE_EVAL: End.")
