import requests
import json
import hmac
import hashlib
import base64
import string
import random
import time

#base_url = "https://test.zeuscasinovip.com/api/ezugi/live_casino/v1/"
betconnection_url = "https://staging.integrationscenter.net/"
base_url = "https://casino-administracion.com/api/betconnection/v1/"
client_id = "664bcb5aec9268bb0acb7657"
token = "862c4d33-364f-4860-b4b7-bbfa2ad58217"
file_path_url = '/var/www/html/api_tests/betconnection/output/'

products = {}

def is_json(myjson):
  try:
    json.loads(myjson)
  except ValueError as e:
    return False
  return True

def get_current_timestamp():
    return int(time.time())

def generate_transaction_id():
    return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(30))

def pretty_json(jsonData):
    return json.dumps(jsonData, indent=2)

def save_to_file(text, file_name):
    file_object = open(file_path_url + file_name, 'w')
    file_object.write(text)
    file_object.close()

def client_data():
    global betconnection_url
    global token
    global base_url
    global client_id
    global products
    # The API endpoint
    
    url = betconnection_url + "customers" + "/" + client_id 

    data = {
    }

    headers = {
            'content-type': 'application/json',
            'tokenClient' :  token
    }

    # A POST request to the API
    response = requests.get(url, json=data, headers=headers)
    json_response = response.json()

    if 'products' in json_response:
        print("Saving products data")
        products = json_response.get('products')

    save_to_file(pretty_json(json_response), 'out.txt')

    return pretty_json(json_response)

def list_products():
    global betconnection_url
    global token
    global base_url
    global client_id
    global products

    productsIds = []
    i = 0
    ask_str = "PRODUCT LIST: \n\n"
    for product in products:
        ask_str += "\t" + str(i) + ": " + product['productId'] + "\n"
        productsIds.append(product['productId'])
        i+=1

    return ask_str

def list_games(product_id):
    global betconnection_url
    global token
    global base_url
    global client_id
    global products

    productsIds = []
    for product in products:
        productsIds.append(product['productId'])

    if (product_id < 0 or product_id >= len(productsIds)):
        return "Invalid product index"


    # /products/:productId/games

    # The API endpoint
    
    url = betconnection_url + "products" + "/" + productsIds[product_id] + "/games"

    data = {
    }

    headers = {
            'content-type': 'application/json',
            'tokenClient' :  token
    }

    # A POST request to the API
    response = requests.get(url, json=data, headers=headers)
    json_response = response.json()

    save_to_file(pretty_json(json_response), 'out.txt')

    if 'games' in json_response:
        print("Saving games data")
        products[product_id]["games"] = json_response.get('games')


    return pretty_json(json_response)

def process_input(input_res):

    if (input_res == 'client data'): 
        return client_data()
    if (input_res == 'list products'): 
        return list_products()
    if (input_res == 'list games'): 
        product_id = int(input("Introduce the product ID (see: list products): "))
        return list_games(product_id)
    if (input_res == 'launch url'): 
        product_id = int(input("Introduce the product ID (see: list products): "))
        game_id = int(input("Introduce the game ID (see: list games): "))
        return "WIP"



    elif (input_res == 'exit'):
        return "bye"
    else:
        return "Invalid action";

res = "ok"
while (res != "exit"):
    res = input("Write the name of the API Action: ")
    print(process_input(res))
