import os
import time
from logging import getLogger

from tools.trains.t002_lgb import t002_lgb_train as train
from tools.utils.args import parse_train_args
from tools.utils.configs import load_configs
from tools.utils.logs import logInit, send_line_notification

CONFIG_FILE = './configs/c044.yml'

if __name__ == '__main__':
    t0 = time.time()
    script_name = os.path.basename(__file__).split('.')[0]
    log_file = script_name + '.log'

    logger = getLogger(__name__)
    logger = logInit(logger, './mnt/logs/', log_file)
    args = parse_train_args(logger)
    configs = load_configs(CONFIG_FILE, logger)

    auc_mean, auc_std = train(args, script_name, configs, logger)
    prec_time = time.time() - t0
    send_line_notification(f'Finished: {script_name} '
                           f'using CONFIG: {CONFIG_FILE} '
                           f'w/ AUC {auc_mean:.5f}+-{auc_std:.5f} '
                           f'in {prec_time:.1f} s !')
    args = parse_args(None)
    exp_id = args.exp_id
    log_file = f'{script_name}_{exp_id}.log'

    logger = getLogger(__name__)
    logger = logInit(logger, './mnt/logs/', log_file)
    sel_log(f'args: {sorted(vars(args).items())}', logger)
    configs = load_configs(CONFIG_DIR + exp_id + '.yml', logger)

    if configs['runner'] == 'r001':
        from tools.runners.r001_basic_runner import Runner
    elif configs['runner'] == 'r002':
        from tools.runners.r002_warmup_separate import Runner
    elif configs['runner'] == 'r003':
        from tools.runners.r003_metric_learning import Runner
    elif configs['runner'] == 'r004':
        from tools.runners.r004_test_random_sampler import Runner
    runner = Runner(configs, args, logger)
    if not args.prediction:
        if len(args.cell_types) != 1:
            raise Exception('you can use just one cell type for train')
        cell_type = args.cell_types[0]
        runner.train_model(cell_type)
    else:
        # sub_filename = './mnt/submissions/e083_2019-09-25-16-13-01_1.06221_11.00000_sub.csv'
        sub_filename = None
        for i, cell_type in enumerate(args.cell_types):
            sub_filename = runner.make_submission_file(cell_type, sub_filename)

    send_line_notification(f'Finished cell_type {args.cell_types}')