import lbppy
import os

class Transcription(lbppy.Resource):
    def file_path(self):
        file_path = self.values("http://scta.info/property/hasXML").to_s()
        #file_path = self.results.dup.filter(:p => RDF::URI("http://scta.info/property/hasXML")).first[:o].to_s
        if (os.getenv('EXIST') == "local"):
            file_path = file_path.replace("exist.scta.info", "localhost:8080")

        return file_path
    #TODO; this will need to change because an expression above the item level will have more than one doc listed
    def doc_path(self, branch="master"):
        doc_path = self.values("http://scta.info/property/hasDocument").to_s()
        #file_path = self.results.dup.filter(:p => RDF::URI("http://scta.info/property/hasXML")).first[:o].to_s
        if (branch != "master"):
            doc_path = doc_path.replace("master", branch)

        return doc_path

    def transcription_type(self):
        type = self.values("http://scta.info/property/transcriptionType")
        #type = self.results.dup.filter(:p => RDF::URI("http://scta.info/property/transcriptionType")).first[:o].to_s
        return type.to_s().lower()

    def file(self, **kwargs):
        path = kwargs.get("path") if kwargs.get("path") else "doc"
        confighash = kwargs.get("confighash") if kwargs.get("confighash") else None
        branch = kwargs.get("branch") if kwargs.get("branch") else "master"

        if (path == "file"):
            file = lbppy.File(self.file_path(), self.transcription_type(), confighash)
        elif (path == "doc"):
            file = lbppy.File(self.doc_path(branch), self.transcription_type(), confighash)

        return file

    # #NOTE: this really is a temporary method, since the database
    # #should point to file corresponding to each transcription
    # #dynamically generated by the exist-db database.
    # # but this could remain in case it was useful to grab the part
    # # from a file that would include a tei header etc.
    def file_part(self, **kwargs):
        partid = kwargs.get("partid") if kwargs.get("partid") else None
        path = kwargs.get("path") if kwargs.get("path") else "doc"
        confighash = kwargs.get("confighash") if kwargs.get("confighash") else None

        if (path == "file"):
            file = lbppy.FilePart(self.file_path(), self.transcription_type(), confighash, partid)
        elif (path == "doc"):
            file = lbppy.FilePart(self.doc_path(), self.transcription_type(), confighash, partid)

        return file
