package net.taehui.twilight.system

import net.taehui.twilight.Logger
import net.taehui.twilight.TwilightComponent
import net.taehui.twilight.Utility
import java.io.IOException
import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap

object NoteFilesSystem : Logger {
    private var noteFiles = ConcurrentHashMap<String, Path>()

    fun loadNoteFiles() {
        DB.getNotes().thenAccept { notes ->
            try {
                noteFiles = ConcurrentHashMap(notes.associateWith {
                    TwilightComponent.NOTE_ENTRY_PATH.resolve(Utility.getNoteID512(it))
                })
                logInfo("Loaded Note Files")
            } catch (e: IOException) {
                logFault(e)
            }
        }
    }

    val noteFile: Map.Entry<String, Path>?
        get() = noteFiles.entries.randomOrNull()

    fun getNoteFile(noteID: String): Path? {
        return noteFiles[noteID]
    }

    fun hasNoteFile(noteID: String): Boolean {
        return noteFiles.containsKey(noteID)
    }
}