package es.verdnatura

import android.app.Application
import android.content.SharedPreferences
import android.media.MediaPlayer
import es.verdnatura.di.viewModelModule
import es.verdnatura.domain.RestClient
import es.verdnatura.domain.SalixService
import es.verdnatura.domain.VerdnaturaService
import es.verdnatura.presentation.view.component.CustomDialogMainActivity
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.loadKoinModules
import org.koin.core.context.startKoin

class MobileApplication : Application() {
    lateinit var silex: VerdnaturaService
    lateinit var salix: SalixService
    var userId: Int? = null
    var userName: String? = null
    var renewPeriod:Long = 0
    var renewInterval:Long = 0
    var userPassword: String? = null
    var hasNetwork:Boolean = true
    var serialNumber:String = ""
    var versionName:String = ""
    var androidId:String = ""
    var mperror: MediaPlayer? = null
    var mpok: MediaPlayer? = null
    private lateinit var customDialog: CustomDialogMainActivity


    protected val PREFS_USER = "es.verdnatura.user.prefs"

    override fun onCreate() {
        super.onCreate()
        mperror = MediaPlayer.create((this), R.raw.error)
        mpok = MediaPlayer.create((this), R.raw.ok)
        startKoin {
            androidContext(this@MobileApplication)
            val moduleList = listOf(viewModelModule)
            loadKoinModules(moduleList)
        }

        var restClient = RestClient(this)
        silex = restClient.restClient
        salix = restClient.salixClient
    }

    fun getPrefsEditor(): SharedPreferences.Editor {
        val prefs: SharedPreferences = getSharedPreferences(PREFS_USER, 0)
        return prefs.edit()
    }
    fun getPrefsShared(): SharedPreferences {
        return getSharedPreferences(PREFS_USER, 0)
    }

    fun playSoundIsOK(bool: Boolean) {
        if (bool) mpok!!.start() else mperror!!.start()
    }

}