#        config.yaml
# Output: xlsx tournaments database
#
# 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)])
# import sys
# print sys.path

import utils
import models

data_folder = "../data/"

league_filename = "(nueva versión) Liga Dos Orillas 2016 - Categorías Mayores.xlsx"

# utils.load_league_workbook(data_folder + league_filename)

a = utils.load_sheet_workbook(data_folder + league_filename, "Jugadores")

# Loading players info list
players = models.PlayersList()
players.load_list(a)

# print(players)

# Loading initial ranking
initial_ranking = models.Ranking()

raw_ranking = utils.load_sheet_workbook(data_folder + league_filename, "Ranking inicial")
initial_ranking.load_list([[r[0], r[2], r[3]] for r in raw_ranking])

# print(initial_ranking)

snames = utils.get_ordered_sheet_names(data_folder + league_filename, "Partidos")
# print(snames)
# It looks for unknown or unrated players.
# It will ask for information not given 
# and saves the result into the same xlsx
##########################################

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"]))