import numpy as np
import pandas as pd
import pickle
import os

os.chdir('/Users/mikaelajordan/Documents/Projects/MarkovChainBaseball')

### Field names
cols = ['gameid', 'visteam', 'inning', 'batteam', 'outs', 'balls', 'strikes', 'visscore', 'homescore', 'resbatter', 'resbatterhand', 'respitcher', 'respitcherhand', 'firstrunner', 'secondrunner', 'thirdrunner', 'eventtext', 'leadoffflag', 'pinchhitflag', 'defensivepos', 'lineuppos', 'eventtype', 'battereventflag', 'abflag', 'hitvalue', 'SHflag', 'SFflag', 'outsonplay', 'RBIonplay', 'wildpitchflag', 'passedballflag', 'numerrors', 'batterdest', 'runon1dest', 'runon2dest', 'runon3dest']


# with open('attributes.txt', "wb") as fp:
#     pickle.dump(cols, fp)
#
# ## To load the column names
# with open('attributes.txt', "rb") as fp:
#     b = pickle.load(fp)

dfnames = os.listdir('./2016')
dfnames = [os.path.splitext(x)[0] for x in os.listdir('./2016') if len(x) == 3]


play_by_play = pd.DataFrame()
play_by_play = pd.read_csv('./2016/ana.txt', header=None)
play_by_play = play_by_play.append(pd.read_csv('./2016/ari.txt', header=None), ignore_index=True)
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/atl.txt', header=None), ignore_index=True)
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/bal.txt', header=None), ignore_index=True)
#print("Added team BAL")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/bos.txt', header=None), ignore_index=True)
#print("Added team BOS")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/cha.txt', header=None), ignore_index=True)
#print("Added team CHA")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/chn.txt', header=None), ignore_index=True)
#print("Added team CHN")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/cin.txt', header=None), ignore_index=True)
#print("Added team CIN")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/cle.txt', header=None), ignore_index=True)
#print("Added team CLE")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/col.txt', header=None), ignore_index=True)
#print("Added team COL")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/det.txt', header=None), ignore_index=True)
#print("Added team DET")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/hou.txt', header=None), ignore_index=True)
#print("Added team HOU")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/kca.txt', header=None), ignore_index=True)
#print("Added team KCA")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/lan.txt', header=None), ignore_index=True)
#print("Added team LAN")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/mia.txt', header=None), ignore_index=True)
#print("Added team MIA")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/mil.txt', header=None), ignore_index=True)
#print("Added team MIL")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/min.txt', header=None), ignore_index=True)
#print("Added team MIN")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/nya.txt', header=None), ignore_index=True)
#print("Added team NYA")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/nyn.txt', header=None), ignore_index=True)
#print("Added team NYN")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/oak.txt', header=None), ignore_index=True)
#print("Added team OAK")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/phi.txt', header=None), ignore_index=True)
#print("Added team PHI")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/pit.txt', header=None), ignore_index=True)
#print("Added team PIT")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/sdn.txt', header=None), ignore_index=True)
#print("Added team SDN")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/sea.txt', header=None), ignore_index=True)
#print("Added team SEA")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/sfn.txt', header=None), ignore_index=True)
#print("Added team SFN")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/sln.txt', header=None), ignore_index=True)
#print("Added team SLN")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/tba.txt', header=None), ignore_index=True)
#print("Added team TBA")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/tex.txt', header=None), ignore_index=True)
#print("Added team TEX")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/tor.txt', header=None), ignore_index=True)
#print("Added team TOR")
#print(play_by_play.shape)
play_by_play = play_by_play.append(pd.read_csv('./2016/was.txt', header=None), ignore_index=True)
#print("Added team WAS")
#print(play_by_play.shape)

play_by_play.columns = cols
print(play_by_play.columns)
print(play_by_play.head())

play_by_play.to_csv('./plays.csv')
