#!/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
import time
import json
import traceback

import pandas as pd

from single_case_eval import single_case_eval
import log

if __name__ == "__main__":
    # import warnings
    # warnings.filterwarnings("ignore")
    from warnings import simplefilter

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

    if len(sys.argv) >= 8:
        configPath = sys.argv[1]
        dataPath = sys.argv[2]
        reportPath = sys.argv[3]
        csvPath = sys.argv[4]
        logPath = sys.argv[5]
        customMetricPath = sys.argv[6]
        customScorePath = sys.argv[7]
        if len(sys.argv) >= 9:
            playbackPath = sys.argv[8]
        else:
            playbackPath = sys.argv[4] + "_playback.csv"

        if len(sys.argv) > 9:
            case_name = sys.argv[9]
        else:
            case_name = os.path.basename(os.path.dirname(dataPath))

        log.setup_logger(logPath)
        logger = log.get_logger()

        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   logPath: {logPath},\n   customMetricPath: {customMetricPath}\n   "
                      f"customScorePath: {customScorePath},\n   caseName: {case_name}")
                t1 = time.time()
                single_case_eval(configPath, dataPath, reportPath, csvPath, playbackPath, customMetricPath,
                                 customScorePath, 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("异常如下：")
                traceback.print_exc()
    else:
        logPath = sys.argv[5]
        log.setup_logger(logPath)
        logger = log.get_logger()
        print('SINGLE_CASE_EVAL: No enough arguments!')
        logger.error(f"SINGLE_CASE_EVAL: No enough arguments!")
        sys.exit(-1)
