# It looks for unknown or unrated players.
# It will ask for information not given
# and saves the result into the same xlsx
##########################################

xlsx_file = cfg["io"]["data_folder"] + cfg["io"]["tournaments_filename"]

# Listing tournament sheetnames by increasing date
tournament_sheetnames = utils.get_sheetnames_by_date(xlsx_file, cfg["sheetname"]["tournaments_key"])

# Loading and completing the players list
players = models.PlayersList()
players.load_list(utils.load_sheet_workbook(xlsx_file, cfg["sheetname"]["players"]))

# Loading initial ranking and adding new players with 0
ranking = utils.load_ranking_sheet(xlsx_file, cfg["sheetname"]["initial_ranking"])

for tid, tournament_sheetname in enumerate(tournament_sheetnames):
    # Loading tournament info
    tournament = utils.load_tournament_xlsx(xlsx_file, tournament_sheetname)

    for name in tournament.get_players_names():
        if players.get_pid(name) is None:
            association = input("Enter the association of %s:\n" % name)
            city = input("Enter the city of %s:\n" % name)
            # Assign a pid for the new given player and add it to the list
            players.add_new_player(name, association, city)
            print(players[players.get_pid(name)])

        pid = players.get_pid(name)
##########################################

tournaments_xlsx = cfg["io"]["data_folder"] + cfg["io"]["tournaments_filename"]
rankings_xlsx = cfg["io"]["data_folder"] + cfg["io"]["rankings_filename"]
log_xlsx = cfg["io"]["data_folder"] + cfg["io"]["log_filename"]
histories_xlsx = cfg["io"]["data_folder"] + "histories.xlsx"

# Listing tournament sheetnames by increasing date
tournament_sheetnames = utils.get_sheetnames_by_date(tournaments_xlsx, cfg["sheetname"]["tournaments_key"])

# Loading players info list
players = models.PlayersList()
players.load_list(utils.load_sheet_workbook(tournaments_xlsx, cfg["sheetname"]["players"]))

# Loading initial ranking
initial_ranking = utils.load_ranking_sheet(tournaments_xlsx, cfg["sheetname"]["initial_ranking"])

for tid, tournament_sheetname in enumerate(tournament_sheetnames):
    # Loading tournament info
    tournament = utils.load_tournament_xlsx(tournaments_xlsx, tournament_sheetname)

    old_ranking = models.Ranking("pre_" + tournament.name, tournament.date, tournament.location, tid - 1)

    # Load previous ranking if exists
    if tid-1 >= 0:
        old_ranking = utils.load_ranking_sheet(rankings_xlsx, tournament_sheetnames[tid - 1].replace(
            cfg["sheetname"]["tournaments_key"], cfg["sheetname"]["rankings_key"]))

    # Load initial rankings for new players
    pid_new_players = []
    for name in tournament.get_players_names():