# compare titles of openalex and xref, checking to see if the subtitle matters
# would probably work a lot better if openalex was in a database. major bottleneck on api wait times

import sys, pymongo, json
from alive_progress import alive_bar
sys.path.insert(1, 'comparisonTest/')
from compare import getDOIOpenAlex, getTitleFromOpenAlexObject

#get next document from mongo
#get title, subtitle, and DOI
#get title from openalex
#compare xref title+subtitle with oAlex title
#count depending on:
#   - titles are directly equivalent, without subtitles
#   - titles are not equivalent, but subtitle makes them equivalent
#   - titles are not equivalent, even with subtitles
#print to log file

writeFile = open("titleLog.txt", "w", encoding="utf-8")

def printMessage(doi, xRefTitle, oAlexTitle, equivalenceMessage):
    if(equivalenceMessage == "Titles Equivalent"):
        print(doi + " : Equivalent")
        return
    print("========================")
    print("DOI: " + doi)
    print("XREF TITLE: " + xRefTitle)
    print("OA TITLE: " + oAlexTitle)
    print("EQUIVALENT: " + equivalenceMessage)
    print("========================")

    dictToConvert = {"DOI" : doi, "xrefTitle" : xRefTitle ,"openAlexTitle" : oAlexTitle, "msg" : equivalenceMessage}
    jsonToWrite = json.dumps(dictToConvert, indent=3)
    writeFile.write(jsonToWrite + "\n")
    #since it is most likely that someone using this program will not be closing the file handler, we should flush it
    #so that data is actually displayed
    writeFile.flush()

def main():
    #initialise mongo connection
    mongoClient = pymongo.MongoClient()
    mongoDatabase = mongoClient["test"]
    mongoCollection = mongoDatabase["test"]

    #get cursor for searching database
    documents = mongoCollection.find()
    count = mongoCollection.count_documents({})

    #for each document in the database
    with alive_bar(count) as bar:
        for document in documents:
            try:
                #get the xref information
                xRefTitle = document["title"]
                xRefSubtitle = None
                if "subtitle" in document.keys():
                    xRefSubtitle = document["subtitle"]
                xRefDOI = document["DOI"]
                
                #now lets get the openalex info
                oAlexObject = getDOIOpenAlex(xRefDOI)
                oAlexTitle = getTitleFromOpenAlexObject(oAlexObject)

                #and we must determine equivalence and such...
                #case 1 titles are same
                if xRefTitle == oAlexTitle:
                    printMessage(xRefDOI, xRefTitle, oAlexTitle, "Titles Equivalent")
                    continue

                #case 2 titles are not same and subtitle does not exist for this DOI
                if xRefSubtitle is None or xRefSubtitle == "":
                    printMessage(xRefDOI, xRefTitle, oAlexTitle, "Titles Not Equivalent")
                    continue

                #now we know the titles are not the same... does the openalex title contain the subtitle?
                #case 3 titles are completely different
                if xRefSubtitle not in oAlexTitle:
                    printMessage(xRefDOI, xRefTitle, oAlexTitle, "Titles Not Equivalent")
                    continue

                #so the titles are not equivalent... but openalex title contains the subtitle! they are probably a little equivalent...
                #get all words that are not in the title or subtitle
                splitOAlexTitle = oAlexTitle.split([xRefTitle, xRefSubtitle])
                #only one string between them
                if len(splitOAlexTitle) == 1:
                    printMessage(xRefDOI, xRefTitle + " " + xRefSubtitle, oAlexTitle, "Title + Subtitle Somewhat Equivalent to Title")
                    continue
                else:
                    printMessage(xRefDOI, xRefTitle + " " + xRefSubtitle, oAlexTitle, "Title + Subtitle Similar to Title but Not Equivalent")
                    continue    
            except Exception as error:
                #probably from openalex not having DOI
                print("Error: " + str(error))
            finally:
                bar()

    #close the file handler
    writeFile.close()

main()