"""

# NBA 2020/2021 Fantasy Draft Kit

Starting from the 19/20 stats and the 20/21 projections, visualise and sort the data. Find the z-scores and display, to help you make better choices.

TODO:
- Track all the teams as they are picked
- Calculate the expected performance of your current team, both in absolute projected score and relative position
- Find a better way to do this live
- Find a way to swap between DataFrames in the interact widget
"""
import pandas as pd
import seaborn as sns

from ipywidgets import interact, fixed
pd.set_option("display.max_rows", 1000)
"""
### Load and format the input data
"""
projections = pd.read_csv("/kaggle/input/nba-9cat-stats-and-projections-20202021/projections.csv")
stats = pd.read_csv("/kaggle/input/nba-9cat-stats-and-projections-20202021/stats.csv")
CATEGORIES = ["FG%", "FT%", "3PM", "REB", "AST", "STL", "BLK", "TO", "PTS"]
COLS = ["name", "positions", "GP", "MIN", "A/TO", *CATEGORIES]
def format_table(df):
    numerics = df.columns.drop(["name", "positions"])
    for col in numerics:
        df[col] = df[col].astype(float)
    df = df[COLS].copy()
    df["team"] = [None]*len(df)
    return df
projections = format_table(projections)
projections.head()
# Z-scores
def get_zscores_df(df):
    zscores_df = df.drop(columns=CATEGORIES)
    for col in CATEGORIES:
        zscores_df[col] = round((df[col]-df[col].mean())/df[col].std(ddof=0),2)
    return zscores_df[COLS + ["team"]]

def cross_out(row):
    """cross out taken players"""
    if row["team"] is not None:
        return ['text-decoration: line-through' for _ in row]
    else:
        return ['' for _ in row]


def display_table(df):
    display(df.style.background_gradient(cmap=CMAP, subset=CATEGORIES)
            .set_precision(2).apply(cross_out, axis=1))
zscores_df = get_zscores_df(projections)
def sort(df, column, position):
    if position != "ALL":
        df = df[df.positions.apply(lambda x: position in x)]
    if column != "OVERALL":
        df = df.sort_values(by=column, ascending=False)
    return display(df.style.background_gradient(cmap=CMAP, subset=CATEGORIES)
            .set_precision(2).apply(cross_out, axis=1))  
CMAP=sns.diverging_palette(5, 250, as_cmap=True)
# interact(sort, df=[("projections", projections), ("zscores",zscores_df)], column=CATEGORIES)
interact(sort, df=fixed(zscores_df), column=["OVERALL", *CATEGORIES],
         position=["ALL", "PG", "SG", "PF", "SF", "C"])