import json
from pyscript import document, when
import asyncio

# Shared variables to store both sets of data
followers_data = None
following_data = None


# Allow followers file to be dropped
@when("dragover", "#follower-drop-zone")
def drag_over_followers(event):
    event.preventDefault()


# Handle followers file
@when("drop", "#follower-drop-zone")
def drop_followers(event):
    event.preventDefault()

    files = event.dataTransfer.files

    if files.length > 0:
        file = files.item(0)

        asyncio.ensure_future(
            read_file(file.name, file.text(), "followers")
        )


# Allow following file to be dropped
@when("dragover", "#following-drop-zone")
def drag_over_following(event):
    event.preventDefault()


# Handle following file
@when("drop", "#following-drop-zone")
def drop_following(event):
    event.preventDefault()

    files = event.dataTransfer.files

    if files.length > 0:
        file = files.item(0)

        asyncio.ensure_future(
            read_file(file.name, file.text(), "following")
        )


# Read the contents of the file
async def read_file(filename, content, file_type):

    global followers_data, following_data

    file_content = await content

    if file_type == "followers":
        followers_data = json.loads(file_content)

    elif file_type == "following":
        following_data = json.loads(file_content)

    document.getElementById("status").textContent = (
        f"File received: {filename}"
    )

    print(filename)

    # Check whether both files have finished loading
    if followers_data is not None and following_data is not None:
        print("Both files successfully loaded!")

        # Future integration point for your algorithm
        # ORIGINAL: Terminal-based file loading
        # Restore this section when running with regular Python.
        # Remember to comment out the temporary PyScript loading above.

        #file path goes where the json files go
        # followersPath = input("Enter the path for your 'followers_1.json' file: ")
        # followingPath = input("Enter the path for your 'following.json' file: ")

        # with open(followersPath, "r", encoding="utf-8") as f:
        #     followers_data = json.load(f)

        # with open(followingPath, "r", encoding="utf-8") as f:
        #     following_data = json.load(f)

        #print(following_data)

        # Extract usernames into Python lists
        followers  = dict()
        following = []

        #O(n)
        for person in following_data["relationships_following"]:
            following.append(person["title"])
            
        #O(m)
        for person in followers_data:
            followers[person["string_list_data"][0]["value"]] = 0

        #print(myFollowers)
        unfollowers = []

        #O(n)
        for person in following:
            if person not in followers:
                link = "https://www.instagram.com/" + person
                unfollowers.append([person,link])
                
        print(unfollowers)
        output_div = document.querySelector("#textarea")
        output_div.innerText = unfollowers