import pickle
import os

path = os.path.join(os.path.dirname('getMatchList.ipynb'), 'data')
with open(os.path.join(path, 'league_match_history_2016_na.pickle'), 'rb') as f:
    match_ids = pickle.load(f)
    matches = pickle.load(f)
print(len(matches))

import pandas as pd

matches_stats = pd.DataFrame.from_dict(matches)
matches_stats.head()

"""
Explanation: I've crawled all matches played by challenger tier in NA of SEASON2016 by using 'crawlTierMatches.py'. The S7 season has not started yet, let's just play on this data first.
Load Data
End of explanation
"""


matches_stats.loc[matches_stats['role'] == 'DUO_CARRY', 'lane'] = 'BOT_ADC'
matches_stats.loc[matches_stats['role'] == 'DUO_SUPPORT', 'lane'] = 'BOT_SUP'
# drop those BOTTOM that are neither DUO_CARRY nor DUO_SUPPORT
matches_stats = matches_stats[matches_stats['lane'] != 'BOTTOM']
matches_stats.head()

lane_stats = matches_stats.groupby(['lane', 'queue']).size()
lane_stats = lane_stats.unstack()
lane_stats

ax = lane_stats.plot.bar(stacked=True, legend=False);
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=0)
pass

"""
Explanation: What Lane Do People Play Most?
First we need to seperate duo_carry and duo_support.
End of explanation
"""


champ_stats = matches_stats.groupby(['lane', 'champion']).size()
champ_stats = champ_stats.unstack()
champ_stats.head()

from lolcrawler_util import get_champion_name
import matplotlib.pyplot as plt
import numpy as np

f, axarr = plt.subplots(3, 2, figsize=(15,15))
plt_cnt = 0
for lane, row in champ_stats.iterrows():
    sorted_row = row.sort_values(ascending=False)
    # print(sorted_row[:5].values)
    axarr[plt_cnt/2, plt_cnt%2].bar(np.arange(10), sorted_row[:10].values)
    axarr[plt_cnt/2, plt_cnt%2].title.set_text(lane)
    champion_name = []
    for c_id in sorted_row[:10].index.values:
        champion_name.append(get_champion_name(c_id))
    axarr[plt_cnt/2, plt_cnt%2].xaxis.set_ticks(np.arange(10))
    axarr[plt_cnt/2, plt_cnt%2].set_xticklabels(champion_name, rotation=45)
    plt_cnt += 1
f.subplots_adjust(hspace=0.5)
axarr[-1, -1].axis('off')
plt.show()

"""
Explanation: What Champion Do People Play Most?
End of explanation
"""


