#!/usr/bin/env python
# Test Harness to test the Game Controller Functionality
#


import requests  # https://github.com/kennethreitz/requests/
import time
import random


#Replace this variables as appropriate
server_url = 'http://192.168.59.103:8080/api'
admin_header = {'X-Auth-Token': '1234'}


# Server Calls ----------------------------------------------
def server_start(server_url):
    """Start game
       curl -i -H 'X-Auth-Token: 1234' -X POST http://localhost:8080/api/start
    """
    url = server_url + '/start'
    start_game = requests.post(url, headers=admin_header)
    if start_game.status_code == 200:
        print('Server: Game has been Started!')
    else:
        print ('Server: Game Start Failed!')
        print ("HTTP Code: " + str(start_game.status_code) + " | Response: " + start_game.text)


def server_reset(server_url):
    """Reset game
       curl -i -H 'X-Auth-Token: 1234' -X POST http://localhost:8080/api/start
    """
    url = server_url + '/reset'
    reset_game = requests.post(url, headers=admin_header)
    if reset_game.status_code == 200:
        print('Server: Game has been Reset!')
    else:
        print ('Server: Game Reset Failed!')
        print ("HTTP Code: " + str(reset_game.status_code) + " | Response: " + reset_game.text)


def server_stop(server_url):
    """Stop game
       curl -i -H 'X-Auth-Token: 1234' -X POST http://localhost:8080/api/stop
    """
    game_stop_url = server_url + '/stop'
    stop_game = requests.post(game_stop_url, headers=admin_header)
    if stop_game.status_code == 200:
        print('Server: Game has been Stopped!')
    else:
        print ('Server: Game Stop Failed!')
        print ("HTTP Code: " + str(stop_game.status_code) + " | Response: " + stop_game.text)


def server_check_game_started(server_url):
    """
    Start game
    curl -i -H 'X-Auth-Token: 1234' -X POST http://localhost:8080/api/start
    """
    gstart_url = '{0}/start'.format(server_url)
    start_game = requests.post(gstart_url, headers=admin_header)
    if start_game.status_code == 400:
        return True
    else:
        return False


def server_kick_team(server_url, team_name):
    """
    Kicks a team from the registration list. Before the game is started.
    Example Curl: curl -i -H 'X-Auth-Token: 1234' -X POST http://localhost:8080/api/kick/foobar
    :param server_url: The Server URL
    :param team_name: The Team's name
    """
    kick_url = server_url + '/kick/' + team_name
    team_kicked = requests.post(kick_url, headers=admin_header)
    if team_kicked.status_code == 200:
        print('Server: The team: {0} has been Kicked out!'.format(team_name))
    else:
        print ('Server: Team Kick failed for Team: {0}'.format(team_name))
        print ("HTTP Code: {0} | Response: {1}".format(str(team_kicked.status_code), team_kicked.text))


def server_config(server_url):
    """
    Retries the Server's configuration parameters
    curl -i -X GET http://localhost:8080/api/config
    :param server_url:
    :return: Nothing
    """
    kick_url = '{0}/config'.format(server_url)
    srv_config = requests.get(kick_url)
    print ("HTTP Code: {0} | Response: {1}".format(str(srv_config.status_code), srv_config.text))


# Shield Calls ------------------------------------------------
def team_shield_up(team_name, team_auth):
    """
    Sets the team shield up
    curl -i -H 'X-Auth-Token: 1335aa6af5d0289f' -X POST http://localhost:8080/api/shield/enable

    """
    url = server_url + '/shield/enable'
    auth_header = {'X-Auth-Token': team_auth}
    shield_up = requests.post(url, headers=auth_header)
    if shield_up.status_code == 200:
        print ('Server: Team: ' + team_name + ' Shield is UP!')
    else:
        print ('Server: Team: ' + team_name + ' Shield UP! request Failed!')
        print ("HTTP Code: " + str(shield_up.status_code) + " | Response: " + shield_up.text)


def team_shield_down(team_name, team_auth):
    """
    Sets the team shield Down
    curl -i -H 'X-Auth-Token: 1335aa6af5d0289f' -X POST http://localhost:8080/api/shield/disable
    """
    url = server_url + '/shield/disable'
    auth_header = {'X-Auth-Token': team_auth}
    shield_down = requests.post(url, headers=auth_header)
    if shield_down.status_code == 200:
        print ('Server: Team: ' + team_name + ' Shield is DOWN!')
    else:
        print ('Server: Team: ' + team_name + ' Shield DOWN! request Failed!')
        print ("HTTP Code: " + str(shield_down.status_code) + " | Response: " + shield_down.text)