"""
Script for evaluating Stock Trading Bot.

Usage:
    eval.py <eval-stock> [--window-size=<window-size>]
    [--model-name=<model-name>] [--debug]

Options:
    --window-size=<window-size> Size of the n-day window stock data
                                representation used as the feature
                                vector.
                                    [default: 10]
    --model-name=<model-name>   Name of the pretrained model to use
                                (will eval all models in `models/` if
                                unspecified).
    --debug                     Specifies whether to use verbose logs
                                during eval operation.
"""

import os
import coloredlogs

from docopt import docopt

from trading_bot.agent import Agent
from trading_bot.methods import evaluate_model
from trading_bot.utils import (filter_data_by_feature_columns,
                               get_stock_data,
                               show_eval_result,
                               switch_k_backend_device)


def main(eval_stock, window_size, model_name, debug):
    """ Evaluates the stock trading bot.
    Please see https://arxiv.org/abs/1312.5602 for more details.

    Args: [python eval.py --help]
    """
    data = filter_data_by_feature_columns(get_stock_data(eval_stock))

    # Single Model Evaluation
    if model_name is not None:
        agent = Agent(window_size * data.shape[1], pretrained=True,
                      model_name=model_name)
        profit, history = evaluate_model(agent, data, window_size, debug)
        first_buy = history[0][0]
        show_eval_result(model_name, profit, first_buy)

    # Multiple Model Evaluation
    else:
        for model in os.listdir("models"):
            if os.path.isfile(os.path.join("models", model)):
                agent = Agent(window_size * data.shape[1], pretrained=True,
                              model_name=model)
                profit = evaluate_model(agent, data, window_size, debug)
                show_eval_result(model, profit)
                del agent


if __name__ == "__main__":
    args = docopt(__doc__)

    eval_stock = args["<eval-stock>"]
    window_size = int(args["--window-size"])
    model_name = args["--model-name"]
    debug = args["--debug"]

    coloredlogs.install(level="DEBUG")
    switch_k_backend_device()

    try:
        main(eval_stock, window_size, model_name, debug)
    except KeyboardInterrupt:
        print("Aborted")
