import threading

from ucsschool.veyon_client.client import VeyonClient
from ucsschool.veyon_client.models import AuthenticationMethod

VEYON_KEY_FILE = "/etc/ucsschool-veyon/key.pem"

with open(VEYON_KEY_FILE, "r") as fp:
    key_data = fp.read().strip()

client = VeyonClient(
    "http://localhost:11080/api/v1",
    credentials={"keyname": "teacher", "keydata": key_data},
    auth_method=AuthenticationMethod.AUTH_KEYS,
)


def create_session():
    # host ~ windows client
    # default would be the ucs host -> connection timeout
    print(client._get_connection_uid(host="10.200.47.13"))


if __name__ == "__main__":
    # this was used as a rough qa for `Bug 52521 - Make ucsschool-veyon-client thread safe

    threads = []

    for i in range(50):
        x = threading.Thread(target=create_session)
        threads.append(x)
        x.start()

    for index, thread in enumerate(threads):
        thread.join()

    # before fix
    # e84e9c18-1d65-426d-9ff5-b4567deb29d0
    # 56e919ce-79f8-4ac5-9677-dc977b41b362
    # cadc162e-d604-48c9-bee6-8684f97c0e9d
    # 6a2a2e83-dabf-46ba-a6e2-bc1813fa5193

    # after fix
    # 86efe23e-24fd-4a5b-9439-e80a1fe865e4
    # 86efe23e-24fd-4a5b-9439-e80a1fe865e4
    # 86efe23e-24fd-4a5b-9439-e80a1fe865e4
    # 86efe23e-24fd-4a5b-9439-e80a1fe865e4