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 BannedNote : Logger {
    private var bannedNoteStore = mutableMapOf<String, String>()

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

    fun saveBannedNote() {
        ObjectMapper().writerWithDefaultPrettyPrinter()
            .writeValue(Paths.get("Banned Note.json").toFile(), bannedNoteStore)
        logInfo("Saved Banned Note")
    }

    fun putBannedNoteFile(noteID: String, commentary: String) {
        bannedNoteStore[noteID] = commentary
    }

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

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