import requests
import json


videoListUrl = "https://dev758755.vialog.app/Videos/Meta/ListAll"
videoUrl = "https://dev758755.vialog.app/stat/events?type=video&id="

# token from Rest Gateway to authorize 
JWT_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InJlZ3VsYXJAaXRlYy5hYXUuYXQiLCJjcmVhdGVkX2F0IjoiMjAyMS0wNC0wNyAxMjo0OTo0MS43MTkzNjQiLCJ2YWxpZF91bnRpbCI6IjIwMjEtMDQtMDggMTI6NDk6NDEuNzE5MzY0In0.FN6qqBQeJSmXtS0-0dBiL-ojz6Ou7E5Tc9macrrhM4A'

useCase = "vialog-new-enum"

def send_transaction_to_rest_gateway(transaction: dict):
    res = requests.post(
        url = 'https://articonf1.itec.aau.at:30401/api/trace',
        json = transaction,
        headers = {"Authorization": f"Bearer {JWT_TOKEN}"},
        verify = False # ignore ssl error
    )

    print(res)


videosRequest = requests.get(videoListUrl)
empty = set()
html = set()

if (videosRequest.status_code != 200):
    print(f"Status: {videosRequest.status_code}")

dataCount = 0
for video in videosRequest.json():
    dataCount += 1
    id = video["videoId"]
    videoRequest = requests.get(f"{videoUrl}{id}")
    
    if videoRequest.status_code != 200:
        print(f"Status: {videoRequest.status_code}")
    
    if videoRequest.text != "" and not videoRequest.text.startswith("<!DOCTYPE html>"):
        print(f"\n\n{videoUrl}{id}")
        historyList = sorted(videoRequest.json()[0]["History"], key=lambda k: k['moderationDate'], reverse=True)
        historyList.append(empty)

        i = 0
        changeList = []
        for eventMap in historyList:
            if historyList[i+1] == empty:
                break

            if i == 0:
                lastState = eventMap
                lastState["ApplicationType"] = useCase
                lastState["docType"] = "video"
                print(f"{lastState}\n\n\n")
                send_transaction_to_rest_gateway(lastState)
            
            if eventMap["eventName"] == 'r1eabcbdc8f5378b2ba71a1b6fe2038b Created' or eventMap["eventName"] == 'Created':
                change = {"changeType": "Created", "changedValue": "video_state", "previousValue": "", "newValue":"Created"}
            elif eventMap["eventName"] == "Restore":
                change = {"changeType": "Restore", "changedValue": "", "previousValue": "", "newValue":""}
            elif eventMap["eventName"] == "PositionChange":
                change = {"changeType": "PositionChange", "changedValue": "position", "previousValue": historyList[i+1]["position"], "newValue": eventMap["position"]}
            elif eventMap["eventName"] == "Hide":
                change = {"changeType": "Hide", "changedValue": "video_state", "previousValue": historyList[i+1]["video_state"], "newValue": eventMap["video_state"]}
            elif eventMap["eventName"] == "Publish":
                change = {"changeType": "Publish", "changedValue": "video_state", "previousValue": historyList[i+1]["video_state"], "newValue":eventMap["video_state"]}
            elif eventMap["eventName"] == "CMNote":
                change = {"changeType": "CMNote", "changedValue": "communityManagerNotes", "previousValue": historyList[i+1]["communityManagerNotes"], "newValue":eventMap["communityManagerNotes"]}
            elif eventMap["eventName"] == "Move":
                change = {"changeType": "Move", "changedValue": "position", "previousValue": historyList[i+1]["position"], "newValue":eventMap["position"]}
            elif eventMap["eventName"] == "VideoType":
                change = {"changeType": "VideoType", "changedValue": "video_type", "previousValue": historyList[i+1]["video_type"], "newValue":eventMap["video_type"]}
            elif eventMap["eventName"] == "Delete":
                change = {"changeType": "Delete", "changedValue": "", "previousValue": "", "newValue": ""}
            elif eventMap["eventName"] == "Copy":
                change = {"changeType": "Copy", "changedValue": "", "previousValue": "", "newValue":""}
            elif eventMap["eventName"] == "CustomLabel":
                change = {"changeType": "CustomLabel", "changedValue": "label", "previousValue": historyList[i+1]["label"], "newValue":eventMap["label"]}
            
            change["videoId"] = id
            change["changeId"] = i
            change["timestamp"] = eventMap["moderationDate"]
            change["ApplicationType"] = useCase
            change["docType"] = "change"
            print(change)
            send_transaction_to_rest_gateway(change)
            i += 1
    elif videoRequest.text == "":
        empty.add(id)
    else:
        html.add(id)


print(f"empty: {empty}\n\n")
print(f"html page: {html}\n\n")
print(f"history: {historyList}")
print(dataCount)

        



    





