# -*- coding: utf-8 -*-

from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint
import codecs, re, pprint

def get_bar(lhs, rhs, bar_len, mid_str_fmt):
    dis_chr = u'\u25a0'
    emp_chr = u' '
    ls = emp_chr * (bar_len - lhs) + dis_chr * int(lhs)
    rs = dis_chr * rhs + emp_chr * (bar_len - rhs)
	
    return u'{0:>{1}}'.format(ls, bar_len) + \
		    mid_str_fmt.format(lhs, rhs) + \
		    u'{0:<{1}}'.format(rs, bar_len)

class MyHTMLParser(HTMLParser):
	def __init__(self):
		HTMLParser.__init__(self)
		self.match_commentary = ''
		
	def handle_data(self, data):
		if 'commentaryUpdater' in data:
			temp = data[data.find('commentaryUpdater.load([[')+25:]
			temp = temp[temp.find('[['):temp.find(']]')+1]
			self.match_commentary = re.sub('\[,+', '[', temp)
			

match_id = raw_input("Enter match id: ")
with codecs.open(match_id, 'r') as f:
	s = f.read()

parser = MyHTMLParser()
parser.feed(s)

fmt = '%-6s|%-15s|%-62s|%s'
		
match_commentary_list = [c[:-2] for c in eval(parser.match_commentary)
								if c[0][0].isdigit()]

for comment in match_commentary_list:
	if len(comment[2]) > 60:
		comment[2] = comment[2][:60]
	print fmt % tuple(comment)


attempt_count = [0,0]
yellow_count  = [0,0]
red_count     = [0,0]
goal_count    = [0,0]
penalty_count = [0,0]
foul_count    = [0,0]
offside_count = [0,0]
corner_count  = [0,0]
ontarget_count = [0,0]
offtarget_count = [0,0]
blocked_count = [0,0]
owngoal_count = [0,0]
sub_count     = [0,0]
hitpost_count = [0,0]

first_attempt = ('','')
first_yellow  = ('','')
first_red     = ('','')
first_goal    = ('','')
first_penalty = ('','')
first_foul    = ('','')
first_offside = ('','')
first_corner  = ('','')
first_ontarget = ('','')
first_offtarget = ('','')
first_blocked = ('','')
first_owngoal = ('','')
first_sub     = ('','')
first_hitpost = ('','')

team = ()

