package net.taehui.twilight.system

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import net.taehui.twilight.Logger
import java.io.IOException
import java.nio.file.Paths
import java.util.concurrent.ConcurrentHashMap

object BannedNoteFile : Logger {
    private var bannedNoteFileStore = mutableMapOf<String, String>()

    fun loadBannedNoteFile() {
        try {
            bannedNoteFileStore.putAll(
                ObjectMapper().readValue(
                    Paths.get("Banned Note File.json").toFile(),
                    object : TypeReference<ConcurrentHashMap<String, String>>() {})
            )
            logInfo("Loaded Banned Note File")
            saveBannedNoteFile()
        } catch (e: IOException) {
            logFault(e)
        }
    }

    fun saveBannedNoteFile() {
        ObjectMapper().writerWithDefaultPrettyPrinter()
            .writeValue(Paths.get("Banned Note File.json").toFile(), bannedNoteFileStore)
        logInfo("Saved Banned Note File")
    }

    fun putBannedNoteFile(noteID: String, comment: String) {
        bannedNoteFileStore[noteID] = comment
    }

    fun wipeBannedNoteFile(noteID: String) {
        bannedNoteFileStore.remove(noteID)
    }

    fun isBanned(noteID: String): Boolean {
        return bannedNoteFileStore.contains(noteID)
    }
}