package app.furmeet.events.system.helper

import android.os.Build
import android.os.FileObserver
import java.io.File

abstract class FileObserverCompat(private val filename: String) {
    constructor(file: File) : this(file.absoluteFile.absolutePath)

    private val file = File(filename)
    private val implementation: FileObserver

    init {
        implementation =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
                    object : FileObserver(file) {
                        override fun onEvent(event: Int, path: String?) {
                            this@FileObserverCompat.onEvent(event, path)
                        }
                    }
                else
                    @Suppress("DEPRECATION")
                    object : FileObserver(filename) {
                        override fun onEvent(event: Int, path: String?) {
                            this@FileObserverCompat.onEvent(event, path)
                        }
                    }
    }

    fun startWatching() = implementation.startWatching()
    fun stopWatching() = implementation.stopWatching()
    abstract fun onEvent(event: Int, path: String?)
}
