import requests
requests.packages.urllib3.disable_warnings() 
from icecream import ic


def httpget(url):
    token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InJlZ3VsYXJAaXRlYy5hYXUuYXQiLCJjcmVhdGVkX2F0IjoiMjAyMS0wMy0yNCAxMDoxMzo1MS4wMjkwNDkiLCJ2YWxpZF91bnRpbCI6IjIwMjEtMDMtMjUgMTA6MTM6NTEuMDI5MDQ5In0.V6kYV5Lmb_tUIsF-6AKNB8_lIifmJP_Dm8gHhGa5w_o'
    res = requests.get(url, 
                    verify=False, 
                    headers = { "Authorization": f"Bearer {token}"})
    return res

# list tables
res = httpget(url = 'https://articonf1.itec.aau.at:30420/api/use-cases/crowd-journalism/tables')
print("Tables: ", [entry['name'] for entry in res.json()])



# count pushed data
def count_data(json_res, table_identifier='table'):
    tables = {}
    for entry in json_res:
        key = entry[table_identifier]
        if key not in tables:
            tables[key] = 0
        tables[key] += 1
    ic(tables)

res = httpget(url = 'https://articonf1.itec.aau.at:30001/api/use_cases/crowd-journalism/transactions')
count_data(res.json())

res_f = httpget(url = 'https://articonf1.itec.aau.at:30001/api/use_cases/crowd-journalism/transactions-failed')
count_data(res_f.json(), 'docType')

# failed tags: the "tag" is missing, but is called name
# failed purchases: duplicate keys generated from (userid, videoid, ownerid)
# failed classifications: impact is missing


# visualize content
import matplotlib.pyplot as plt

def visualize_video_coordinates():
    geolocations = []
    for entry in res.json():
        if entry['table'] != 'video':
            continue
        loc_ = entry['properties']['geolocation'].split(',')
        if loc_[0] == 'undefined' or loc_[1] == 'undefined':
            continue
        geolocations.append(loc_)

    plt.scatter([float(coor[0]) for coor in geolocations], [float(coor[1]) for coor in geolocations])
    plt.axis('off')
    plt.show()
# visualize_video_coordinates()


def visualize_video_prices():
    price = []
    for entry in res.json():
        if entry['table'] != 'video':
            continue
        price.append(entry['properties']['price'])

    from collections import Counter
    print(Counter(price))

    plt.hist(price, bins=len(set(price)))
    plt.show()
# visualize_video_prices()


def visualize_content_ratings():
    impact = []
    informative = []
    trustiness = []
    for entry in res.json():
        if entry['table'] != 'classification':
            continue
        if entry['properties']['impact'] is not None:
            impact.append(entry['properties']['impact'])
        if entry['properties']['informative'] is not None:
            informative.append(entry['properties']['informative'])
        if entry['properties']['trustiness'] is not None:
            trustiness.append(entry['properties']['trustiness'])

    from collections import Counter
    print(Counter(impact))
    print(Counter(informative))
    print(Counter(trustiness))

    fig, (ax1, ax2, ax3) = plt.subplots(3)
    ax1.hist(impact, bins=len(set(impact)))
    ax1.set_title('impact')
    ax2.hist(informative, bins=len(set(informative)))
    ax2.set_title('informative')
    ax3.hist(trustiness, bins=len(set(trustiness)))
    ax3.set_title('trustiness')
    plt.show()
# visualize_content_ratings()


# counting duplicate entries for 'purchase'
working_purchase_ids = [(entry['properties']['userid'], entry['properties']['videoid'], entry['properties']['ownerid']) for entry in res.json() if entry['table'] == 'purchase']
failed_purchase_ids = [(entry['userid'], entry['videoid'], entry['ownerid']) for entry in res_f.json() if entry['docType'] == 'purchase']

ic(len(working_purchase_ids))
ic(len(failed_purchase_ids))

cnt = 0
for failed_id in failed_purchase_ids:
    if failed_id in working_purchase_ids:
        cnt += 1

ic(cnt)