import requests, json

def GetCourseNameID():
    print("Existing course names:")

    r = requests.get("http://5.67.108.54:8080/coursename")
    courseNameJson = json.loads(r.content)

    for courseNameObj in courseNameJson['_embedded']['coursename']:
        courseNameID = courseNameObj['_links']['self']['href']
        courseName = courseNameObj['coursename']

        print("{0}\t{1}".format(courseNameID, courseName))

    useExisting = input("Do you want to use an existing course name? 'y' for yes: ")

    if(useExisting == "y"):
        #Use existing
        id = input("Please enter ID to use: ")
        return id
        #TODO: Should error check this by ensuring the id entered exists.
    else:
        #insert new
        courseName = input("Please enter course name (e.g. Software Engineering): ")

        postCourseName = {
            "coursename": courseName,
            }

        r = requests.post("http://5.67.108.54:8080/coursename", json=postCourseName)
        responseJson = json.loads(r.content)

        link = responseJson['_links']['self']['href']
        id = link.split("/")[-1:]

        return id[0]

def GetDegreeTypeID():
    print("Existing degree types:")

    r = requests.get("http://5.67.108.54:8080/degreetype")
    degreeTypeJson = json.loads(r.content)

    for degreeType in degreeTypeJson['_embedded']['degreetype']:
        degreeTypeID = degreeType['_links']['self']['href']
        shortCode = degreeType['shortcode']
        typeName = degreeType['typename']

        print("{0}\t{1} ({2})".format(degreeTypeID, shortCode, typeName))

    useExisting = input("Do you want to use an existing degree type? 'y' for yes: ")

    if(useExisting == "y"):
        #Use existing
        id = input("Please enter ID to use: ")
        return id
        #TODO: Should error check this by ensuring the id entered exists.
    else:
        #insert new
        typeName = input("Please enter type name (e.g. bachelor of science): ")
        shortCode = input("Please enter shortcode (e.g. BSc): ")

        postDegreeType = {
            "typename": typeName,
            "shortcode": shortCode
            }

        r = requests.post("http://5.67.108.54:8080/degreetype", json=postDegreeType)
        responseJson = json.loads(r.content)

        link = responseJson['_links']['self']['href']
        id = link.split("/")[-1:]

        return id[0]

def GetCourseID():
    r = requests.get("http://5.67.108.54:8080/course")
    courseListJson = json.loads(r.content)

    print("Existing courses:")

    for course in courseListJson['_embedded']['course']:
        requestDegreeType = requests.get(course['_links']['degreetype']['href'])
        degreeTypeJson = json.loads(requestDegreeType.content)

        requestCourseName = requests.get(course['_links']['coursename']['href'])
        courseNameJson = json.loads(requestCourseName.content)

        print(course['_links']['self']['href'] + " " + degreeTypeJson['shortcode'] + " " + courseNameJson['coursename'])

    useExisting = input("Do you want to use an existing course? 'y' for yes: ")

    if(useExisting == "y"):
        #Use existing
        id = input("Please enter ID to use: ")
        return id
        #TODO: Should error check this by ensuring the id entered exists.
    else:
        degreeTypeID = GetDegreeTypeID()
        courseNameID = GetCourseNameID()

        postCourse = {
            "degreetype": "http://5.67.108.54:8080/degreetype/" + degreeTypeID,
            "coursename": "http://5.67.108.54:8080/coursename/" + courseNameID
            }

        r = requests.post("http://5.67.108.54:8080/course", json=postCourse)
        responseJson = json.loads(r.content)

        link = responseJson['_links']['self']['href']
        id = link.split("/")[-1:]

        return id[0]

name = input("Please enter student name: ")
address = input("Please enter student address: ")
dob = input("Please enter student dob: ")
courseID = GetCourseID()

data = {
    'name': name,
    'address': address,
    'dob': dob,
    'course': 'http://5.67.108.54:8080/course/' + courseID
    }

r = requests.post("http://5.67.108.54:8080/student/", json=data)

print("Status code: {0}\n".format(r.status_code))
print(r.content)