for comment in match_commentary_list:
	if 'end 2' in comment[1]:
		team = re.findall(r'ends, (.+) [0-9]+, (.+) [0-9]+\.', comment[2])[0]
	elif 'red' in comment[1] or 'secondyellow' in comment[1]:
		first_red = comment[-1],comment[0]
		if comment[-1]=='home': red_count[0] += 1
		else: red_count[1] += 1
	elif 'yellow' in comment[1]:
		first_yellow = comment[-1],comment[0]
		if comment[-1]=='home': yellow_count[0] += 1
		else: yellow_count[1] += 1
	elif 'penalty lost' in comment[1]:
		first_foul = comment[-1],comment[0]
		if comment[-1]=='home': foul_count[0] += 1
		else: foul_count[1] += 1
		if comment[-1]=='home':
			penalty_count[1] += 1
			first_penalty = 'away',comment[0]
		else:
			penalty_count[0] += 1
			first_penalty = 'home',comment[0]
	elif 'free kick lost' in comment[1]:
		first_foul = comment[-1],comment[0]
		if comment[-1]=='home': foul_count[0] += 1
		else: foul_count[1] += 1
	elif 'corner' in comment[1]:
		first_corner = comment[-1],comment[0]
		if comment[-1]=='home': corner_count[0] += 1
		else: corner_count[1] += 1
	elif 'offside' in comment[1]:
		first_offside = comment[-1],comment[0]
		if comment[-1]=='home': offside_count[0] += 1
		else: offside_count[1] += 1
	elif 'own goal' in comment[1]:
		first_owngoal = comment[-1],comment[0]
		if comment[-1]=='home':
			first_goal = 'away',comment[0]
			goal_count[1] += 1
			owngoal_count[0] += 1
		else:
			first_goal = 'home',comment[0]
			goal_count[0] += 1
			owngoal_count[1] += 1
	elif 'goal' in comment[1]:
		first_goal = first_attempt = comment[-1],comment[0]
		if comment[-1]=='home':
			goal_count[0] += 1
			ontarget_count[0] += 1
		else:
			goal_count[1] += 1
			ontarget_count[1] += 1
	elif 'saved' in comment[1]:
		first_ontarget = first_attempt = comment[-1],comment[0]
		if comment[-1]=='home': ontarget_count[0] += 1
		else: ontarget_count[1] += 1
	elif 'miss' in comment[1]:
		first_offtarget = first_attempt = comment[-1],comment[0]
		if comment[-1]=='home': offtarget_count[0] += 1
		else: offtarget_count[1] += 1
	elif 'post' in comment[1]:
		first_hitpost = first_attempt = comment[-1],comment[0]
		if comment[-1]=='home': hitpost_count[0] += 1
		else: hitpost_count[1] += 1
	elif 'blocked' in comment[1]:
		first_blocked = first_attempt = comment[-1],comment[0]
		if comment[-1]=='home': blocked_count[0] += 1
		else: blocked_count[1] += 1
	elif 'substitution' in comment[1]:
		first_sub = comment[-1],comment[0]
		if comment[-1]=='home': sub_count[0] += 1
		else: sub_count[1] += 1
	else:
		if 'free kick won' not in comment[1]:
			print 'uncategorized comment:', comment[1], comment[-1], comment[0]
	attempt_count[0] = ontarget_count[0] + offtarget_count[0] +\
					   hitpost_count[0] + blocked_count[0]
	attempt_count[1] = ontarget_count[1] + offtarget_count[1] +\
					   hitpost_count[1] + blocked_count[1]
			


print '\ngoals: home %d, away %d' % tuple(goal_count)
print 'first goal: %s %s' % first_goal
print '\nown goals: home %d, away %d' % tuple(owngoal_count)
print 'first own goal: %s %s' % first_owngoal

print '\npenalties: home %d, away %d' % tuple(penalty_count)
print 'first penalty: %s %s' % first_penalty

print '\ntotal sub: home %d, away %d' % tuple(sub_count)
print 'first sub: %s %s' % first_sub

bar_len = max(len(team[0]), attempt_count[0], yellow_count[0], red_count[0],\
			  offside_count[0], foul_count[0], corner_count[0]) + 1
print '\n\n{0:>{4}} {1:>2} - {2:<2} {3}\n'.format(team[0], goal_count[0],\
	goal_count[1], team[1], 13 + bar_len)
mid_str_fmt = u' {0:>2}   {1:<2} '
print '       Shots %s' % (get_bar(attempt_count[0], attempt_count[1],\
						    bar_len, mid_str_fmt))
print '   On target %s' % (get_bar(ontarget_count[0], ontarget_count[1],\
						    bar_len, mid_str_fmt))
print '  Off target %s' % (get_bar(offtarget_count[0], offtarget_count[1],\
						    bar_len, mid_str_fmt))
print 'Hit woodwork %s' % (get_bar(hitpost_count[0], hitpost_count[1],\
						    bar_len, mid_str_fmt))
print '     Blocked %s' % (get_bar(blocked_count[0], blocked_count[1],\
						    bar_len, mid_str_fmt))
print ''
print 'Yellow cards %s' % (get_bar(yellow_count[0], yellow_count[1],\
						    bar_len, mid_str_fmt))
print '   Red cards %s' % (get_bar(red_count[0], red_count[1],\
						    bar_len, mid_str_fmt))
print ''
print '    Offsides %s' % (get_bar(offside_count[0], offside_count[1],\
						    bar_len, mid_str_fmt))
print '       Fouls %s' % (get_bar(foul_count[0], foul_count[1],\
						    bar_len, mid_str_fmt))
print '     Corners %s' % (get_bar(corner_count[0], corner_count[1],\
						    bar_len, mid_str_fmt))