import os


def addVerses(text):
    lines = text.split("\n")
    newLines = [
        f"{index - 1} {line}"
        for index, line in enumerate(lines, 1)
        if len(line) > 0 and index > 1
    ]

    return "\n".join([lines[0]] + newLines)


def process_all_files(source_directory, verse_directory, reading_directory):
    try:
        bookDict = {}
        for filename in os.listdir(source_directory):
            if filename.endswith(".txt"):
                source_path = os.path.join(source_directory, filename)
                # destination_path = os.path.join(destination_directory, filename)

                with open(source_path, "r") as text_file:
                    content = text_file.read()
                    if content is not None:
                        lines = content.split("\n")
                        if filename[12:15] == "PSA":
                            if filename[16:19] == "001":
                                lines[0] = " Book 1, Psalm."
                            elif filename[16:19] == "042":
                                lines[0] = " Book 2, Psalm."
                            elif filename[16:19] == "073":
                                lines[0] = " Book 3, Psalm."
                            elif filename[16:19] == "090":
                                lines[0] = " Book 4, Psalm."
                            elif filename[16:19] == "107":
                                lines[0] = " Book 5, Psalm."
                            else:
                                lines[0] = " Psalm."
                            lines[1] = lines[1].split(" ")[1]

                        modified_content = "\n".join(
                            [lines[0][:-1] + " " + lines[1][:-1] + " "]
                            + lines[2:]
                            + [""]
                        )
                    # modified_content = addVerses(content)
                # modified_content = "hi"

                if modified_content is not None:
                    # Write the modified content to a new file in the destination directory
                    # with open(destination_path, "w") as modified_file:
                    #     modified_file.write(modified_content)
                    # print(filename[16:18])
                    if not filename[8:15] in bookDict:
                        bookDict[filename[8:15]] = ["" for num in range(151)]

                    bookDict[filename[8:15]][
                        int(filename.split("_")[3])
                    ] = modified_content
        for book, chapterList in bookDict.items():
            chapterList = [chapter[1:] for chapter in chapterList if chapter != ""]
            destination_path = os.path.join(reading_directory, book + "_read.txt")
            with open(destination_path, "w") as modified_file:
                modified_file.write("".join(chapterList))
            chapterList = [addVerses(chapter) for chapter in chapterList]
            destination_path = os.path.join(verse_directory, book + ".txt")
            with open(destination_path, "w") as modified_file:
                modified_file.write("\n\n".join(chapterList))
        # print(bookDict["092_1JN"])

    except FileNotFoundError:
        print(f"Directory not found: {source_directory}")


source_directory = "inputFiles"
verse_directory = "numberedOutput"
reading_directory = "readingOutput"
process_all_files(source_directory, verse_directory, reading_directory)
