package com.offline.music.task.player

import android.app.Application
import android.content.Context
import com.offline.music.task.player.db.bean.LocalAudio
import com.offline.music.task.player.db.bean.CurrentPlayingLocalAudio
import com.offline.music.task.player.db.bean.LocalResourcesList
import com.offline.music.task.player.db.tools.CurrentAudioManager
import com.offline.music.task.player.db.tools.DatabaseManager
import com.offline.music.task.player.tools.media.MediaControllerManager
import com.offline.music.task.player.tools.util.parseResources
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.BufferedReader
import java.io.InputStreamReader

class ProApp : Application() {
    companion object {
        lateinit var appContext: Context
        lateinit var proApp: ProApp
            private set
        lateinit var currentAudioManager: CurrentAudioManager
            private set
        lateinit var databaseManager: DatabaseManager
            private set
        var currentPlayingLocalAudio: CurrentPlayingLocalAudio? = null
            private set
        lateinit var importList: List<LocalAudio>
            private set
        lateinit var localResourcesList: LocalResourcesList
            private set

        lateinit var realHumanVoiceList: List<LocalAudio>
            private set

        lateinit var soundsOfAppliancesList: List<LocalAudio>
            private set

        lateinit var soundsOfNatureList: List<LocalAudio>
            private set

        private var isInitialized = false

        fun initialize(context: Context) {
            if (!isInitialized) {
                val jsonString = readAssetsFile(context)
                localResourcesList = parseResources(context, jsonString)
                splitResourcesList()
                isInitialized = true
            }
        }

        fun initCurrentPlayingAudio() {
            CoroutineScope(Dispatchers.IO).launch {
                currentPlayingLocalAudio = currentAudioManager.getCurrentPlayingAudio()
            }
        }

        fun initImportAudio(callback: (List<LocalAudio>) -> Unit = {}) {
            CoroutineScope(Dispatchers.IO).launch {
                importList = databaseManager.getAllAudioFiles()
                withContext(Dispatchers.Main) {
                    callback(importList)
                }
            }
        }

        private fun readAssetsFile(context: Context): String {
            val assetManager = context.assets
            val inputStream = assetManager.open("resources.json")
            val bufferedReader = BufferedReader(InputStreamReader(inputStream))
            val stringBuilder = StringBuilder()
            var line: String?
            while (bufferedReader.readLine().also { line = it } != null) {
                stringBuilder.append(line)
            }
            return stringBuilder.toString()
        }

        private fun splitResourcesList() {
            realHumanVoiceList = mutableListOf()
            soundsOfAppliancesList = mutableListOf()
            soundsOfNatureList = mutableListOf()

            for (category in localResourcesList.categories) {
                when (category.name) {
                    "Sound of instrument" -> realHumanVoiceList = category.localAudios
                    "White noise" -> soundsOfAppliancesList = category.localAudios
                    "Voice of Nature" -> soundsOfNatureList = category.localAudios
                }
            }
        }
    }

    override fun onCreate() {
        super.onCreate()
        proApp = this
        appContext = this
        initialize(this)
        MediaControllerManager.init(this)
        currentAudioManager = CurrentAudioManager.getInstance(this)
        databaseManager = DatabaseManager.getInstance(this)
        initCurrentPlayingAudio()
        initImportAudio()
    }

}