import logging
import os
import argparse
from datetime import datetime
from comp.comp import get_comp_info
from heros.heroes import get_heroes_info
from minion.minion import get_minion_info

session_id_value = ""  # 请替换为实际的sessionid值
csrf_token_value = ""  # 请替换为实际的csrftoken值


def main():
    parser = argparse.ArgumentParser(description="爬取hsreplay.net酒馆数据")
    parser.add_argument("-o",
                        "--operation",
                        choices=["comp", "hero", "minion"],
                        required=True,
                        help="comp: 流派 hero: 英雄 minion: 随从")
    parser.add_argument("-p",
                        "--percentile",
                        choices=["ALL", "TOP_20_PERCENT"],
                        required=True,
                        help="ALL: 全部玩家 TOP_20_PERCENT: 百分之二十玩家")
    parser.add_argument(
        "-r",
        "--range",
        choices=["CURRENT_BATTLEGROUNDS_PATCH", "LAST_7_DAYS"],
        required=True,
        help="CURRENT_BATTLEGROUNDS_PATCH: 当前版本 LAST_7_DAYS: 最近7天")
    parser.add_argument("-s",
                        "--sessionid",
                        default="",
                        help="session_id的值(可选/在main.py中修改)")
    parser.add_argument("-c",
                        "--csrftoken",
                        default="",
                        help="csrf_token的值(可选/在main.py中修改)")

    args = parser.parse_args()

    params = {
        "BattlegroundsMMRPercentile": args.percentile,
        "BattlegroundsTimeRange": args.range,
    }
    log = create_logger(
        args.operation,
        os.path.join("logs/" + args.operation,
                     datetime.now().strftime("%Y%m%d_%H%M%S") + ".log"))
    session_id_value = args.sessionid
    csrf_token_value = args.csrftoken
    headers = {
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
        "Referer": "https://hsreplay.net/",
        "accept-language": "zh-CN,zh;q=0.9",
        "Cookie":
        f"sessionid={session_id_value}; csrftoken={csrf_token_value}",
    }

    if args.operation == "comp":
        get_comp_info(params, headers, log)
    elif args.operation == "hero":
        get_heroes_info(params, headers, log)
    elif args.operation == "minion":
        get_minion_info(params, headers, log)


def safe_create_directory(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)


def create_logger(logger_name, file_name):
    safe_create_directory(os.path.dirname(file_name))
    logger = logging.getLogger(logger_name)
    logger.setLevel(logging.INFO)
    file_handler = logging.FileHandler(file_name)
    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)
    return logger


if __name__ == "__main__":
    main()