'''
This program assigns near and far player attributes to points in annotation files

Currently requires each set to be done manually

'''
import numpy as np
import json
import os
import math
from os import listdir
from os.path import isfile, join
import utilities.paths as paths

DRIVE = paths.get_drive()

VIDEOS = ['V001']

PLAYERS = {'V001': {'P1': ['Roger', 'Federer'], 'P2': ['Rafael', 'Nadal']},
           'V006': {'P1': ['Serena', 'Williams'], 'P2': ['Maria', 'Sharapova']},
           'V007': {'P1': ['Novak', 'Djokovic'], 'P2': ['Jo-Wilfried', 'Tsonga']},
           'V008': {'P1': ['Vera', 'Zvonareva'], 'P2': ['Serena', 'Williams']},
           'V009': {'P1': ['Roger', 'Federer'], 'P2': ['Juan Martin', 'Delpotro']},
           'V010': {'P1': ['Serena', 'Williams'], 'P2': ['Victoria', 'Azarenka']}}

out_path = DRIVE + 'DATASETS/VIDEO/TENNIS/ANNOTATOR_FILES/'
for video in VIDEOS:
    if os.path.isfile(out_path + 'AF' + video[1:] + '.json'):
        print 'File ' + out_path + 'AF' + video[1:] + '.json' + ' exists. Loading it.'
        inF = open(out_path + 'AF' + video[1:] + '.json', 'r')
        database = json.load(inF)
        inF.close()


    # for each set
    for set in database['classes']['Set']:
        set_number = int(set['custom']['Score'].split('-')[0]) + int(set['custom']['Score'].split('-')[1])
        starting_near = set['custom']['Near']
        # print starting_near
        for players in PLAYERS[video]:
            for name in PLAYERS[video][players]:
                starting_near = starting_near.replace(name, players)

        # print starting_near

        # assign near for each game
        swaps = 0
        for game in database['classes']['Game']:
            if int(set['start']) < int(game['start']) < int(set['end']) or int(set['start']) < int(game['end']) < int(set['end']):
                game_number = int(game['custom']['Score'].split('-')[0]) + int(game['custom']['Score'].split('-')[1])
                print game_number # ensure these go up correctly
                swaps = int(math.floor(game_number/2))

                if swaps % 2 == 0:
                    game['custom']['Near'] = starting_near
                else:
                    if starting_near == 'P1':
                        game['custom']['Near'] = 'P2'
                    else:
                        game['custom']['Near'] = 'P1'

                game['custom']['Near'] = PLAYERS[video][game['custom']['Near']][1]

                # assign near for each point
                for point in database['classes']['Point']:
                    if int(game['start']) < int(point['start']) < int(game['end']) or int(game['start']) < int(point['end']) < int(game['end']):
                        swaps_tmp = swaps
                        if game_number > 12 and set_number < 3: # tiebreak
                            if len(point['custom']['Score'].split('-')) > 1:
                                point_number = int(point['custom']['Score'].split('-')[0]) + int(point['custom']['Score'].split('-')[1])
                                print point_number  # ensure these go up correctly
                                swaps_tmp = swaps + int(math.floor((point_number - 1) / 6))

                        if swaps_tmp % 2 == 0:
                            point['custom']['Near'] = starting_near
                        else:
                            if starting_near == 'P1':
                                point['custom']['Near'] = 'P2'
                            else:
                                point['custom']['Near'] = 'P1'

                        point['custom']['Near'] = PLAYERS[video][point['custom']['Near']][1]


    outAF = open(out_path + 'AF' + video[1:] + '_NEAR.json', 'w')
    json.dump(database, outAF)
    outAF.close()