package cy.hexor.furumi.data.local

import android.content.Context
import cy.hexor.furumi.domain.model.TrackCard
import dagger.hilt.android.qualifiers.ApplicationContext
import java.io.File
import java.io.IOException
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request

@Singleton
class OfflineMediaStore @Inject constructor(
    @ApplicationContext context: Context,
    private val okHttpClient: OkHttpClient,
    private val libraryStore: OfflineLibraryStore,
    private val settingsStorage: OfflineSettingsStorage
) {
    private val rootDir = File(context.filesDir, "offline")
    private val audioDir = File(rootDir, "audio")
    private val coversDir = File(rootDir, "covers")

    suspend fun cacheTrackAfterListening(trackId: Long) = withContext(Dispatchers.IO) {
        if (!settingsStorage.state.value.settings.saveListenedTracksEnabled) {
            libraryStore.markTrackAccessed(trackId)
            publishStorageStats()
            return@withContext
        }

        val track = libraryStore.getTrack(trackId, preferLocalMedia = false)
            ?: return@withContext
        cacheTrackMedia(track)
    }

    suspend fun cacheTrackMedia(track: TrackCard) = withContext(Dispatchers.IO) {
        libraryStore.upsertTracks(listOf(track))
        libraryStore.markTrackAccessed(track.id)
        pruneStaleMedia()

        val storedTrack = libraryStore.getTrack(track.id, preferLocalMedia = false) ?: track
        val audioPath = storedTrack.localAudioPath
        if (audioPath.isNullOrBlank() || !File(audioPath).exists()) {
            downloadAudio(storedTrack)
        }

        val coverPath = storedTrack.localCoverPath
        if ((coverPath.isNullOrBlank() || !File(coverPath).exists()) &&
            !storedTrack.remoteCoverUrl.isNullOrBlank()
        ) {
            downloadCover(storedTrack)
        }

        enforceStorageLimit()
        publishStorageStats()
    }

    suspend fun clearCachedMedia() = withContext(Dispatchers.IO) {
        rootDir.deleteRecursively()
        libraryStore.clearCachedMedia()
        publishStorageStats()
    }

    suspend fun pruneStaleMedia() = withContext(Dispatchers.IO) {
        libraryStore.staleMediaPaths().forEach { stale ->
            File(stale.path).delete()
            if (stale.isAudio) {
                libraryStore.markAudioRemoved(stale.trackId)
            } else {
                libraryStore.markCoverRemoved(stale.trackId)
            }
        }
    }

    fun publishStorageStats() {
        val stats = libraryStore.storageStats()
        settingsStorage.setStorageStats(stats.usedBytes, stats.cachedTrackCount)
    }

    private fun downloadAudio(track: TrackCard) {
        val sourceUrl = track.remoteStreamUrl.ifBlank { track.streamUrl }
        if (sourceUrl.isBlank()) return

        val extension = extensionForMime(track.audioMimeType)
            ?: extensionFromUrl(sourceUrl)
            ?: "audio"
        val target = File(audioDir, "${track.id}-${track.audioHash ?: track.audioUpdatedAt ?: "audio"}.$extension")
        val bytes = downloadToFile(sourceUrl, target)
        libraryStore.markAudioCached(track.id, target.absolutePath, bytes)
    }

    private fun downloadCover(track: TrackCard) {
        val sourceUrl = track.remoteCoverUrl ?: track.coverUrl ?: return
        if (sourceUrl.isBlank()) return

        val extension = extensionFromUrl(sourceUrl) ?: "jpg"
        val target = File(coversDir, "${track.id}-${track.coverHash ?: track.coverUpdatedAt ?: "cover"}.$extension")
        val bytes = downloadToFile(sourceUrl, target)
        libraryStore.markCoverCached(track.id, target.absolutePath, bytes)
    }

    private fun downloadToFile(url: String, target: File): Long {
        target.parentFile?.mkdirs()
        val temp = File(target.parentFile, "${target.name}.tmp")
        temp.delete()

        val request = Request.Builder()
            .url(url)
            .build()

        okHttpClient.newCall(request).execute().use { response ->
            if (!response.isSuccessful) {
                throw IOException("Offline download failed with HTTP ${response.code}")
            }
            val body = response.body ?: throw IOException("Offline download response is empty")
            temp.outputStream().use { output ->
                body.byteStream().use { input ->
                    input.copyTo(output)
                }
            }
        }

        if (!temp.renameTo(target)) {
            temp.copyTo(target, overwrite = true)
            temp.delete()
        }
        return target.length()
    }

    private fun enforceStorageLimit() {
        val limitBytes = settingsStorage.state.value.settings.storageLimitBytes
        var usedBytes = libraryStore.storageStats().usedBytes
        if (usedBytes <= limitBytes) return

        for (entry in libraryStore.cachedAudioEntriesForEviction()) {
            if (usedBytes <= limitBytes) break
            File(entry.path).delete()
            libraryStore.markAudioRemoved(entry.trackId)
            usedBytes -= entry.sizeBytes
        }
    }

    private fun extensionForMime(mimeType: String?): String? {
        return when (mimeType?.lowercase()) {
            "audio/mpeg", "audio/mp3" -> "mp3"
            "audio/flac" -> "flac"
            "audio/ogg", "audio/opus" -> "ogg"
            "audio/mp4", "audio/aac" -> "m4a"
            "audio/wav", "audio/x-wav" -> "wav"
            else -> null
        }
    }

    private fun extensionFromUrl(url: String): String? {
        val cleanUrl = url.substringBefore('?').substringBefore('#')
        return cleanUrl.substringAfterLast('/', "")
            .substringAfterLast('.', "")
            .takeIf { it.length in 2..5 && it.all(Char::isLetterOrDigit) }
            ?.lowercase()
    }
}
