# Just a simple wrapper function to print the title of each of our phases to the console...
def print_phase_header(message):
    global COUNTER
    print("\n[" + str("%02d" % int(COUNTER)) + "] >>> " + message)
    COUNTER += 1


# This wrapper function prints our messages to the console with a timestamp...
def print_phase_message(message):
    time_stamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(str(time_stamp) + ": " + message)


### get ams redirected url
response = amspy.get_url(access_token)
if (response.status_code == 200):
    ams_redirected_rest_endpoint = str(response.url)
else:
    print_phase_message("GET Status: " + str(response.status_code) +
                        " - Getting Redirected URL ERROR." +
                        str(response.content))
    exit(1)

######################### PHASE 1: UPLOAD #########################
### create an asset
print_phase_header("Creating a Media Asset")
response = amspy.create_media_asset(access_token, NAME)
if (response.status_code == 201):
    resjson = response.json()
    asset_id = str(resjson['d']['Id'])
###########################################################################################

# Load Azure app defaults
try:
    with open('config.json') as configFile:
        configData = json.load(configFile)
except FileNotFoundError:
    print("ERROR: Expecting config.json in current folder")
    sys.exit()

account_name = configData['accountName']
account_key = configData['accountKey']

# Get the access token...
response = amspy.get_access_token(account_name, account_key)
resjson = response.json()
access_token = resjson["access_token"]

#Initialization...
print("\n-----------------------= AMS Py =----------------------")
print("Simple Python Library for Azure Media Services REST API")
print("-------------------------------------------------------\n")

### get ams redirected url
response = amspy.get_url(access_token)
if (response.status_code == 200):
    print("New Redirected URL: " + str(response.url))
else:
    print("GET Status: " + str(response.status_code) +
          " - Getting Redirected URL ERROR." + str(response.content))